#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
const int Nmax=510, Kmax=Nmax*Nmax/2;
int N, M, p, u[Kmax], v[Kmax], par[Nmax], parc[Nmax], dep[Nmax], dfsn[Nmax], top[Nmax], low[Nmax], back[Nmax];
int total, chk[Kmax];
vector<pair<int, int>> adj[Nmax], adj2[Nmax];
vector<tuple<int, int, int>> tree;
vector<int> V;
int value(int x, int y) {
for(int &i:V) if(i==x) i=y;
int ret=count_common_roads(V);
for(int &i:V) if(i==y) i=x;
return ret;
}
int value2(vector<int> X) {
for(int i:X) {
if(dep[u[i]]<dep[v[i]]) swap(u[i], v[i]);
for(int &j:V) if(j==parc[u[i]]) j=i;
}
int ret=count_common_roads(V)-total;
for(int i:X) {
for(int &j:V) if(j==i) j=parc[u[i]], ret+=chk[parc[u[i]]];
}
return ret;
}
void DFS(int curr, int prev) {
dfsn[curr]=top[curr]=++p;
for(auto [next, c]:adj[curr]) if(next!=prev) {
if(dfsn[next]) {
if(top[curr]>dfsn[next]) top[curr]=dfsn[next], back[curr]=c, low[curr]=curr;
}
else {
par[next]=curr, parc[next]=c, dep[next]=dep[curr]+1;
tree.push_back({curr, next, c});
adj2[curr].push_back({next, c});
DFS(next, curr);
if(top[curr]>top[next]) top[curr]=top[next], back[curr]=back[next], low[curr]=low[next];
}
}
}
void DFS2(int curr) {
for(auto [next, c]:adj2[curr]) {
DFS2(next);
if(chk[c]!=-1) continue;
if(top[next]>dfsn[curr]) chk[c]=1;
else {
vector<int> tmp;
bool flag=false; int q=0;
for(int i=low[next]; dfsn[i]!=top[next]; i=par[i]) {
tmp.push_back(parc[i]);
if(chk[parc[i]]!=-1) flag=true, q=parc[i];
}
if(flag) {
chk[back[next]]=chk[q]+value(q, back[next])-total;
for(int i:tmp) if(chk[i]==-1) chk[i]=chk[back[next]]-value(i, back[next])+total;
}
else {
int X=total;
vector<int> Y;
for(int i:tmp) Y.push_back(value(i, back[next]));
bool flag2=false;
for(int i=0; i<Y.size(); i++) {
if(Y[i]==X+1) flag2=true, chk[back[next]]=1, chk[tmp[i]]=0;
else if(Y[i]==X-1) flag2=true, chk[back[next]]=0, chk[tmp[i]]=1;
}
if(flag2) {
for(int i=0; i<Y.size(); i++) if(Y[i]==X) chk[tmp[i]]=chk[back[next]];
}
else {
chk[back[next]]=false;
for(int i=0; i<Y.size(); i++) if(Y[i]==X) chk[tmp[i]]=false;
}
}
}
}
}
void F(int l, int r, int sum, vector<int> E) {
if(l==r) {
if(sum) chk[E[l]]=1;
else chk[E[r]]=0;
return;
}
if(l>r) return;
int m=(l+r)/2;
vector<int> tmp;
for(int i=l; i<=m; i++) tmp.push_back(E[i]);
int lsum=value2(tmp);
if(lsum) F(l, m, lsum, E);
if(sum-lsum) F(m+1, r, sum-lsum, E);
}
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
N=n, M=u.size();
for(int i=0; i<M; i++) {
::u[i]=u[i], ::v[i]=v[i];
adj[u[i]].push_back({v[i], i}), adj[v[i]].push_back({u[i], i});
}
for(int i=0; i<M; i++) chk[i]=-1;
DFS(0, -1);
for(auto [u, v, c]:tree) V.push_back(c);
total=count_common_roads(V);
DFS2(0);
for(int i=0; i<N; i++) {
vector<int> E;
for(auto [j, c]:adj[i]) if(dfsn[j]>dfsn[i] && chk[c]==-1) E.push_back(c);
if(!E.empty()) F(0, E.size()-1, value2(E), E);
}
vector<int> ret;
for(int i=0; i<M; i++) if(chk[i]==1) ret.push_back(i);
return ret;
}
Compilation message
simurgh.cpp: In function 'void DFS2(int)':
simurgh.cpp:68:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int i=0; i<Y.size(); i++) {
| ~^~~~~~~~~
simurgh.cpp:73:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i=0; i<Y.size(); i++) if(Y[i]==X) chk[tmp[i]]=chk[back[next]];
| ~^~~~~~~~~
simurgh.cpp:77:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int i=0; i<Y.size(); i++) if(Y[i]==X) chk[tmp[i]]=false;
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
correct |
2 |
Correct |
0 ms |
348 KB |
correct |
3 |
Correct |
0 ms |
348 KB |
correct |
4 |
Correct |
0 ms |
348 KB |
correct |
5 |
Correct |
0 ms |
348 KB |
correct |
6 |
Correct |
0 ms |
348 KB |
correct |
7 |
Correct |
0 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
344 KB |
correct |
9 |
Correct |
1 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 |
0 ms |
348 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
correct |
2 |
Correct |
0 ms |
348 KB |
correct |
3 |
Correct |
0 ms |
348 KB |
correct |
4 |
Correct |
0 ms |
348 KB |
correct |
5 |
Correct |
0 ms |
348 KB |
correct |
6 |
Correct |
0 ms |
348 KB |
correct |
7 |
Correct |
0 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
344 KB |
correct |
9 |
Correct |
1 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 |
0 ms |
348 KB |
correct |
14 |
Correct |
1 ms |
348 KB |
correct |
15 |
Correct |
1 ms |
344 KB |
correct |
16 |
Correct |
1 ms |
348 KB |
correct |
17 |
Correct |
1 ms |
348 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
1 ms |
348 KB |
correct |
20 |
Correct |
1 ms |
348 KB |
correct |
21 |
Correct |
1 ms |
348 KB |
correct |
22 |
Correct |
1 ms |
348 KB |
correct |
23 |
Correct |
1 ms |
348 KB |
correct |
24 |
Correct |
1 ms |
348 KB |
correct |
25 |
Correct |
0 ms |
348 KB |
correct |
26 |
Correct |
1 ms |
348 KB |
correct |
27 |
Correct |
1 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
344 KB |
correct |
29 |
Correct |
1 ms |
348 KB |
correct |
30 |
Correct |
1 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
344 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
348 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
correct |
2 |
Correct |
0 ms |
348 KB |
correct |
3 |
Correct |
0 ms |
348 KB |
correct |
4 |
Correct |
0 ms |
348 KB |
correct |
5 |
Correct |
0 ms |
348 KB |
correct |
6 |
Correct |
0 ms |
348 KB |
correct |
7 |
Correct |
0 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
344 KB |
correct |
9 |
Correct |
1 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 |
0 ms |
348 KB |
correct |
14 |
Correct |
1 ms |
348 KB |
correct |
15 |
Correct |
1 ms |
344 KB |
correct |
16 |
Correct |
1 ms |
348 KB |
correct |
17 |
Correct |
1 ms |
348 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
1 ms |
348 KB |
correct |
20 |
Correct |
1 ms |
348 KB |
correct |
21 |
Correct |
1 ms |
348 KB |
correct |
22 |
Correct |
1 ms |
348 KB |
correct |
23 |
Correct |
1 ms |
348 KB |
correct |
24 |
Correct |
1 ms |
348 KB |
correct |
25 |
Correct |
0 ms |
348 KB |
correct |
26 |
Correct |
1 ms |
348 KB |
correct |
27 |
Correct |
1 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
344 KB |
correct |
29 |
Correct |
1 ms |
348 KB |
correct |
30 |
Correct |
1 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
344 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
348 KB |
correct |
34 |
Correct |
36 ms |
2136 KB |
correct |
35 |
Correct |
38 ms |
1984 KB |
correct |
36 |
Correct |
26 ms |
1628 KB |
correct |
37 |
Correct |
4 ms |
604 KB |
correct |
38 |
Correct |
36 ms |
2024 KB |
correct |
39 |
Correct |
30 ms |
1628 KB |
correct |
40 |
Correct |
24 ms |
1628 KB |
correct |
41 |
Correct |
35 ms |
1884 KB |
correct |
42 |
Correct |
32 ms |
1996 KB |
correct |
43 |
Correct |
17 ms |
1428 KB |
correct |
44 |
Correct |
14 ms |
1116 KB |
correct |
45 |
Correct |
20 ms |
1112 KB |
correct |
46 |
Correct |
13 ms |
1092 KB |
correct |
47 |
Correct |
7 ms |
604 KB |
correct |
48 |
Correct |
1 ms |
348 KB |
correct |
49 |
Correct |
3 ms |
604 KB |
correct |
50 |
Correct |
7 ms |
788 KB |
correct |
51 |
Correct |
16 ms |
1116 KB |
correct |
52 |
Correct |
14 ms |
1112 KB |
correct |
53 |
Correct |
13 ms |
860 KB |
correct |
54 |
Correct |
18 ms |
1372 KB |
correct |
55 |
Correct |
16 ms |
1116 KB |
correct |
56 |
Correct |
16 ms |
1232 KB |
correct |
57 |
Correct |
16 ms |
1116 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
correct |
2 |
Correct |
0 ms |
348 KB |
correct |
3 |
Correct |
140 ms |
4444 KB |
correct |
4 |
Correct |
266 ms |
7468 KB |
correct |
5 |
Correct |
281 ms |
6996 KB |
correct |
6 |
Correct |
199 ms |
6992 KB |
correct |
7 |
Correct |
236 ms |
7136 KB |
correct |
8 |
Correct |
246 ms |
7188 KB |
correct |
9 |
Correct |
264 ms |
7472 KB |
correct |
10 |
Correct |
284 ms |
7236 KB |
correct |
11 |
Correct |
261 ms |
7244 KB |
correct |
12 |
Correct |
261 ms |
7228 KB |
correct |
13 |
Correct |
0 ms |
344 KB |
correct |
14 |
Correct |
234 ms |
7004 KB |
correct |
15 |
Correct |
290 ms |
7228 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
correct |
2 |
Correct |
0 ms |
348 KB |
correct |
3 |
Correct |
0 ms |
348 KB |
correct |
4 |
Correct |
0 ms |
348 KB |
correct |
5 |
Correct |
0 ms |
348 KB |
correct |
6 |
Correct |
0 ms |
348 KB |
correct |
7 |
Correct |
0 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
344 KB |
correct |
9 |
Correct |
1 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 |
0 ms |
348 KB |
correct |
14 |
Correct |
1 ms |
348 KB |
correct |
15 |
Correct |
1 ms |
344 KB |
correct |
16 |
Correct |
1 ms |
348 KB |
correct |
17 |
Correct |
1 ms |
348 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
1 ms |
348 KB |
correct |
20 |
Correct |
1 ms |
348 KB |
correct |
21 |
Correct |
1 ms |
348 KB |
correct |
22 |
Correct |
1 ms |
348 KB |
correct |
23 |
Correct |
1 ms |
348 KB |
correct |
24 |
Correct |
1 ms |
348 KB |
correct |
25 |
Correct |
0 ms |
348 KB |
correct |
26 |
Correct |
1 ms |
348 KB |
correct |
27 |
Correct |
1 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
344 KB |
correct |
29 |
Correct |
1 ms |
348 KB |
correct |
30 |
Correct |
1 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
344 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
348 KB |
correct |
34 |
Correct |
36 ms |
2136 KB |
correct |
35 |
Correct |
38 ms |
1984 KB |
correct |
36 |
Correct |
26 ms |
1628 KB |
correct |
37 |
Correct |
4 ms |
604 KB |
correct |
38 |
Correct |
36 ms |
2024 KB |
correct |
39 |
Correct |
30 ms |
1628 KB |
correct |
40 |
Correct |
24 ms |
1628 KB |
correct |
41 |
Correct |
35 ms |
1884 KB |
correct |
42 |
Correct |
32 ms |
1996 KB |
correct |
43 |
Correct |
17 ms |
1428 KB |
correct |
44 |
Correct |
14 ms |
1116 KB |
correct |
45 |
Correct |
20 ms |
1112 KB |
correct |
46 |
Correct |
13 ms |
1092 KB |
correct |
47 |
Correct |
7 ms |
604 KB |
correct |
48 |
Correct |
1 ms |
348 KB |
correct |
49 |
Correct |
3 ms |
604 KB |
correct |
50 |
Correct |
7 ms |
788 KB |
correct |
51 |
Correct |
16 ms |
1116 KB |
correct |
52 |
Correct |
14 ms |
1112 KB |
correct |
53 |
Correct |
13 ms |
860 KB |
correct |
54 |
Correct |
18 ms |
1372 KB |
correct |
55 |
Correct |
16 ms |
1116 KB |
correct |
56 |
Correct |
16 ms |
1232 KB |
correct |
57 |
Correct |
16 ms |
1116 KB |
correct |
58 |
Correct |
0 ms |
348 KB |
correct |
59 |
Correct |
0 ms |
348 KB |
correct |
60 |
Correct |
140 ms |
4444 KB |
correct |
61 |
Correct |
266 ms |
7468 KB |
correct |
62 |
Correct |
281 ms |
6996 KB |
correct |
63 |
Correct |
199 ms |
6992 KB |
correct |
64 |
Correct |
236 ms |
7136 KB |
correct |
65 |
Correct |
246 ms |
7188 KB |
correct |
66 |
Correct |
264 ms |
7472 KB |
correct |
67 |
Correct |
284 ms |
7236 KB |
correct |
68 |
Correct |
261 ms |
7244 KB |
correct |
69 |
Correct |
261 ms |
7228 KB |
correct |
70 |
Correct |
0 ms |
344 KB |
correct |
71 |
Correct |
234 ms |
7004 KB |
correct |
72 |
Correct |
290 ms |
7228 KB |
correct |
73 |
Correct |
1 ms |
344 KB |
correct |
74 |
Correct |
267 ms |
7320 KB |
correct |
75 |
Correct |
251 ms |
6992 KB |
correct |
76 |
Correct |
83 ms |
3096 KB |
correct |
77 |
Correct |
283 ms |
7248 KB |
correct |
78 |
Correct |
260 ms |
7212 KB |
correct |
79 |
Correct |
277 ms |
7208 KB |
correct |
80 |
Correct |
254 ms |
7068 KB |
correct |
81 |
Correct |
221 ms |
6412 KB |
correct |
82 |
Correct |
251 ms |
6992 KB |
correct |
83 |
Correct |
137 ms |
4200 KB |
correct |
84 |
Correct |
137 ms |
4956 KB |
correct |
85 |
Correct |
131 ms |
4672 KB |
correct |
86 |
Correct |
82 ms |
3208 KB |
correct |
87 |
Correct |
61 ms |
2648 KB |
correct |
88 |
Correct |
50 ms |
1884 KB |
correct |
89 |
Correct |
47 ms |
1884 KB |
correct |
90 |
Correct |
41 ms |
1628 KB |
correct |
91 |
Correct |
12 ms |
600 KB |
correct |
92 |
Correct |
4 ms |
348 KB |
correct |
93 |
Correct |
115 ms |
4952 KB |
correct |
94 |
Correct |
89 ms |
3444 KB |
correct |
95 |
Correct |
78 ms |
3164 KB |
correct |
96 |
Correct |
43 ms |
1628 KB |
correct |
97 |
Correct |
50 ms |
1884 KB |
correct |
98 |
Correct |
61 ms |
2648 KB |
correct |
99 |
Correct |
53 ms |
1884 KB |
correct |
100 |
Correct |
18 ms |
860 KB |
correct |
101 |
Correct |
5 ms |
604 KB |
correct |
102 |
Correct |
107 ms |
4000 KB |
correct |
103 |
Correct |
124 ms |
4432 KB |
correct |
104 |
Correct |
108 ms |
3924 KB |
correct |
105 |
Correct |
111 ms |
3940 KB |
correct |