#include "simurgh.h"
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>
#include <cassert>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef pair<int, int> pii;
const int N = 510;
const int M = N * N;
int n, m, uu[M], vv[M];
vector<pii> g[N];
bool isb[M];
vector<pii> mst;
int dep[N], mind[N]; // memset(dep)
int dfstree(int u, int p, int pi) {
mind[u] = dep[u];
for(pii e : g[u]) {
int v = e.X, i = e.Y;
if(v != p) {
if(dep[v] == -1) {
dep[v] = dep[u] + 1;
mind[u] = min(mind[u], dfstree(v, u, i));
} else if( dep[v] < dep[u]) {
mind[u] = min(mind[u], dep[v]);
}
}
}
if(pi != -1 && mind[u] >= dep[u]) {
mst.PB({pi, 1});
isb[pi] = 1;
}
return mind[u];
}
void bridge() {
memset(dep, -1, sizeof(dep));
dep[0] = 0;
dfstree(0, -1, -1);
}
int un[N], siz[N];
void clear() {
memset(un, -1, sizeof(un));
for(int i = 0; i < n; ++i) {
siz[i] = 1;
}
}
int trazi(int u) { return un[u] == -1 ? u : un[u] = trazi(un[u]); }
bool unija(int u, int v) {
u = trazi(u);
v = trazi(v);
if(u == v) { return 0; }
if(siz[u] < siz[v]) { swap(u, v); }
un[v] = u;
siz[u] += siz[v];
return 1;
}
vector<int> qry;
int fillup(vector<int> &e, bool f) {
clear();
qry.clear();
int ret = 0;
for(int i : e) {
unija(uu[i], vv[i]);
qry.PB(i);
}
if(f == 0) {
for(int i = 0; i < m; ++i) {
if(unija(uu[i], vv[i])) {
qry.PB(i);
}
}
} else {
for(pii i : mst) {
if(unija(uu[i.X], vv[i.X])) {
qry.PB(i.X);
ret += i.Y;
}
}
}
return ret;
}
int bio[N], bioe[M], val[M];
vector<int> tmp, cp, pth;
void comp(int u) {
if(bio[u] != -1) { return; }
bio[u] = 0;
cp.PB(u);
for(pii e : g[u]) {
if(!isb[e.Y]) {
comp(e.X);
}
}
}
bool tried[N];
int spec[2], sp;
bool dfs(int u) {
bio[u] = 1;
tried[u] = 1;
for(pii e : g[u]) {
int v = e.X, i = e.Y;
if(!isb[i] && !bioe[i] && bio[v] != 1 && !tried[v]) {
tmp.PB(i);
bioe[i] = 1;
if(bio[v] == 2) {
spec[sp] = v;
return 1;
} else {
if(dfs(v)) {
return 1;
}
bioe[i] = 0;
tmp.pop_back();
}
}
}
bio[u] = 0;
return 0;
}
bool path(int u, int t, int pr = -1) {
if(u == t) { return 1; }
bool ret = 0;
for(pii p : g[u]) {
int v = p.X, ind = p.Y;
if(v != pr && !isb[ind] && bioe[ind] == 1 && bio[v] == 2) {
if(path(v, t, u)) {
pth.PB(ind);
return 1;
}
}
}
return 0;
}
void cycle(vector<int> &e) {
pth.clear();
path(spec[0], spec[1]);
int f = -1;
for(int i = 0; i < pth.size(); ++i) {
if(val[pth[i]] == 0) {
f = i;
}
}
int xam = -1;
if(f != -1) {
vector<int> fu;
for(int i = 0; i < pth.size(); ++i) {
if(i != f) {
fu.PB(pth[i]);
}
}
for(int i : e) {
fu.PB(i);
}
fillup(fu, 0);
xam = count_common_roads(qry);
}
vector<pii> res;
for(int i = 0; i < e.size(); ++i) {
vector<int> fu;
for(int j : pth) {
fu.PB(j);
}
for(int j = 0; j < e.size(); ++j) {
if(j != i) {
fu.PB(e[j]);
}
}
fillup(fu, 0);
res.PB({e[i], count_common_roads(qry)});
xam = max(xam, res.back().Y);
}
for(int i = 0; i < (int) res.size() - 1; ++i) {
mst.PB({res[i].X, xam - res[i].Y});
val[res[i].X] = xam - res[i].Y;
}
bioe[res.back().X] = 0;
}
void ear() {
memset(bio, -1, sizeof(bio));
for(int i = 0; i < n; ++i) {
if(bio[i] == -1) {
cp.clear();
comp(i);
bio[i] = 2;
for(int j : cp) {
if(bio[j] != 2) {
tmp.clear();
memset(tried, 0, sizeof(tried));
sp = 0; dfs(j);
memset(tried, 0, sizeof(tried));
sp = 1; dfs(j);
cycle(tmp);
for(int k : tmp) {
bio[uu[k]] = 2;
bio[vv[k]] = 2;
}
}
}
}
}
}
bool ans[M];
int deg[N], cnt = 0;
int query(vector<int> &q) {
int d = fillup(q, 1);
return count_common_roads(qry) - d;
}
int leaf(int u) {
vector<int> ng;
for(pii e : g[u]) {
if(!ans[e.Y]) {
ng.PB(e.Y);
}
}
for(; ng.size() > 1; ) {
vector<int> q;
int k = (ng.size() + 1) / 2;
for(int i = 0; i < k; ++i) {
q.PB(ng[i]);
}
if(query(q)) {
ng.erase(ng.begin() + k, ng.end());
} else {
ng.erase(ng.begin(), ng.begin() + k);
}
}
return ng[0];
}
vector<int> find_roads(int nn, vector<int> A, vector<int> B) {
n = nn;
m = A.size();
for(int i = 0; i < m; ++i) {
uu[i] = A[i];
vv[i] = B[i];
g[uu[i]].PB({vv[i], i});
g[vv[i]].PB({uu[i], i});
}
bridge();
ear();
for(int i = 0; i < n; ++i) {
vector<int> q;
for(pii i : g[i]) {
q.PB(i.Y);
}
deg[i] = query(q);
}
for(; cnt < n - 1; ) {
for(int i = 0; i < n; ++i) {
if(deg[i] == 1) {
++cnt;
int ind = leaf(i);
ans[ind] = 1;
--deg[uu[ind]];
--deg[vv[ind]];
}
}
}
vector<int> ret;
for(int i = 0; i < m; ++i) {
if(ans[i]) {
ret.PB(i);
}
}
return ret;
}
Compilation message
simurgh.cpp: In function 'bool path(int, int, int)':
simurgh.cpp:151:7: warning: unused variable 'ret' [-Wunused-variable]
151 | bool ret = 0;
| ^~~
simurgh.cpp: In function 'void cycle(std::vector<int>&)':
simurgh.cpp:168:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
168 | for(int i = 0; i < pth.size(); ++i) {
| ~~^~~~~~~~~~~~
simurgh.cpp:177:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
177 | for(int i = 0; i < pth.size(); ++i) {
| ~~^~~~~~~~~~~~
simurgh.cpp:191:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
191 | for(int i = 0; i < e.size(); ++i) {
| ~~^~~~~~~~~~
simurgh.cpp:196:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
196 | for(int j = 0; j < e.size(); ++j) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
correct |
2 |
Correct |
0 ms |
2396 KB |
correct |
3 |
Correct |
0 ms |
2396 KB |
correct |
4 |
Correct |
0 ms |
2396 KB |
correct |
5 |
Correct |
0 ms |
2396 KB |
correct |
6 |
Correct |
0 ms |
2396 KB |
correct |
7 |
Correct |
1 ms |
2396 KB |
correct |
8 |
Correct |
1 ms |
2396 KB |
correct |
9 |
Correct |
0 ms |
2396 KB |
correct |
10 |
Correct |
0 ms |
2396 KB |
correct |
11 |
Correct |
0 ms |
2396 KB |
correct |
12 |
Correct |
0 ms |
2396 KB |
correct |
13 |
Correct |
1 ms |
2392 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
correct |
2 |
Correct |
0 ms |
2396 KB |
correct |
3 |
Correct |
0 ms |
2396 KB |
correct |
4 |
Correct |
0 ms |
2396 KB |
correct |
5 |
Correct |
0 ms |
2396 KB |
correct |
6 |
Correct |
0 ms |
2396 KB |
correct |
7 |
Correct |
1 ms |
2396 KB |
correct |
8 |
Correct |
1 ms |
2396 KB |
correct |
9 |
Correct |
0 ms |
2396 KB |
correct |
10 |
Correct |
0 ms |
2396 KB |
correct |
11 |
Correct |
0 ms |
2396 KB |
correct |
12 |
Correct |
0 ms |
2396 KB |
correct |
13 |
Correct |
1 ms |
2392 KB |
correct |
14 |
Correct |
2 ms |
2396 KB |
correct |
15 |
Correct |
2 ms |
2652 KB |
correct |
16 |
Correct |
2 ms |
2552 KB |
correct |
17 |
Correct |
2 ms |
2396 KB |
correct |
18 |
Correct |
1 ms |
2396 KB |
correct |
19 |
Correct |
1 ms |
2492 KB |
correct |
20 |
Correct |
2 ms |
2396 KB |
correct |
21 |
Correct |
2 ms |
2396 KB |
correct |
22 |
Correct |
1 ms |
2396 KB |
correct |
23 |
Correct |
1 ms |
2396 KB |
correct |
24 |
Correct |
2 ms |
2396 KB |
correct |
25 |
Correct |
1 ms |
2452 KB |
correct |
26 |
Correct |
1 ms |
2396 KB |
correct |
27 |
Correct |
1 ms |
2396 KB |
correct |
28 |
Correct |
1 ms |
2396 KB |
correct |
29 |
Correct |
1 ms |
2396 KB |
correct |
30 |
Correct |
1 ms |
2396 KB |
correct |
31 |
Correct |
2 ms |
2528 KB |
correct |
32 |
Correct |
2 ms |
2484 KB |
correct |
33 |
Correct |
1 ms |
2396 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
correct |
2 |
Correct |
0 ms |
2396 KB |
correct |
3 |
Correct |
0 ms |
2396 KB |
correct |
4 |
Correct |
0 ms |
2396 KB |
correct |
5 |
Correct |
0 ms |
2396 KB |
correct |
6 |
Correct |
0 ms |
2396 KB |
correct |
7 |
Correct |
1 ms |
2396 KB |
correct |
8 |
Correct |
1 ms |
2396 KB |
correct |
9 |
Correct |
0 ms |
2396 KB |
correct |
10 |
Correct |
0 ms |
2396 KB |
correct |
11 |
Correct |
0 ms |
2396 KB |
correct |
12 |
Correct |
0 ms |
2396 KB |
correct |
13 |
Correct |
1 ms |
2392 KB |
correct |
14 |
Correct |
2 ms |
2396 KB |
correct |
15 |
Correct |
2 ms |
2652 KB |
correct |
16 |
Correct |
2 ms |
2552 KB |
correct |
17 |
Correct |
2 ms |
2396 KB |
correct |
18 |
Correct |
1 ms |
2396 KB |
correct |
19 |
Correct |
1 ms |
2492 KB |
correct |
20 |
Correct |
2 ms |
2396 KB |
correct |
21 |
Correct |
2 ms |
2396 KB |
correct |
22 |
Correct |
1 ms |
2396 KB |
correct |
23 |
Correct |
1 ms |
2396 KB |
correct |
24 |
Correct |
2 ms |
2396 KB |
correct |
25 |
Correct |
1 ms |
2452 KB |
correct |
26 |
Correct |
1 ms |
2396 KB |
correct |
27 |
Correct |
1 ms |
2396 KB |
correct |
28 |
Correct |
1 ms |
2396 KB |
correct |
29 |
Correct |
1 ms |
2396 KB |
correct |
30 |
Correct |
1 ms |
2396 KB |
correct |
31 |
Correct |
2 ms |
2528 KB |
correct |
32 |
Correct |
2 ms |
2484 KB |
correct |
33 |
Correct |
1 ms |
2396 KB |
correct |
34 |
Correct |
77 ms |
3852 KB |
correct |
35 |
Correct |
62 ms |
3792 KB |
correct |
36 |
Correct |
59 ms |
3420 KB |
correct |
37 |
Correct |
10 ms |
2392 KB |
correct |
38 |
Correct |
65 ms |
3836 KB |
correct |
39 |
Correct |
81 ms |
3672 KB |
correct |
40 |
Correct |
47 ms |
3420 KB |
correct |
41 |
Correct |
68 ms |
3832 KB |
correct |
42 |
Correct |
63 ms |
3676 KB |
correct |
43 |
Correct |
46 ms |
3392 KB |
correct |
44 |
Correct |
34 ms |
2904 KB |
correct |
45 |
Correct |
38 ms |
3160 KB |
correct |
46 |
Correct |
30 ms |
2908 KB |
correct |
47 |
Correct |
18 ms |
2648 KB |
correct |
48 |
Correct |
4 ms |
2396 KB |
correct |
49 |
Correct |
9 ms |
2600 KB |
correct |
50 |
Correct |
18 ms |
2652 KB |
correct |
51 |
Correct |
44 ms |
3164 KB |
correct |
52 |
Correct |
31 ms |
3108 KB |
correct |
53 |
Correct |
34 ms |
3056 KB |
correct |
54 |
Correct |
50 ms |
3388 KB |
correct |
55 |
Correct |
35 ms |
3184 KB |
correct |
56 |
Correct |
32 ms |
3188 KB |
correct |
57 |
Correct |
44 ms |
3200 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
correct |
2 |
Correct |
0 ms |
2396 KB |
correct |
3 |
Correct |
281 ms |
6496 KB |
correct |
4 |
Correct |
597 ms |
8192 KB |
correct |
5 |
Correct |
583 ms |
8252 KB |
correct |
6 |
Correct |
518 ms |
8172 KB |
correct |
7 |
Correct |
652 ms |
8208 KB |
correct |
8 |
Correct |
566 ms |
8192 KB |
correct |
9 |
Correct |
660 ms |
8180 KB |
correct |
10 |
Correct |
629 ms |
8204 KB |
correct |
11 |
Correct |
588 ms |
8212 KB |
correct |
12 |
Correct |
653 ms |
8168 KB |
correct |
13 |
Correct |
1 ms |
2396 KB |
correct |
14 |
Correct |
432 ms |
8176 KB |
correct |
15 |
Correct |
434 ms |
8196 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
correct |
2 |
Correct |
0 ms |
2396 KB |
correct |
3 |
Correct |
0 ms |
2396 KB |
correct |
4 |
Correct |
0 ms |
2396 KB |
correct |
5 |
Correct |
0 ms |
2396 KB |
correct |
6 |
Correct |
0 ms |
2396 KB |
correct |
7 |
Correct |
1 ms |
2396 KB |
correct |
8 |
Correct |
1 ms |
2396 KB |
correct |
9 |
Correct |
0 ms |
2396 KB |
correct |
10 |
Correct |
0 ms |
2396 KB |
correct |
11 |
Correct |
0 ms |
2396 KB |
correct |
12 |
Correct |
0 ms |
2396 KB |
correct |
13 |
Correct |
1 ms |
2392 KB |
correct |
14 |
Correct |
2 ms |
2396 KB |
correct |
15 |
Correct |
2 ms |
2652 KB |
correct |
16 |
Correct |
2 ms |
2552 KB |
correct |
17 |
Correct |
2 ms |
2396 KB |
correct |
18 |
Correct |
1 ms |
2396 KB |
correct |
19 |
Correct |
1 ms |
2492 KB |
correct |
20 |
Correct |
2 ms |
2396 KB |
correct |
21 |
Correct |
2 ms |
2396 KB |
correct |
22 |
Correct |
1 ms |
2396 KB |
correct |
23 |
Correct |
1 ms |
2396 KB |
correct |
24 |
Correct |
2 ms |
2396 KB |
correct |
25 |
Correct |
1 ms |
2452 KB |
correct |
26 |
Correct |
1 ms |
2396 KB |
correct |
27 |
Correct |
1 ms |
2396 KB |
correct |
28 |
Correct |
1 ms |
2396 KB |
correct |
29 |
Correct |
1 ms |
2396 KB |
correct |
30 |
Correct |
1 ms |
2396 KB |
correct |
31 |
Correct |
2 ms |
2528 KB |
correct |
32 |
Correct |
2 ms |
2484 KB |
correct |
33 |
Correct |
1 ms |
2396 KB |
correct |
34 |
Correct |
77 ms |
3852 KB |
correct |
35 |
Correct |
62 ms |
3792 KB |
correct |
36 |
Correct |
59 ms |
3420 KB |
correct |
37 |
Correct |
10 ms |
2392 KB |
correct |
38 |
Correct |
65 ms |
3836 KB |
correct |
39 |
Correct |
81 ms |
3672 KB |
correct |
40 |
Correct |
47 ms |
3420 KB |
correct |
41 |
Correct |
68 ms |
3832 KB |
correct |
42 |
Correct |
63 ms |
3676 KB |
correct |
43 |
Correct |
46 ms |
3392 KB |
correct |
44 |
Correct |
34 ms |
2904 KB |
correct |
45 |
Correct |
38 ms |
3160 KB |
correct |
46 |
Correct |
30 ms |
2908 KB |
correct |
47 |
Correct |
18 ms |
2648 KB |
correct |
48 |
Correct |
4 ms |
2396 KB |
correct |
49 |
Correct |
9 ms |
2600 KB |
correct |
50 |
Correct |
18 ms |
2652 KB |
correct |
51 |
Correct |
44 ms |
3164 KB |
correct |
52 |
Correct |
31 ms |
3108 KB |
correct |
53 |
Correct |
34 ms |
3056 KB |
correct |
54 |
Correct |
50 ms |
3388 KB |
correct |
55 |
Correct |
35 ms |
3184 KB |
correct |
56 |
Correct |
32 ms |
3188 KB |
correct |
57 |
Correct |
44 ms |
3200 KB |
correct |
58 |
Correct |
1 ms |
2392 KB |
correct |
59 |
Correct |
0 ms |
2396 KB |
correct |
60 |
Correct |
281 ms |
6496 KB |
correct |
61 |
Correct |
597 ms |
8192 KB |
correct |
62 |
Correct |
583 ms |
8252 KB |
correct |
63 |
Correct |
518 ms |
8172 KB |
correct |
64 |
Correct |
652 ms |
8208 KB |
correct |
65 |
Correct |
566 ms |
8192 KB |
correct |
66 |
Correct |
660 ms |
8180 KB |
correct |
67 |
Correct |
629 ms |
8204 KB |
correct |
68 |
Correct |
588 ms |
8212 KB |
correct |
69 |
Correct |
653 ms |
8168 KB |
correct |
70 |
Correct |
1 ms |
2396 KB |
correct |
71 |
Correct |
432 ms |
8176 KB |
correct |
72 |
Correct |
434 ms |
8196 KB |
correct |
73 |
Correct |
1 ms |
2396 KB |
correct |
74 |
Correct |
543 ms |
8428 KB |
correct |
75 |
Correct |
544 ms |
8076 KB |
correct |
76 |
Correct |
170 ms |
4692 KB |
correct |
77 |
Correct |
433 ms |
8196 KB |
correct |
78 |
Correct |
592 ms |
8384 KB |
correct |
79 |
Correct |
487 ms |
8276 KB |
correct |
80 |
Correct |
526 ms |
8280 KB |
correct |
81 |
Correct |
352 ms |
7776 KB |
correct |
82 |
Correct |
511 ms |
8056 KB |
correct |
83 |
Correct |
276 ms |
5716 KB |
correct |
84 |
Correct |
299 ms |
6344 KB |
correct |
85 |
Correct |
313 ms |
5976 KB |
correct |
86 |
Correct |
217 ms |
4696 KB |
correct |
87 |
Correct |
182 ms |
4652 KB |
correct |
88 |
Correct |
148 ms |
3772 KB |
correct |
89 |
Correct |
132 ms |
3724 KB |
correct |
90 |
Correct |
118 ms |
3528 KB |
correct |
91 |
Correct |
37 ms |
2652 KB |
correct |
92 |
Correct |
17 ms |
2568 KB |
correct |
93 |
Correct |
343 ms |
6132 KB |
correct |
94 |
Correct |
229 ms |
4792 KB |
correct |
95 |
Correct |
225 ms |
4860 KB |
correct |
96 |
Correct |
126 ms |
3676 KB |
correct |
97 |
Correct |
143 ms |
3788 KB |
correct |
98 |
Correct |
181 ms |
4388 KB |
correct |
99 |
Correct |
136 ms |
3792 KB |
correct |
100 |
Correct |
57 ms |
2648 KB |
correct |
101 |
Correct |
19 ms |
2396 KB |
correct |
102 |
Correct |
251 ms |
5400 KB |
correct |
103 |
Correct |
244 ms |
5408 KB |
correct |
104 |
Correct |
225 ms |
5380 KB |
correct |
105 |
Correct |
229 ms |
5208 KB |
correct |