CF BUDDY
← Problems·

1815C · Between

2200 · constructive algorithms, dfs and similar, graphs

Problem: You are given an integer nn, as well as mm pairs of integers (ai,bi)(a_i,b_i), where 1ai,bin1\leq a_i , b_i \leq n, aibia_i \ne b_i.

You want to construct a sequence satisfying the following requirements:

  • All elements in the sequence are integers between 11 and nn.
  • There is exactly one element with value 11 in the sequence.
  • For each ii (1im1 \le i \le m), between any two elements (on different positions) in the sequence with value aia_i, there is at least one element with value bib_i.
  • The sequence constructed has the maximum length among all possible sequences satisfying the above properties.

Sometimes, it is possible that such a sequence can be arbitrarily long, in which case you should output "INFINITE". Otherwise, you should output "FINITE" and the sequence itself. If there are multiple possible constructions that yield the maximum length, output any.

Input Format: Each test consists of multiple test cases. The first line contains a single integer tt (1t3001 \le t \le 300) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers nn and mm (1n15001 \le n \le 1500, 0m50000 \le m \le 5000) — the maximum possible value of the element of the sequence and the number of pairs.

The ii-th of the next mm lines contain two integers aia_i and bib_i (1ai,bin1 \le a_i , b_i \le n, aibia_i \ne b_i).

(ai,bi)(aj,bj)(a_i, b_i) \ne (a_j, b_j) for all 1i<jm1 \le i < j \le m.

It is guaranteed that sum of nn over all test cases doesn't exceed 15001500 and the sum of mm over all test cases doesn't exceed 50005000.

Output Format: For each test case, on the first line output "INFINITE" if the sequence can be arbitrarily long, and "FINITE" otherwise.

If you have outputted "FINITE", then your output should be followed by 22 lines.

The first line contains an integer ss, the maximum length of the sequence.

The second line contains ss integers, each are between 11 and nn inclusive, representing the elements of the sequence.

If there are multiple sequences with the maximum length, output any of them.

It can be shown that, for all test cases with answer "FINITE", then under the constraints, the maximum possible sum of sequence lengths of those test cases does not exceed 21062\cdot10^6.

Note: In the first test case, there is an element 11 between two elements with value 33 and an element 11 between two elements with value 22. It can be shown that there is no suitable sequences of length more than 55.

In the second case, [1][1] is the only possible sequence because there should be exactly one element with value 11 in the sequence.

In the third case, we can get an arbitrary long sequence like 1,2,2,2,1, 2, 2, 2, \ldots.

Sample Cases

Case 1

Input

5
3 2
3 1
2 1
1 0
2 0
2 2
1 2
2 1
5 5
2 1
3 1
4 2
4 5
5 1

Output

FINITE
5
2 3 1 2 3 
FINITE
1
1 
INFINITE
FINITE
3
2 1 2 
FINITE
10
4 2 3 5 4 1 3 2 5 4

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback