#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
vector<pair<int, int> > edge;
bool ans[150000], ch[150000], in[505];
vi inq, tr, adj[505], vall, qq, ret, cyc, fin, onn, off;
int p[505], t, par[505], dep[505], n, m;
int get(int v, int i){
return edge[i].ff + edge[i].ss - v;
}
void reset(){
for(int i = 0; i < n; i++)
p[i] = i;
}
int find(int v){
return v == p[v] ? v : p[v] = find(p[v]);
}
bool merge(int v, int u){
v = find(v);
u = find(u);
if(v == u) return 0;
p[v] = u;
return 1;
}
int ask(){
reset();
for(int x : inq)
merge(edge[x].ff, edge[x].ss);
t = 0;
for(int x : tr){
if(merge(edge[x].ff, edge[x].ss)){
inq.pb(x);
t += ans[x];
}
}
return count_common_roads(inq);
}
void dfs(int v){
for(int x : adj[v]){
int u = get(v, x);
if(dep[u]) continue;
par[u] = x;
dep[u] = dep[v] + 1;
tr.pb(x);
dfs(u);
}
}
int fillexc(int x){
inq = cyc;
for(int& a : inq){
if(a == x){
swap(a, inq.back());
inq.pop_back();
break;
}
}
return ask();
}
void cycle(){
if(fin.empty()) return;
int lo;
if(onn.empty() && off.empty()){
lo = -1;
vall.clear();
for(int& x : cyc)
vall.pb(fillexc(x));
for(int& x : vall)
lo = max(lo, x);
for(int i = cyc.size() - 1; i >= 0; i--){
ch[cyc[i]] = 1;
ans[cyc[i]] = (lo - vall[i]);
}
} else {
if(off.empty())
lo = fillexc(onn[0]);
else
lo = fillexc(off[0]) - 1;
for(int x : fin){
ch[x] = 1;
ans[x] = (fillexc(x) == lo);
}
}
}
void tree(){
for(int i = 0; i < m; i++){
if(dep[edge[i].ff] < dep[edge[i].ss]) swap(edge[i].ff, edge[i].ss);
if(dep[edge[i].ff] - dep[edge[i].ss] == 1) continue;
cyc.clear(); fin.clear(); off.clear(); onn.clear();
cyc.pb(i);
int s = edge[i].ff;
while(s != edge[i].ss){
cyc.pb(par[s]);
if(ch[par[s]] == 0) fin.pb(par[s]);
else if(ans[par[s]] == 1) onn.pb(par[s]);
else off.pb(par[s]);
s = get(s, par[s]);
}
cycle();
}
for(int x : tr)
if(!ch[x])
ch[x] = ans[x] = 1;
}
void dfs2(int v){
in[v] = 1;
for(int i = adj[v].size() - 1; i >= 0; i--){
if(ch[adj[v][i]]){
swap(adj[v][i], adj[v].back());
adj[v].pop_back();
continue;
}
int u = get(v, adj[v][i]);
if(in[u]) continue;
qq.pb(adj[v][i]);
dfs2(u);
}
}
void vsolve(){
if(qq.empty()) return;
inq = qq;
if(ask() == t){
for(int& x : qq){
ch[x] = 1;
ans[x] = 0;
}
return;
}
int l = 0, r = qq.size() - 1;
while(l != r){
int m = (l + r) >> 1;
inq.clear();
for(int i = 0; i <= m; i++)
inq.pb(qq[i]);
if(ask() == t)
l = m + 1;
else
r = m;
}
for(int i = 0; i < r; i++){
ch[qq[i]] = 1;
ans[qq[i]] = 0;
}
ch[qq[r]] = ans[qq[r]] = 1;
}
vi find_roads(int N, vi u, vi v) {
n = N;
m = u.size();
for(int i = 0; i < m; i++){
adj[v[i]].pb(i);
adj[u[i]].pb(i);
edge.pb({v[i], u[i]});
}
dep[0] = 1;
dfs(0);
tree();
do{
memset(in, 0, sizeof in);
qq.clear();
for(int i = 0; i < n; i++)
if(!in[i])
dfs2(i);
vsolve();
} while(!qq.empty());
for(int i = 0; i < m; i++)
if(ans[i])
ret.pb(i);
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
2 ms |
376 KB |
correct |
4 |
Correct |
2 ms |
376 KB |
correct |
5 |
Correct |
2 ms |
376 KB |
correct |
6 |
Correct |
2 ms |
376 KB |
correct |
7 |
Correct |
2 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
376 KB |
correct |
9 |
Correct |
2 ms |
376 KB |
correct |
10 |
Correct |
2 ms |
376 KB |
correct |
11 |
Correct |
2 ms |
376 KB |
correct |
12 |
Correct |
2 ms |
376 KB |
correct |
13 |
Correct |
2 ms |
380 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
2 ms |
376 KB |
correct |
4 |
Correct |
2 ms |
376 KB |
correct |
5 |
Correct |
2 ms |
376 KB |
correct |
6 |
Correct |
2 ms |
376 KB |
correct |
7 |
Correct |
2 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
376 KB |
correct |
9 |
Correct |
2 ms |
376 KB |
correct |
10 |
Correct |
2 ms |
376 KB |
correct |
11 |
Correct |
2 ms |
376 KB |
correct |
12 |
Correct |
2 ms |
376 KB |
correct |
13 |
Correct |
2 ms |
380 KB |
correct |
14 |
Correct |
4 ms |
380 KB |
correct |
15 |
Correct |
4 ms |
376 KB |
correct |
16 |
Correct |
4 ms |
376 KB |
correct |
17 |
Correct |
4 ms |
376 KB |
correct |
18 |
Correct |
3 ms |
380 KB |
correct |
19 |
Correct |
4 ms |
376 KB |
correct |
20 |
Correct |
4 ms |
376 KB |
correct |
21 |
Correct |
4 ms |
376 KB |
correct |
22 |
Correct |
3 ms |
380 KB |
correct |
23 |
Correct |
3 ms |
376 KB |
correct |
24 |
Correct |
3 ms |
376 KB |
correct |
25 |
Correct |
2 ms |
376 KB |
correct |
26 |
Correct |
3 ms |
376 KB |
correct |
27 |
Correct |
3 ms |
376 KB |
correct |
28 |
Correct |
2 ms |
376 KB |
correct |
29 |
Correct |
2 ms |
376 KB |
correct |
30 |
Correct |
3 ms |
376 KB |
correct |
31 |
Correct |
3 ms |
376 KB |
correct |
32 |
Correct |
3 ms |
376 KB |
correct |
33 |
Correct |
3 ms |
376 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
2 ms |
376 KB |
correct |
4 |
Correct |
2 ms |
376 KB |
correct |
5 |
Correct |
2 ms |
376 KB |
correct |
6 |
Correct |
2 ms |
376 KB |
correct |
7 |
Correct |
2 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
376 KB |
correct |
9 |
Correct |
2 ms |
376 KB |
correct |
10 |
Correct |
2 ms |
376 KB |
correct |
11 |
Correct |
2 ms |
376 KB |
correct |
12 |
Correct |
2 ms |
376 KB |
correct |
13 |
Correct |
2 ms |
380 KB |
correct |
14 |
Correct |
4 ms |
380 KB |
correct |
15 |
Correct |
4 ms |
376 KB |
correct |
16 |
Correct |
4 ms |
376 KB |
correct |
17 |
Correct |
4 ms |
376 KB |
correct |
18 |
Correct |
3 ms |
380 KB |
correct |
19 |
Correct |
4 ms |
376 KB |
correct |
20 |
Correct |
4 ms |
376 KB |
correct |
21 |
Correct |
4 ms |
376 KB |
correct |
22 |
Correct |
3 ms |
380 KB |
correct |
23 |
Correct |
3 ms |
376 KB |
correct |
24 |
Correct |
3 ms |
376 KB |
correct |
25 |
Correct |
2 ms |
376 KB |
correct |
26 |
Correct |
3 ms |
376 KB |
correct |
27 |
Correct |
3 ms |
376 KB |
correct |
28 |
Correct |
2 ms |
376 KB |
correct |
29 |
Correct |
2 ms |
376 KB |
correct |
30 |
Correct |
3 ms |
376 KB |
correct |
31 |
Correct |
3 ms |
376 KB |
correct |
32 |
Correct |
3 ms |
376 KB |
correct |
33 |
Correct |
3 ms |
376 KB |
correct |
34 |
Correct |
93 ms |
1432 KB |
correct |
35 |
Correct |
91 ms |
1396 KB |
correct |
36 |
Correct |
69 ms |
1144 KB |
correct |
37 |
Correct |
25 ms |
456 KB |
correct |
38 |
Correct |
94 ms |
1396 KB |
correct |
39 |
Correct |
82 ms |
1272 KB |
correct |
40 |
Correct |
70 ms |
1144 KB |
correct |
41 |
Correct |
87 ms |
1396 KB |
correct |
42 |
Correct |
89 ms |
1524 KB |
correct |
43 |
Correct |
30 ms |
1016 KB |
correct |
44 |
Correct |
25 ms |
888 KB |
correct |
45 |
Correct |
28 ms |
888 KB |
correct |
46 |
Correct |
25 ms |
888 KB |
correct |
47 |
Correct |
15 ms |
632 KB |
correct |
48 |
Correct |
5 ms |
504 KB |
correct |
49 |
Correct |
11 ms |
376 KB |
correct |
50 |
Correct |
15 ms |
632 KB |
correct |
51 |
Correct |
29 ms |
888 KB |
correct |
52 |
Correct |
25 ms |
888 KB |
correct |
53 |
Correct |
24 ms |
936 KB |
correct |
54 |
Correct |
30 ms |
1016 KB |
correct |
55 |
Correct |
31 ms |
888 KB |
correct |
56 |
Correct |
31 ms |
888 KB |
correct |
57 |
Correct |
30 ms |
1016 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
386 ms |
3572 KB |
correct |
4 |
Correct |
736 ms |
4840 KB |
correct |
5 |
Correct |
728 ms |
4900 KB |
correct |
6 |
Correct |
676 ms |
4800 KB |
correct |
7 |
Correct |
674 ms |
4716 KB |
correct |
8 |
Correct |
671 ms |
4688 KB |
correct |
9 |
Correct |
721 ms |
4732 KB |
correct |
10 |
Correct |
720 ms |
4844 KB |
correct |
11 |
Correct |
719 ms |
4836 KB |
correct |
12 |
Correct |
732 ms |
4856 KB |
correct |
13 |
Correct |
2 ms |
376 KB |
correct |
14 |
Correct |
732 ms |
4688 KB |
correct |
15 |
Correct |
714 ms |
4844 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
2 ms |
376 KB |
correct |
4 |
Correct |
2 ms |
376 KB |
correct |
5 |
Correct |
2 ms |
376 KB |
correct |
6 |
Correct |
2 ms |
376 KB |
correct |
7 |
Correct |
2 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
376 KB |
correct |
9 |
Correct |
2 ms |
376 KB |
correct |
10 |
Correct |
2 ms |
376 KB |
correct |
11 |
Correct |
2 ms |
376 KB |
correct |
12 |
Correct |
2 ms |
376 KB |
correct |
13 |
Correct |
2 ms |
380 KB |
correct |
14 |
Correct |
4 ms |
380 KB |
correct |
15 |
Correct |
4 ms |
376 KB |
correct |
16 |
Correct |
4 ms |
376 KB |
correct |
17 |
Correct |
4 ms |
376 KB |
correct |
18 |
Correct |
3 ms |
380 KB |
correct |
19 |
Correct |
4 ms |
376 KB |
correct |
20 |
Correct |
4 ms |
376 KB |
correct |
21 |
Correct |
4 ms |
376 KB |
correct |
22 |
Correct |
3 ms |
380 KB |
correct |
23 |
Correct |
3 ms |
376 KB |
correct |
24 |
Correct |
3 ms |
376 KB |
correct |
25 |
Correct |
2 ms |
376 KB |
correct |
26 |
Correct |
3 ms |
376 KB |
correct |
27 |
Correct |
3 ms |
376 KB |
correct |
28 |
Correct |
2 ms |
376 KB |
correct |
29 |
Correct |
2 ms |
376 KB |
correct |
30 |
Correct |
3 ms |
376 KB |
correct |
31 |
Correct |
3 ms |
376 KB |
correct |
32 |
Correct |
3 ms |
376 KB |
correct |
33 |
Correct |
3 ms |
376 KB |
correct |
34 |
Correct |
93 ms |
1432 KB |
correct |
35 |
Correct |
91 ms |
1396 KB |
correct |
36 |
Correct |
69 ms |
1144 KB |
correct |
37 |
Correct |
25 ms |
456 KB |
correct |
38 |
Correct |
94 ms |
1396 KB |
correct |
39 |
Correct |
82 ms |
1272 KB |
correct |
40 |
Correct |
70 ms |
1144 KB |
correct |
41 |
Correct |
87 ms |
1396 KB |
correct |
42 |
Correct |
89 ms |
1524 KB |
correct |
43 |
Correct |
30 ms |
1016 KB |
correct |
44 |
Correct |
25 ms |
888 KB |
correct |
45 |
Correct |
28 ms |
888 KB |
correct |
46 |
Correct |
25 ms |
888 KB |
correct |
47 |
Correct |
15 ms |
632 KB |
correct |
48 |
Correct |
5 ms |
504 KB |
correct |
49 |
Correct |
11 ms |
376 KB |
correct |
50 |
Correct |
15 ms |
632 KB |
correct |
51 |
Correct |
29 ms |
888 KB |
correct |
52 |
Correct |
25 ms |
888 KB |
correct |
53 |
Correct |
24 ms |
936 KB |
correct |
54 |
Correct |
30 ms |
1016 KB |
correct |
55 |
Correct |
31 ms |
888 KB |
correct |
56 |
Correct |
31 ms |
888 KB |
correct |
57 |
Correct |
30 ms |
1016 KB |
correct |
58 |
Correct |
2 ms |
376 KB |
correct |
59 |
Correct |
2 ms |
376 KB |
correct |
60 |
Correct |
386 ms |
3572 KB |
correct |
61 |
Correct |
736 ms |
4840 KB |
correct |
62 |
Correct |
728 ms |
4900 KB |
correct |
63 |
Correct |
676 ms |
4800 KB |
correct |
64 |
Correct |
674 ms |
4716 KB |
correct |
65 |
Correct |
671 ms |
4688 KB |
correct |
66 |
Correct |
721 ms |
4732 KB |
correct |
67 |
Correct |
720 ms |
4844 KB |
correct |
68 |
Correct |
719 ms |
4836 KB |
correct |
69 |
Correct |
732 ms |
4856 KB |
correct |
70 |
Correct |
2 ms |
376 KB |
correct |
71 |
Correct |
732 ms |
4688 KB |
correct |
72 |
Correct |
714 ms |
4844 KB |
correct |
73 |
Correct |
2 ms |
376 KB |
correct |
74 |
Correct |
740 ms |
4980 KB |
correct |
75 |
Correct |
701 ms |
4704 KB |
correct |
76 |
Correct |
240 ms |
2036 KB |
correct |
77 |
Correct |
720 ms |
4856 KB |
correct |
78 |
Correct |
714 ms |
4716 KB |
correct |
79 |
Correct |
723 ms |
4716 KB |
correct |
80 |
Correct |
687 ms |
4808 KB |
correct |
81 |
Correct |
573 ms |
4220 KB |
correct |
82 |
Correct |
693 ms |
4540 KB |
correct |
83 |
Correct |
404 ms |
2904 KB |
correct |
84 |
Correct |
216 ms |
3440 KB |
correct |
85 |
Correct |
195 ms |
3028 KB |
correct |
86 |
Correct |
128 ms |
2160 KB |
correct |
87 |
Correct |
98 ms |
1652 KB |
correct |
88 |
Correct |
83 ms |
1344 KB |
correct |
89 |
Correct |
85 ms |
1244 KB |
correct |
90 |
Correct |
73 ms |
1268 KB |
correct |
91 |
Correct |
36 ms |
504 KB |
correct |
92 |
Correct |
16 ms |
376 KB |
correct |
93 |
Correct |
191 ms |
3028 KB |
correct |
94 |
Correct |
129 ms |
2036 KB |
correct |
95 |
Correct |
124 ms |
2040 KB |
correct |
96 |
Correct |
81 ms |
1360 KB |
correct |
97 |
Correct |
86 ms |
1396 KB |
correct |
98 |
Correct |
96 ms |
1752 KB |
correct |
99 |
Correct |
84 ms |
1272 KB |
correct |
100 |
Correct |
50 ms |
632 KB |
correct |
101 |
Correct |
24 ms |
376 KB |
correct |
102 |
Correct |
202 ms |
2672 KB |
correct |
103 |
Correct |
207 ms |
2676 KB |
correct |
104 |
Correct |
198 ms |
2636 KB |
correct |
105 |
Correct |
196 ms |
2544 KB |
correct |