-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path655D. Robot Rapping Results Report.cpp
74 lines (60 loc) · 1.31 KB
/
655D. Robot Rapping Results Report.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Idea:
- Binary search on `k`.
- In check function we can do topological sort to check if
there is a valid skills order.
- At anytime if there is more than 1 node in the priority queue
then there is no valid skills order.
*/
#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 1;
int n, m, in[N];
priority_queue<int> pq;
vector<pair<int, int> > edges;
vector<vector<int> > g;
bool can(int mid) {
while(!pq.empty())
pq.pop();
memset(in, 0, sizeof in);
g.clear();
g.resize(n);
for(int i = 0; i < mid; ++i) {
++in[edges[i].second];
g[edges[i].first].push_back(edges[i].second);
}
for(int i = 0; i < n; ++i)
if(in[i] == 0) {
pq.push(i);
if(pq.size() > 1)
return false;
}
while(!pq.empty()) {
int u = pq.top();
pq.pop();
for(int i = 0; i < g[u].size(); ++i)
if(--in[g[u][i]] == 0)
pq.push(g[u][i]);
if(pq.size() > 1)
return false;
}
return true;
}
int main() {
scanf("%d %d", &n, &m);
for(int i = 0, a, b; i < m; ++i) {
scanf("%d %d", &a, &b);
--a, --b;
edges.push_back({a, b});
}
int l = 1, r = m, mid, res = -1;
while(l <= r) {
mid = (l + r) >> 1;
if(can(mid))
res = mid, r = mid - 1;
else
l = mid + 1;
}
printf("%d\n", res);
return 0;
}