#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
struct edge{
int u,v;
}e[150000];
int n,m,ch[150000],low[501],tin[501],tout[501],t,par[501],cur,p[501];
vector <int> ve,res;
set <int> s,ke[501];
int ask(set <int> s){
vector <int> r;
for (int i:s)
r.push_back(i);
return count_common_roads(r);
}
void dfs(int u, int parent){
tin[u]=low[u]=++t;
for (int i:ke[u]){
int v=e[i].u^e[i].v^u;
if (v!=parent){
if (!tin[v]){
s.insert(i);
par[v]=i;
dfs(v,u);
low[u]=min(low[u],low[v]);
if (low[v]==tin[v])
ch[i]=1;
}
else
low[u]=min(low[u],tin[v]);
}
}
tout[u]=t;
}
bool isancestor(int u, int v){
return (tin[u]<=tin[v]&&tout[u]>=tout[v]);
}
void get_path(int u, int v){
ve.clear();
while (!isancestor(u,v)){
ve.push_back(par[u]);
u^=e[par[u]].u^e[par[u]].v;
}
while (!isancestor(v,u)){
ve.push_back(par[v]);
v^=e[par[v]].u^e[par[v]].v;
}
}
int f(int i){
return (p[i]==i?i:p[i]=f(p[i]));
}
bool unite(int i, int j){
i=f(i);
j=f(j);
if (i==j)
return false;
p[j]=i;
return true;
}
int calc(set <int> s2){
iota(p,p+n,0);
for (int i:s2)
unite(e[i].u,e[i].v);
int cnt=0;
for (int i:s)
if (unite(e[i].u,e[i].v)){
s2.insert(i);
cnt+=ch[i];
}
return ask(s2)-cnt;
}
vector <int> find_roads(int N, vector <int> u, vector <int> v){
n=N,m=u.size();
for (int i=0;i<m;i++){
e[i]={u[i],v[i]};
ke[u[i]].insert(i);
ke[v[i]].insert(i);
}
fill(ch,ch+m,-1);
dfs(0,0);
cur=ask(s);
for (int i=0;i<m;i++)
if (s.find(i)==s.end()){
get_path(u[i],v[i]);
int c=1;
for (int j:ve)
if (ch[j]==-1)
c=0;
if (c)
continue;
for (int j:ve){
if (ch[j]!=-1){
s.erase(j);
s.insert(i);
ch[i]=(ch[j]?1-cur+ask(s):ask(s)-cur);
s.erase(i);
s.insert(j);
break;
}
}
if (ch[i]!=-1){
for (int j:ve)
if (ch[j]==-1){
s.erase(j);
s.insert(i);
ch[j]=(ch[i]?1+cur-ask(s):cur-ask(s));
s.erase(i);
s.insert(j);
}
continue;
}
int mx=-1;
for (int j:ve){
s.erase(j);
s.insert(i);
ch[j]=ask(s)-cur;
mx=max(mx,ch[j]);
s.erase(i);
s.insert(j);
}
if (mx==1){
ch[i]=1;
for (int j:ve)
ch[j]^=1;
continue;
}
ch[i]=0;
for (int j:ve)
ch[j]=-ch[j];
}
vector <int> tmp;
set <int> S;
for (int i=0;i<n;i++){
int deg=calc(ke[i]);
while (deg){
tmp.clear();
for (int j:ke[i])
tmp.push_back(j);
int l=0,r=tmp.size()-1,kq=-1;
while (l<=r){
int mid=(l+r)>>1;
S.clear();
for (int i=0;i<=mid;i++)
S.insert(tmp[i]);
if (calc(S)==deg){
kq=tmp[mid];
r=mid-1;
}
else
l=mid+1;
}
res.push_back(kq);
ke[u[kq]].erase(kq);
ke[v[kq]].erase(kq);
deg--;
}
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
correct |
2 |
Correct |
1 ms |
348 KB |
correct |
3 |
Correct |
1 ms |
348 KB |
correct |
4 |
Correct |
1 ms |
344 KB |
correct |
5 |
Correct |
1 ms |
344 KB |
correct |
6 |
Correct |
0 ms |
344 KB |
correct |
7 |
Correct |
1 ms |
344 KB |
correct |
8 |
Correct |
0 ms |
344 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
1 ms |
348 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
0 ms |
348 KB |
correct |
13 |
Correct |
1 ms |
348 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
correct |
2 |
Correct |
1 ms |
348 KB |
correct |
3 |
Correct |
1 ms |
348 KB |
correct |
4 |
Correct |
1 ms |
344 KB |
correct |
5 |
Correct |
1 ms |
344 KB |
correct |
6 |
Correct |
0 ms |
344 KB |
correct |
7 |
Correct |
1 ms |
344 KB |
correct |
8 |
Correct |
0 ms |
344 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
1 ms |
348 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
0 ms |
348 KB |
correct |
13 |
Correct |
1 ms |
348 KB |
correct |
14 |
Correct |
4 ms |
604 KB |
correct |
15 |
Correct |
3 ms |
604 KB |
correct |
16 |
Correct |
4 ms |
472 KB |
correct |
17 |
Correct |
3 ms |
604 KB |
correct |
18 |
Correct |
2 ms |
348 KB |
correct |
19 |
Correct |
3 ms |
604 KB |
correct |
20 |
Correct |
5 ms |
604 KB |
correct |
21 |
Correct |
3 ms |
344 KB |
correct |
22 |
Correct |
3 ms |
348 KB |
correct |
23 |
Correct |
4 ms |
564 KB |
correct |
24 |
Correct |
4 ms |
348 KB |
correct |
25 |
Correct |
2 ms |
348 KB |
correct |
26 |
Correct |
2 ms |
348 KB |
correct |
27 |
Correct |
3 ms |
600 KB |
correct |
28 |
Correct |
2 ms |
348 KB |
correct |
29 |
Correct |
2 ms |
348 KB |
correct |
30 |
Correct |
2 ms |
348 KB |
correct |
31 |
Correct |
3 ms |
348 KB |
correct |
32 |
Correct |
2 ms |
348 KB |
correct |
33 |
Correct |
3 ms |
344 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
correct |
2 |
Correct |
1 ms |
348 KB |
correct |
3 |
Correct |
1 ms |
348 KB |
correct |
4 |
Correct |
1 ms |
344 KB |
correct |
5 |
Correct |
1 ms |
344 KB |
correct |
6 |
Correct |
0 ms |
344 KB |
correct |
7 |
Correct |
1 ms |
344 KB |
correct |
8 |
Correct |
0 ms |
344 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
1 ms |
348 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
0 ms |
348 KB |
correct |
13 |
Correct |
1 ms |
348 KB |
correct |
14 |
Correct |
4 ms |
604 KB |
correct |
15 |
Correct |
3 ms |
604 KB |
correct |
16 |
Correct |
4 ms |
472 KB |
correct |
17 |
Correct |
3 ms |
604 KB |
correct |
18 |
Correct |
2 ms |
348 KB |
correct |
19 |
Correct |
3 ms |
604 KB |
correct |
20 |
Correct |
5 ms |
604 KB |
correct |
21 |
Correct |
3 ms |
344 KB |
correct |
22 |
Correct |
3 ms |
348 KB |
correct |
23 |
Correct |
4 ms |
564 KB |
correct |
24 |
Correct |
4 ms |
348 KB |
correct |
25 |
Correct |
2 ms |
348 KB |
correct |
26 |
Correct |
2 ms |
348 KB |
correct |
27 |
Correct |
3 ms |
600 KB |
correct |
28 |
Correct |
2 ms |
348 KB |
correct |
29 |
Correct |
2 ms |
348 KB |
correct |
30 |
Correct |
2 ms |
348 KB |
correct |
31 |
Correct |
3 ms |
348 KB |
correct |
32 |
Correct |
2 ms |
348 KB |
correct |
33 |
Correct |
3 ms |
344 KB |
correct |
34 |
Correct |
117 ms |
4212 KB |
correct |
35 |
Correct |
105 ms |
3932 KB |
correct |
36 |
Correct |
92 ms |
2904 KB |
correct |
37 |
Correct |
40 ms |
668 KB |
correct |
38 |
Correct |
110 ms |
4068 KB |
correct |
39 |
Correct |
112 ms |
3648 KB |
correct |
40 |
Correct |
99 ms |
2964 KB |
correct |
41 |
Correct |
99 ms |
4188 KB |
correct |
42 |
Correct |
107 ms |
4192 KB |
correct |
43 |
Correct |
77 ms |
2476 KB |
correct |
44 |
Correct |
71 ms |
2092 KB |
correct |
45 |
Correct |
71 ms |
2140 KB |
correct |
46 |
Correct |
68 ms |
1892 KB |
correct |
47 |
Correct |
51 ms |
1112 KB |
correct |
48 |
Correct |
20 ms |
348 KB |
correct |
49 |
Correct |
34 ms |
600 KB |
correct |
50 |
Correct |
50 ms |
1112 KB |
correct |
51 |
Correct |
87 ms |
2140 KB |
correct |
52 |
Correct |
66 ms |
1880 KB |
correct |
53 |
Correct |
64 ms |
1896 KB |
correct |
54 |
Correct |
85 ms |
2472 KB |
correct |
55 |
Correct |
71 ms |
2396 KB |
correct |
56 |
Correct |
80 ms |
2372 KB |
correct |
57 |
Correct |
74 ms |
2136 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
correct |
2 |
Correct |
1 ms |
348 KB |
correct |
3 |
Correct |
370 ms |
10880 KB |
correct |
4 |
Correct |
634 ms |
16900 KB |
correct |
5 |
Correct |
855 ms |
16920 KB |
correct |
6 |
Correct |
621 ms |
16708 KB |
correct |
7 |
Correct |
591 ms |
16704 KB |
correct |
8 |
Correct |
605 ms |
16912 KB |
correct |
9 |
Correct |
618 ms |
16792 KB |
correct |
10 |
Correct |
636 ms |
16708 KB |
correct |
11 |
Correct |
629 ms |
16496 KB |
correct |
12 |
Correct |
640 ms |
16716 KB |
correct |
13 |
Correct |
1 ms |
348 KB |
correct |
14 |
Correct |
636 ms |
16660 KB |
correct |
15 |
Correct |
629 ms |
16704 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
correct |
2 |
Correct |
1 ms |
348 KB |
correct |
3 |
Correct |
1 ms |
348 KB |
correct |
4 |
Correct |
1 ms |
344 KB |
correct |
5 |
Correct |
1 ms |
344 KB |
correct |
6 |
Correct |
0 ms |
344 KB |
correct |
7 |
Correct |
1 ms |
344 KB |
correct |
8 |
Correct |
0 ms |
344 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
1 ms |
348 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
0 ms |
348 KB |
correct |
13 |
Correct |
1 ms |
348 KB |
correct |
14 |
Correct |
4 ms |
604 KB |
correct |
15 |
Correct |
3 ms |
604 KB |
correct |
16 |
Correct |
4 ms |
472 KB |
correct |
17 |
Correct |
3 ms |
604 KB |
correct |
18 |
Correct |
2 ms |
348 KB |
correct |
19 |
Correct |
3 ms |
604 KB |
correct |
20 |
Correct |
5 ms |
604 KB |
correct |
21 |
Correct |
3 ms |
344 KB |
correct |
22 |
Correct |
3 ms |
348 KB |
correct |
23 |
Correct |
4 ms |
564 KB |
correct |
24 |
Correct |
4 ms |
348 KB |
correct |
25 |
Correct |
2 ms |
348 KB |
correct |
26 |
Correct |
2 ms |
348 KB |
correct |
27 |
Correct |
3 ms |
600 KB |
correct |
28 |
Correct |
2 ms |
348 KB |
correct |
29 |
Correct |
2 ms |
348 KB |
correct |
30 |
Correct |
2 ms |
348 KB |
correct |
31 |
Correct |
3 ms |
348 KB |
correct |
32 |
Correct |
2 ms |
348 KB |
correct |
33 |
Correct |
3 ms |
344 KB |
correct |
34 |
Correct |
117 ms |
4212 KB |
correct |
35 |
Correct |
105 ms |
3932 KB |
correct |
36 |
Correct |
92 ms |
2904 KB |
correct |
37 |
Correct |
40 ms |
668 KB |
correct |
38 |
Correct |
110 ms |
4068 KB |
correct |
39 |
Correct |
112 ms |
3648 KB |
correct |
40 |
Correct |
99 ms |
2964 KB |
correct |
41 |
Correct |
99 ms |
4188 KB |
correct |
42 |
Correct |
107 ms |
4192 KB |
correct |
43 |
Correct |
77 ms |
2476 KB |
correct |
44 |
Correct |
71 ms |
2092 KB |
correct |
45 |
Correct |
71 ms |
2140 KB |
correct |
46 |
Correct |
68 ms |
1892 KB |
correct |
47 |
Correct |
51 ms |
1112 KB |
correct |
48 |
Correct |
20 ms |
348 KB |
correct |
49 |
Correct |
34 ms |
600 KB |
correct |
50 |
Correct |
50 ms |
1112 KB |
correct |
51 |
Correct |
87 ms |
2140 KB |
correct |
52 |
Correct |
66 ms |
1880 KB |
correct |
53 |
Correct |
64 ms |
1896 KB |
correct |
54 |
Correct |
85 ms |
2472 KB |
correct |
55 |
Correct |
71 ms |
2396 KB |
correct |
56 |
Correct |
80 ms |
2372 KB |
correct |
57 |
Correct |
74 ms |
2136 KB |
correct |
58 |
Correct |
0 ms |
348 KB |
correct |
59 |
Correct |
1 ms |
348 KB |
correct |
60 |
Correct |
370 ms |
10880 KB |
correct |
61 |
Correct |
634 ms |
16900 KB |
correct |
62 |
Correct |
855 ms |
16920 KB |
correct |
63 |
Correct |
621 ms |
16708 KB |
correct |
64 |
Correct |
591 ms |
16704 KB |
correct |
65 |
Correct |
605 ms |
16912 KB |
correct |
66 |
Correct |
618 ms |
16792 KB |
correct |
67 |
Correct |
636 ms |
16708 KB |
correct |
68 |
Correct |
629 ms |
16496 KB |
correct |
69 |
Correct |
640 ms |
16716 KB |
correct |
70 |
Correct |
1 ms |
348 KB |
correct |
71 |
Correct |
636 ms |
16660 KB |
correct |
72 |
Correct |
629 ms |
16704 KB |
correct |
73 |
Correct |
0 ms |
348 KB |
correct |
74 |
Correct |
631 ms |
16708 KB |
correct |
75 |
Correct |
605 ms |
15956 KB |
correct |
76 |
Correct |
308 ms |
6228 KB |
correct |
77 |
Correct |
615 ms |
16468 KB |
correct |
78 |
Correct |
622 ms |
16468 KB |
correct |
79 |
Correct |
685 ms |
16604 KB |
correct |
80 |
Correct |
639 ms |
16212 KB |
correct |
81 |
Correct |
545 ms |
13816 KB |
correct |
82 |
Correct |
616 ms |
16120 KB |
correct |
83 |
Correct |
449 ms |
8576 KB |
correct |
84 |
Correct |
412 ms |
9928 KB |
correct |
85 |
Correct |
392 ms |
8816 KB |
correct |
86 |
Correct |
352 ms |
6352 KB |
correct |
87 |
Correct |
325 ms |
4596 KB |
correct |
88 |
Correct |
284 ms |
3676 KB |
correct |
89 |
Correct |
269 ms |
3584 KB |
correct |
90 |
Correct |
255 ms |
3032 KB |
correct |
91 |
Correct |
142 ms |
860 KB |
correct |
92 |
Correct |
99 ms |
600 KB |
correct |
93 |
Correct |
394 ms |
8968 KB |
correct |
94 |
Correct |
339 ms |
6368 KB |
correct |
95 |
Correct |
343 ms |
6012 KB |
correct |
96 |
Correct |
264 ms |
3164 KB |
correct |
97 |
Correct |
280 ms |
3760 KB |
correct |
98 |
Correct |
306 ms |
4440 KB |
correct |
99 |
Correct |
270 ms |
3672 KB |
correct |
100 |
Correct |
188 ms |
1116 KB |
correct |
101 |
Correct |
100 ms |
692 KB |
correct |
102 |
Correct |
391 ms |
8680 KB |
correct |
103 |
Correct |
406 ms |
8684 KB |
correct |
104 |
Correct |
386 ms |
8528 KB |
correct |
105 |
Correct |
399 ms |
8624 KB |
correct |