Giving Awards (Codeforces 412D)
Tags: Others, dfs and similar

The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else.Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person x to his office for a reward, and then immediately invite person y, who has lent some money to person x, then they can meet. Of course, in such a situation, the joy of person x from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another.However, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist.

Input: The first line contains space-separated integers n and m  — the number of employees in R1 and the number of debt relations. Each of the following m lines contains two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), these integers indicate that the person number ai owes money to a person a number bi. Assume that all the employees are numbered from 1 to n.It is guaranteed that each pair of people p, q is mentioned in the input data at most once. In particular, the input data will not contain pairs p, q and q, p simultaneously.

Output: Print -1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on.If there are multiple correct orders, you are allowed to print any of them.


Sample Inputs:
2 1
1 2

3 3
1 2
2 3
3 1

Sample Outputs:
2 1

2 1 3

Solution (Python):
import sys
import collections
import threading

def solve():

    def dfs(nude, g, v, o):
        v.add(nude)
        for nei in g[nude]:
            if nei in v or nei == nude:
                continue
            else:
                dfs(nei, g, v, o)
        o.append(nude)
    first_line = input().split(' ')
    (nodes, edges) = (first_line[0], first_line[1])
    (nodes, edges) = (int(nodes), int(edges))
    fuck = nodes
    adj_list = collections.defaultdict(set)
    while edges > 0:
        edge = input().split(' ')
        (dest, src) = (edge[0], edge[1])
        adj_list[int(src)].add(int(dest))
        edges -= 1
    output = []
    visited = set()
    for j in range(nodes, 0, -1):
        if not j in visited:
            visited.add(j)
            dfs(j, adj_list, visited, output)
    if output:
        print(' '.join([str(element) for element in output[::-1]]))
sys.setrecursionlimit(2 ** 32 // 2 - 1)
threading.stack_size(1 << 27)
t = threading.Thread(target=solve)
t.start()
t.join()

Solution (Python):
(n, m) = map(int, input().split())
g = [set() for i in range(n)]
for i in range(m):
    (a, b) = map(int, input().split())
    g[a - 1].add(b - 1)
c = [0] * n
for i in range(n):
    c[i] = i
for i in range(n):
    j = i
    while j > 0 and c[j] in g[c[j - 1]]:
        (c[j], c[j - 1]) = (c[j - 1], c[j])
        j -= 1
for i in c:
    print(i + 1, end=' ')

Solution (Python):
(n, m) = map(int, input().split())
g = [set() for i in range(n)]
from sys import stdin
for i in range(m):
    (a, b) = map(int, stdin.readline().split())
    g[a - 1].add(b - 1)
c = [0] * n
for i in range(n):
    c[i] = i
for i in range(n):
    j = i
    while j > 0 and c[j] in g[c[j - 1]]:
        (c[j], c[j - 1]) = (c[j - 1], c[j])
        j -= 1
res = ''
for i in c:
    res += str(i + 1) + ' '
print(res)