message
stringlengths
2
65.1k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
0
108k
cluster
float64
14
14
__index_level_0__
int64
0
217k
Provide tags and a correct Python 3 solution for this coding contest problem. While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot i will beat robot j if and only if robot i has a higher skill level than robot j. And if robot i beats robot j and robot j beats robot k, then robot i will beat robot k. Since rapping is such a subtle art, two robots can never have the same skill level. Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level. Input The first line of the input consists of two integers, the number of robots n (2 ≤ n ≤ 100 000) and the number of rap battles m (<image>). The next m lines describe the results of the rap battles in the order they took place. Each consists of two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi), indicating that robot ui beat robot vi in the i-th rap battle. No two rap battles involve the same pair of robots. It is guaranteed that at least one ordering of the robots satisfies all m relations. Output Print the minimum k such that the ordering of the robots by skill level is uniquely defined by the first k rap battles. If there exists more than one ordering that satisfies all m relations, output -1. Examples Input 4 5 2 1 1 3 2 3 4 2 4 3 Output 4 Input 3 2 1 2 3 2 Output -1 Note In the first sample, the robots from strongest to weakest must be (4, 2, 1, 3), which Bessie can deduce after knowing the results of the first four rap battles. In the second sample, both (1, 3, 2) and (3, 1, 2) are possible orderings of the robots from strongest to weakest after both rap battles.
instruction
0
108,266
14
216,532
Tags: binary search, dp, graphs Correct Solution: ``` n, m = [int(i) for i in input().split()] n += 1 # one-indexed A = [[int(i) for i in input().split()] for j in range(m)] m += 1 def check(upper): p = [[] for i in range(n)] d = [0] * n #record num of parents for u, v in A[:upper]: p[u].append(v) # form arc from u to v d[v] += 1 if d.count(0) > 2: return False x = d.index(0, 1) # find the real ancestor, should only be one while x: q, x = p[x], 0 for y in q: d[y] -= 1 if d[y] == 0: if x: return False x = y return True left, right = 1, m while left < right: mid = (left + right)//2 if check(mid): right = mid else: left = mid + 1 if check(left): print(left) else: print(-1) ```
output
1
108,266
14
216,533
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot i will beat robot j if and only if robot i has a higher skill level than robot j. And if robot i beats robot j and robot j beats robot k, then robot i will beat robot k. Since rapping is such a subtle art, two robots can never have the same skill level. Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level. Input The first line of the input consists of two integers, the number of robots n (2 ≤ n ≤ 100 000) and the number of rap battles m (<image>). The next m lines describe the results of the rap battles in the order they took place. Each consists of two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi), indicating that robot ui beat robot vi in the i-th rap battle. No two rap battles involve the same pair of robots. It is guaranteed that at least one ordering of the robots satisfies all m relations. Output Print the minimum k such that the ordering of the robots by skill level is uniquely defined by the first k rap battles. If there exists more than one ordering that satisfies all m relations, output -1. Examples Input 4 5 2 1 1 3 2 3 4 2 4 3 Output 4 Input 3 2 1 2 3 2 Output -1 Note In the first sample, the robots from strongest to weakest must be (4, 2, 1, 3), which Bessie can deduce after knowing the results of the first four rap battles. In the second sample, both (1, 3, 2) and (3, 1, 2) are possible orderings of the robots from strongest to weakest after both rap battles. Submitted Solution: ``` n, m = map(int, input().split()) curWin, curLos = map(int, input().split()) done = 2 if done == n: print(1) else: flag = False for i in range(1, m): u, v = map(int, input().split()) if v == curWin: curWin = u done += 1 if u == curLos: curLos = v done += 1 if done == n: flag = True print(i + 1) break if not flag: print(-1) ```
instruction
0
108,267
14
216,534
No
output
1
108,267
14
216,535