#include "simurgh.h"
#include <bits/stdc++.h>
#define pb push_back
#define MP make_pair
#define F first
#define S second
using namespace std;
typedef pair<int,int> pii;
vector<int> U,V;
vector<pii> G[505];
pii pa[505];
queue<int> q;
int type[505],dep[505],deg[505],boss[505],n;
set<int> s;
bitset<505> vis,ed;
bitset<250000> used;
int ccr()
{
vector<int> r;
for(auto i:s)
r.pb(i);
return count_common_roads(r);
}
void dfs(int u,int f,int num,int d)
{
pa[u]=MP(f,num),dep[u]=d,vis[u]=1;
if(~num) used[num]=1,s.insert(num);
for(pii i:G[u])
if(!vis[i.F])
dfs(i.F,u,i.S,d+1);
}
int finds(int x)
{
if(x==boss[x]) return x;
return boss[x]=finds(boss[x]);
}
int Union(int a,int b)
{
a=finds(a),b=finds(b);
if(a==b) return 0;
return boss[a]=b,1;
}
int ccr_forest(vector<int> f)
{
int cnt=0;
for(int i=0;i<n;++i)
boss[i]=i;
set<int> tmp;
for(int i:f)
tmp.insert(i),Union(U[i],V[i]);
for(int i=1;i<n;++i)
if(Union(i,pa[i].F))
cnt+=type[i],tmp.insert(pa[i].S);
tmp.swap(s);
cnt=ccr()-cnt;
tmp.swap(s);
return cnt;
}
int leaf_finding(int u)
{
vector<int> candi;
for(pii i:G[u])
if(!ed[i.F])
candi.pb(i.S);
int l=0,r=candi.size()-1;
while(l<r)
{
vector<int> v;
int m=l+r>>1;
for(int i=l;i<=m;++i)
v.pb(candi[i]);
if(ccr_forest(v)>=1) r=m;
else l=m+1;
}
return candi[l];
}
vector<int> find_roads(int N, vector<int> u, vector<int> v)
{
n=N,U=u,V=v;
vector<int> ans;
for(int i=0;i<u.size();++i)
G[u[i]].pb(MP(v[i],i)),G[v[i]].pb(MP(u[i],i));
dfs(0,-1,-1,0),fill(type,type+N,-1);
int cur=ccr();
for(int i=0;i<u.size();++i)
{
if(used[i]) continue;
int cnt=0,sz=0,tp=-1,a=u[i],b=v[i];
pii sw;
while(a!=b)
{
if(dep[a]<dep[b]) swap(a,b);
if(~type[a]) sw=MP(pa[a].S,type[a]);
cnt+=!~type[a],++sz,a=pa[a].F;
}
if(!cnt) continue;
a=u[i],b=v[i];
if(cnt==sz)
{
vector<int> v;
while(a!=b)
{
if(dep[a]<dep[b]) swap(a,b);
s.erase(pa[a].S),s.insert(i);
int tmp=ccr();
if(tmp==cur) v.pb(a);
else
{
if(tmp>cur) tp=1,type[a]=0;
else tp=0,type[a]=1;
for(int j:v)
type[j]=tp;
s.erase(i),s.insert(pa[a].S),a=pa[a].F;
break;
}
s.erase(i),s.insert(pa[a].S),a=pa[a].F;
}
if(a==b&&!~tp)
for(int j:v)
type[j]=0;
}
else
{
s.erase(sw.F),s.insert(i);
tp=(sw.S==0)^(ccr()==cur);
s.erase(i),s.insert(sw.F);
}
while(a!=b)
{
if(dep[a]<dep[b]) swap(a,b);
s.erase(pa[a].S),s.insert(i);
if(!~type[a])
type[a]=tp^(ccr()!=cur);
s.erase(i),s.insert(pa[a].S),a=pa[a].F;
}
}
for(int i=1;i<n;++i)
if(!~type[i]) type[i]=1;
for(int i=0;i<n;++i)
{
vector<int> v;
for(pii j:G[i])
v.pb(j.S);
deg[i]=ccr_forest(v);
if(deg[i]==1)
q.push(i);
}
while(q.size()>1)
{
int u=q.front();
q.pop(),ed[u]=1;
ans.pb(leaf_finding(u));
u^=U[ans.back()]^V[ans.back()];
if(--deg[u]==1)
q.push(u);
}
return ans;
}
Compilation message
simurgh.cpp: In function 'int leaf_finding(int)':
simurgh.cpp:76:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m=l+r>>1;
~^~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:89:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<u.size();++i)
~^~~~~~~~~
simurgh.cpp:93:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<u.size();++i)
~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
380 KB |
correct |
6 |
Correct |
3 ms |
376 KB |
correct |
7 |
Correct |
2 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
380 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 |
376 KB |
correct |
# |
Verdict |
Execution time |
Memory |
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 |
380 KB |
correct |
6 |
Correct |
3 ms |
376 KB |
correct |
7 |
Correct |
2 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
380 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 |
376 KB |
correct |
14 |
Correct |
5 ms |
376 KB |
correct |
15 |
Correct |
5 ms |
424 KB |
correct |
16 |
Correct |
5 ms |
376 KB |
correct |
17 |
Correct |
5 ms |
376 KB |
correct |
18 |
Correct |
4 ms |
376 KB |
correct |
19 |
Correct |
5 ms |
376 KB |
correct |
20 |
Correct |
5 ms |
376 KB |
correct |
21 |
Correct |
5 ms |
380 KB |
correct |
22 |
Correct |
4 ms |
376 KB |
correct |
23 |
Correct |
4 ms |
248 KB |
correct |
24 |
Correct |
4 ms |
376 KB |
correct |
25 |
Correct |
3 ms |
376 KB |
correct |
26 |
Correct |
4 ms |
308 KB |
correct |
27 |
Correct |
4 ms |
376 KB |
correct |
28 |
Correct |
3 ms |
376 KB |
correct |
29 |
Correct |
3 ms |
384 KB |
correct |
30 |
Correct |
4 ms |
376 KB |
correct |
31 |
Correct |
5 ms |
376 KB |
correct |
32 |
Correct |
4 ms |
376 KB |
correct |
33 |
Correct |
4 ms |
376 KB |
correct |
# |
Verdict |
Execution time |
Memory |
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 |
380 KB |
correct |
6 |
Correct |
3 ms |
376 KB |
correct |
7 |
Correct |
2 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
380 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 |
376 KB |
correct |
14 |
Correct |
5 ms |
376 KB |
correct |
15 |
Correct |
5 ms |
424 KB |
correct |
16 |
Correct |
5 ms |
376 KB |
correct |
17 |
Correct |
5 ms |
376 KB |
correct |
18 |
Correct |
4 ms |
376 KB |
correct |
19 |
Correct |
5 ms |
376 KB |
correct |
20 |
Correct |
5 ms |
376 KB |
correct |
21 |
Correct |
5 ms |
380 KB |
correct |
22 |
Correct |
4 ms |
376 KB |
correct |
23 |
Correct |
4 ms |
248 KB |
correct |
24 |
Correct |
4 ms |
376 KB |
correct |
25 |
Correct |
3 ms |
376 KB |
correct |
26 |
Correct |
4 ms |
308 KB |
correct |
27 |
Correct |
4 ms |
376 KB |
correct |
28 |
Correct |
3 ms |
376 KB |
correct |
29 |
Correct |
3 ms |
384 KB |
correct |
30 |
Correct |
4 ms |
376 KB |
correct |
31 |
Correct |
5 ms |
376 KB |
correct |
32 |
Correct |
4 ms |
376 KB |
correct |
33 |
Correct |
4 ms |
376 KB |
correct |
34 |
Correct |
108 ms |
1528 KB |
correct |
35 |
Correct |
106 ms |
1592 KB |
correct |
36 |
Correct |
94 ms |
1400 KB |
correct |
37 |
Correct |
37 ms |
376 KB |
correct |
38 |
Correct |
109 ms |
1656 KB |
correct |
39 |
Correct |
103 ms |
1528 KB |
correct |
40 |
Correct |
95 ms |
1400 KB |
correct |
41 |
Correct |
116 ms |
1656 KB |
correct |
42 |
Correct |
104 ms |
1528 KB |
correct |
43 |
Correct |
88 ms |
1200 KB |
correct |
44 |
Correct |
84 ms |
888 KB |
correct |
45 |
Correct |
86 ms |
1016 KB |
correct |
46 |
Correct |
78 ms |
1016 KB |
correct |
47 |
Correct |
61 ms |
632 KB |
correct |
48 |
Correct |
22 ms |
376 KB |
correct |
49 |
Correct |
40 ms |
376 KB |
correct |
50 |
Correct |
60 ms |
632 KB |
correct |
51 |
Correct |
86 ms |
1064 KB |
correct |
52 |
Correct |
81 ms |
932 KB |
correct |
53 |
Correct |
79 ms |
1016 KB |
correct |
54 |
Correct |
89 ms |
1144 KB |
correct |
55 |
Correct |
89 ms |
892 KB |
correct |
56 |
Correct |
86 ms |
888 KB |
correct |
57 |
Correct |
88 ms |
996 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
correct |
2 |
Correct |
2 ms |
376 KB |
correct |
3 |
Correct |
350 ms |
4088 KB |
correct |
4 |
Correct |
588 ms |
5624 KB |
correct |
5 |
Correct |
589 ms |
5624 KB |
correct |
6 |
Correct |
586 ms |
5564 KB |
correct |
7 |
Correct |
577 ms |
5624 KB |
correct |
8 |
Correct |
619 ms |
5624 KB |
correct |
9 |
Correct |
585 ms |
5496 KB |
correct |
10 |
Correct |
585 ms |
5624 KB |
correct |
11 |
Correct |
582 ms |
5624 KB |
correct |
12 |
Correct |
585 ms |
5624 KB |
correct |
13 |
Correct |
2 ms |
376 KB |
correct |
14 |
Correct |
577 ms |
5624 KB |
correct |
15 |
Correct |
594 ms |
5700 KB |
correct |
# |
Verdict |
Execution time |
Memory |
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 |
380 KB |
correct |
6 |
Correct |
3 ms |
376 KB |
correct |
7 |
Correct |
2 ms |
376 KB |
correct |
8 |
Correct |
2 ms |
380 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 |
376 KB |
correct |
14 |
Correct |
5 ms |
376 KB |
correct |
15 |
Correct |
5 ms |
424 KB |
correct |
16 |
Correct |
5 ms |
376 KB |
correct |
17 |
Correct |
5 ms |
376 KB |
correct |
18 |
Correct |
4 ms |
376 KB |
correct |
19 |
Correct |
5 ms |
376 KB |
correct |
20 |
Correct |
5 ms |
376 KB |
correct |
21 |
Correct |
5 ms |
380 KB |
correct |
22 |
Correct |
4 ms |
376 KB |
correct |
23 |
Correct |
4 ms |
248 KB |
correct |
24 |
Correct |
4 ms |
376 KB |
correct |
25 |
Correct |
3 ms |
376 KB |
correct |
26 |
Correct |
4 ms |
308 KB |
correct |
27 |
Correct |
4 ms |
376 KB |
correct |
28 |
Correct |
3 ms |
376 KB |
correct |
29 |
Correct |
3 ms |
384 KB |
correct |
30 |
Correct |
4 ms |
376 KB |
correct |
31 |
Correct |
5 ms |
376 KB |
correct |
32 |
Correct |
4 ms |
376 KB |
correct |
33 |
Correct |
4 ms |
376 KB |
correct |
34 |
Correct |
108 ms |
1528 KB |
correct |
35 |
Correct |
106 ms |
1592 KB |
correct |
36 |
Correct |
94 ms |
1400 KB |
correct |
37 |
Correct |
37 ms |
376 KB |
correct |
38 |
Correct |
109 ms |
1656 KB |
correct |
39 |
Correct |
103 ms |
1528 KB |
correct |
40 |
Correct |
95 ms |
1400 KB |
correct |
41 |
Correct |
116 ms |
1656 KB |
correct |
42 |
Correct |
104 ms |
1528 KB |
correct |
43 |
Correct |
88 ms |
1200 KB |
correct |
44 |
Correct |
84 ms |
888 KB |
correct |
45 |
Correct |
86 ms |
1016 KB |
correct |
46 |
Correct |
78 ms |
1016 KB |
correct |
47 |
Correct |
61 ms |
632 KB |
correct |
48 |
Correct |
22 ms |
376 KB |
correct |
49 |
Correct |
40 ms |
376 KB |
correct |
50 |
Correct |
60 ms |
632 KB |
correct |
51 |
Correct |
86 ms |
1064 KB |
correct |
52 |
Correct |
81 ms |
932 KB |
correct |
53 |
Correct |
79 ms |
1016 KB |
correct |
54 |
Correct |
89 ms |
1144 KB |
correct |
55 |
Correct |
89 ms |
892 KB |
correct |
56 |
Correct |
86 ms |
888 KB |
correct |
57 |
Correct |
88 ms |
996 KB |
correct |
58 |
Correct |
2 ms |
380 KB |
correct |
59 |
Correct |
2 ms |
376 KB |
correct |
60 |
Correct |
350 ms |
4088 KB |
correct |
61 |
Correct |
588 ms |
5624 KB |
correct |
62 |
Correct |
589 ms |
5624 KB |
correct |
63 |
Correct |
586 ms |
5564 KB |
correct |
64 |
Correct |
577 ms |
5624 KB |
correct |
65 |
Correct |
619 ms |
5624 KB |
correct |
66 |
Correct |
585 ms |
5496 KB |
correct |
67 |
Correct |
585 ms |
5624 KB |
correct |
68 |
Correct |
582 ms |
5624 KB |
correct |
69 |
Correct |
585 ms |
5624 KB |
correct |
70 |
Correct |
2 ms |
376 KB |
correct |
71 |
Correct |
577 ms |
5624 KB |
correct |
72 |
Correct |
594 ms |
5700 KB |
correct |
73 |
Correct |
2 ms |
376 KB |
correct |
74 |
Correct |
589 ms |
5496 KB |
correct |
75 |
Correct |
588 ms |
5624 KB |
correct |
76 |
Correct |
320 ms |
2296 KB |
correct |
77 |
Correct |
625 ms |
5628 KB |
correct |
78 |
Correct |
592 ms |
5624 KB |
correct |
79 |
Correct |
587 ms |
5624 KB |
correct |
80 |
Correct |
591 ms |
5496 KB |
correct |
81 |
Correct |
583 ms |
5088 KB |
correct |
82 |
Correct |
588 ms |
5496 KB |
correct |
83 |
Correct |
464 ms |
3320 KB |
correct |
84 |
Correct |
490 ms |
4088 KB |
correct |
85 |
Correct |
474 ms |
3832 KB |
correct |
86 |
Correct |
423 ms |
2552 KB |
correct |
87 |
Correct |
387 ms |
2168 KB |
correct |
88 |
Correct |
352 ms |
1604 KB |
correct |
89 |
Correct |
342 ms |
1400 KB |
correct |
90 |
Correct |
325 ms |
1400 KB |
correct |
91 |
Correct |
159 ms |
504 KB |
correct |
92 |
Correct |
91 ms |
512 KB |
correct |
93 |
Correct |
498 ms |
3764 KB |
correct |
94 |
Correct |
423 ms |
2424 KB |
correct |
95 |
Correct |
414 ms |
2552 KB |
correct |
96 |
Correct |
336 ms |
1528 KB |
correct |
97 |
Correct |
353 ms |
1656 KB |
correct |
98 |
Correct |
374 ms |
2040 KB |
correct |
99 |
Correct |
349 ms |
1528 KB |
correct |
100 |
Correct |
243 ms |
760 KB |
correct |
101 |
Correct |
95 ms |
376 KB |
correct |
102 |
Correct |
486 ms |
3064 KB |
correct |
103 |
Correct |
485 ms |
3168 KB |
correct |
104 |
Correct |
474 ms |
3064 KB |
correct |
105 |
Correct |
483 ms |
3168 KB |
correct |