Between (Codeforces 1816E)
Tags: Others, dfs and similar

You are given an integer $$$n$$$, as well as $$$m$$$ pairs of integers $$$(a_i,b_i)$$$, where $$$1\leq a_i , b_i \leq n$$$, $$$a_i \ne b_i$$$.You want to construct a sequence satisfying the following requirements:  All elements in the sequence are integers between $$$1$$$ and $$$n$$$.  There is exactly one element with value $$$1$$$ in the sequence.  For each $$$i$$$ ($$$1 \le i \le m$$$), between any two elements (on different positions) in the sequence with value $$$a_i$$$, there is at least one element with value $$$b_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: Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \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 $$$n$$$ and $$$m$$$ ($$$1 \le n \le 1500$$$, $$$0 \le m \le 5000$$$) — the maximum possible value of the element of the sequence and the number of pairs.The $$$i$$$-th of the next $$$m$$$ lines contain two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i , b_i \le n$$$, $$$a_i \ne b_i$$$).$$$(a_i, b_i) \ne (a_j, b_j)$$$ for all $$$1 \le i < j \le m$$$.It is guaranteed that sum of $$$n$$$ over all test cases doesn't exceed $$$1500$$$ and the sum of $$$m$$$ over all test cases doesn't exceed $$$5000$$$.

Output: 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 $$$2$$$ lines.The first line contains an integer $$$s$$$, the maximum length of the sequence.The second line contains $$$s$$$ integers, each are between $$$1$$$ and $$$n$$$ 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 $$$2\cdot10^6$$$.

Note: In the first test case, there is an element $$$1$$$ between two elements with value $$$3$$$ and an element $$$1$$$ between two elements with value $$$2$$$. It can be shown that there is no suitable sequences of length more than $$$5$$$.In the second case, $$$[1]$$$ is the only possible sequence because there should be exactly one element with value $$$1$$$ in the sequence.In the third case, we can get an arbitrary long sequence like $$$1, 2, 2, 2, \ldots$$$.

Sample Inputs:
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

Sample Outputs:
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

Solution (Python):
import sys
input = sys.stdin.buffer.readline

def solve():
    (n, m) = map(int, input().split())
    adj = {}
    for i in range(n + 1):
        adj[i] = []
    for i in range(m):
        (u, v) = map(int, input().split())
        adj[v].append(u)
    occur = {}
    for i in range(1, n + 1):
        occur[i] = 0
    occur[1] = 1
    queue = [1]
    while queue:
        u = queue.pop(0)
        for v in adj[u]:
            if occur[v] == 0:
                occur[v] = occur[u] + 1
                queue.append(v)
    for i in occur:
        if occur[i] == 0:
            print('INFINITE')
            return
    print('FINITE')
    l = []
    for i in occur:
        l.append([i, occur[i]])
    l.sort(key=lambda x: x[1], reverse=True)
    ans = []
    while l:
        for i in l:
            ans.append(i[0])
            i[1] -= 1
        while l and l[-1][1] == 0:
            l.pop()
    print(len(ans))
    print(*ans)
if __name__ == '__main__':
    for i in range(int(input())):
        solve()

Solution (Python):
def solve():
    (n, m) = map(int, input().split())
    adj = {}
    for i in range(n + 1):
        adj[i] = []
    for i in range(m):
        (u, v) = map(int, input().split())
        adj[v].append(u)
    occur = {}
    for i in range(1, n + 1):
        occur[i] = 0
    occur[1] = 1
    queue = [1]
    while queue:
        u = queue.pop(0)
        for v in adj[u]:
            if occur[v] == 0:
                occur[v] = occur[u] + 1
                queue.append(v)
    for i in occur:
        if occur[i] == 0:
            print('INFINITE')
            return
    print('FINITE')
    l = []
    for i in occur:
        l.append([i, occur[i]])
    l.sort(key=lambda x: x[1], reverse=True)
    ans = []
    while l:
        for i in l:
            ans.append(i[0])
            i[1] -= 1
        while l and l[-1][1] == 0:
            l.pop()
    print(len(ans))
    print(*ans)
if __name__ == '__main__':
    for i in range(int(input())):
        solve()

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, m) = readInts()
    graph = [[] for _ in range(n)]
    for _ in range(m):
        (a, b) = readInts()
        if a == 1:
            continue
        a -= 1
        b -= 1
        graph[b].append(a)
    d = [n + 1] * n
    d[0] = 0
    qu = deque([(0, 0)])
    while qu:
        (node, dis) = qu.popleft()
        for nei in graph[node]:
            if d[nei] > dis + 1:
                qu.append((nei, dis + 1))
                d[nei] = dis + 1
    if any([v == n + 1 for v in d]):
        print('INFINITE')
        return
    nodes = [[] for _ in range(n)]
    for i in range(n):
        nodes[d[i]].append(i + 1)
    ans = []
    for i in range(n - 1, -1, -1):
        for j in range(n - 1, n - 1 - i - 1, -1):
            ans.extend(nodes[j])
    print('FINITE')
    print(len(ans))
    print(*ans)
    return
for _ in range(int(input())):
    solve()