#include "simurgh.h"
#include<utility>
#include<queue>
#include<algorithm>
#define x first
#define y second
#define pi pair<int,int>
using namespace std;
vector<vector<pi>>l;
vector<int>mm;
int vs[505], p[505], pl[505], up[505], cs[505], T = 1;
void dfs(int n, int p) {
::p[n] = p;
vs[n] = T++;
for (pi e : l[n]) {
if (e.x == p) {
pl[n] = e.y;
}
if (vs[e.x] == 0)dfs(e.x, n);
}
}
int f(int n) {
if (n == up[n])return n;
return up[n] = f(up[n]);
}
void un(int a, int b) {
a = f(a);
b = f(b);
up[a] = b;
}
vector<int> find_roads(int n, vector<int>u, vector<int>v) {
int i, j, r, m = v.size(), k, nn, ln;
vector<int>qv;
mm.resize(m, -1);
l.resize(n);
while (m--) {
l[u[m]].push_back({ v[m],m });
l[v[m]].push_back({ u[m],m });
}
m = u.size();
dfs(0, -1);
for (i = 1; i < n; i++)qv.push_back(pl[i]);
k = count_common_roads(qv);
//printf("##%d##\n", k);
for (i = 1; i < n; i++) {
//for (j = 0; j < m; j++)printf("%d ", mm[j]);
//printf("\n");
nn = ln = p[i];
for (pi e : l[i]) {
if (vs[e.x] < vs[nn]) {
nn = e.x;
ln = e.y;
}
}
//printf("%d %d\n", i, p[i]);
if (nn == p[i])continue;
//printf("%d [%d,%d]\n", i, nn, ln);
int b = 0, c = 1;
for (j = i; j != nn; j = p[j]) {
r = pl[j];
if (mm[r] >= 0)continue;
c = 0;
qv.clear();
qv.push_back(ln);
for (int ii = 1; ii < n; ii++)if (pl[ii] != r)qv.push_back(pl[ii]);
mm[r] = 3 + count_common_roads(qv) - k;//3+that road-this road
if (mm[r] != 3)b = mm[r] - 3;
}
if (c)continue;
if (b == 1) {
//printf("Case 1\n");
for (j = i; j != nn; j = p[j]) {
r = pl[j];
if (mm[r] > 1) {
mm[r] = 4 - mm[r];
}
}
}
else if (b == -1) {
//printf("Case 2\n");
for (j = i; j != nn; j = p[j]) {
r = pl[j];
if (mm[r] > 1) {
mm[r] = 3 - mm[r];
}
}
}
else {
//printf("Case 3\n");
int K = -1;
for (j = i; j != nn; j = p[j]) {
r = pl[j];
if (mm[r] < 2) {
qv.clear();
qv.push_back(ln);
for (int ii = 1; ii < n; ii++)if (pl[ii] != r)qv.push_back(pl[ii]);
K = count_common_roads(qv) - k + mm[r];
break;
}
}
//printf("[%d]", K);
if (K < 0) {
//printf("ALL FIRST\n");
for (j = i; j != nn; j = p[j]) {
r = pl[j];
mm[r] = 0;
}
}
else {
//printf("PAST EXISTS\n");
for (j = i; j != nn; j = p[j]) {
r = pl[j];
if (mm[r] < 2)continue;
mm[r] = K;
}
}
}
}
//for (j = 0; j < m; j++)printf("%d ", mm[j]);
//printf("\n");
for (i = 1; i < n; i++) {
if (mm[pl[i]] < 0)mm[pl[i]] = 1;
}
//for (j = 0; j < m; j++)printf("%d ", mm[j]);
//printf("\n");
queue<int>q;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)up[j] = j;
qv.clear();
for (pi e : l[i]) {
qv.push_back(e.y);
un(e.x, i);
}
k = 0;
for (j = 1; j < n; j++) {
if (f(j) != f(p[j])) {
un(j, p[j]);
qv.push_back(pl[j]);
k += mm[pl[j]];
}
}
cs[i] = count_common_roads(qv) - k;
if (cs[i] == 1)q.push(i);
}
//for (i = 0; i < n; i++)printf("%d ", cs[i]);
//printf("\n");
//m = u.size();
//for (i = 0; i < m; i++)printf("%d ", mm[i]);
//printf("\n");
vector<pi>vv;
while (q.size()) {
i = q.front();
q.pop();
//printf("Test: %d\n", i);
if (cs[i] != 1)continue;
//printf("Yeah\n");
vv.clear();
for (pi e : l[i]) {
if (cs[e.x])vv.push_back(e);
}
while (vv.size() > 1) {
//printf("[ ");
//for (pi e : vv)printf("%d ", e.y);
//printf("]\n");
for (j = 0; j < n; j++)up[j] = j;
qv.clear();
for (j = 0; j < vv.size() / 2; j++) {
qv.push_back(vv[j].y);
un(vv[j].x, i);
}
k = 0;
for (j = 1; j < n; j++) {
if (f(j) != f(p[j])) {
un(j, p[j]);
qv.push_back(pl[j]);
k += mm[pl[j]];
}
}
//printf("%d %d\n", k, count_common_roads(qv));
if (count_common_roads(qv) == k) {
vv.erase(vv.begin(), vv.begin() + (vv.size() / 2));
}
else {
vv.erase(vv.begin() + (vv.size() / 2), vv.end());
}
}
//printf("[ ");
//for (pi e : vv)printf("%d ", e.y);
//printf("]\n");
for (pi e : l[i]) {
if (e.x == vv[0].x) {
mm[e.y] = 1;
cs[e.x]--;
if (cs[e.x] == 1)q.push(e.x);
}
else if (mm[e.y] == -1) {
mm[e.y] = 0;
}
}
cs[i] = 0;
}
m = u.size();
//for (i = 0; i < m; i++)printf("%d ", mm[i]);
//printf("\n");
vector<int>ans;
for (i = 0; i < m; i++) {
if (mm[i] == 1)ans.push_back(i);
}
return ans;
}
Compilation message
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:167:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
167 | for (j = 0; j < vv.size() / 2; j++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
0 ms |
204 KB |
correct |
3 |
Correct |
0 ms |
288 KB |
correct |
4 |
Correct |
0 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
288 KB |
correct |
6 |
Correct |
0 ms |
204 KB |
correct |
7 |
Correct |
0 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
204 KB |
correct |
9 |
Correct |
0 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
204 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
0 ms |
204 KB |
correct |
3 |
Correct |
0 ms |
288 KB |
correct |
4 |
Correct |
0 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
288 KB |
correct |
6 |
Correct |
0 ms |
204 KB |
correct |
7 |
Correct |
0 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
204 KB |
correct |
9 |
Correct |
0 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
204 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
14 |
Correct |
1 ms |
356 KB |
correct |
15 |
Correct |
1 ms |
336 KB |
correct |
16 |
Correct |
1 ms |
292 KB |
correct |
17 |
Correct |
1 ms |
296 KB |
correct |
18 |
Correct |
1 ms |
208 KB |
correct |
19 |
Correct |
2 ms |
336 KB |
correct |
20 |
Correct |
1 ms |
336 KB |
correct |
21 |
Correct |
1 ms |
336 KB |
correct |
22 |
Correct |
1 ms |
336 KB |
correct |
23 |
Correct |
1 ms |
208 KB |
correct |
24 |
Correct |
1 ms |
208 KB |
correct |
25 |
Correct |
1 ms |
292 KB |
correct |
26 |
Correct |
1 ms |
208 KB |
correct |
27 |
Correct |
1 ms |
208 KB |
correct |
28 |
Correct |
1 ms |
208 KB |
correct |
29 |
Correct |
1 ms |
208 KB |
correct |
30 |
Correct |
1 ms |
284 KB |
correct |
31 |
Correct |
1 ms |
292 KB |
correct |
32 |
Correct |
1 ms |
336 KB |
correct |
33 |
Correct |
1 ms |
208 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
0 ms |
204 KB |
correct |
3 |
Correct |
0 ms |
288 KB |
correct |
4 |
Correct |
0 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
288 KB |
correct |
6 |
Correct |
0 ms |
204 KB |
correct |
7 |
Correct |
0 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
204 KB |
correct |
9 |
Correct |
0 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
204 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
14 |
Correct |
1 ms |
356 KB |
correct |
15 |
Correct |
1 ms |
336 KB |
correct |
16 |
Correct |
1 ms |
292 KB |
correct |
17 |
Correct |
1 ms |
296 KB |
correct |
18 |
Correct |
1 ms |
208 KB |
correct |
19 |
Correct |
2 ms |
336 KB |
correct |
20 |
Correct |
1 ms |
336 KB |
correct |
21 |
Correct |
1 ms |
336 KB |
correct |
22 |
Correct |
1 ms |
336 KB |
correct |
23 |
Correct |
1 ms |
208 KB |
correct |
24 |
Correct |
1 ms |
208 KB |
correct |
25 |
Correct |
1 ms |
292 KB |
correct |
26 |
Correct |
1 ms |
208 KB |
correct |
27 |
Correct |
1 ms |
208 KB |
correct |
28 |
Correct |
1 ms |
208 KB |
correct |
29 |
Correct |
1 ms |
208 KB |
correct |
30 |
Correct |
1 ms |
284 KB |
correct |
31 |
Correct |
1 ms |
292 KB |
correct |
32 |
Correct |
1 ms |
336 KB |
correct |
33 |
Correct |
1 ms |
208 KB |
correct |
34 |
Correct |
26 ms |
1616 KB |
correct |
35 |
Correct |
25 ms |
1596 KB |
correct |
36 |
Correct |
23 ms |
1324 KB |
correct |
37 |
Correct |
8 ms |
336 KB |
correct |
38 |
Correct |
28 ms |
1628 KB |
correct |
39 |
Correct |
25 ms |
1412 KB |
correct |
40 |
Correct |
22 ms |
1360 KB |
correct |
41 |
Correct |
28 ms |
1616 KB |
correct |
42 |
Correct |
25 ms |
1616 KB |
correct |
43 |
Correct |
19 ms |
1104 KB |
correct |
44 |
Correct |
17 ms |
848 KB |
correct |
45 |
Correct |
19 ms |
848 KB |
correct |
46 |
Correct |
16 ms |
856 KB |
correct |
47 |
Correct |
12 ms |
560 KB |
correct |
48 |
Correct |
4 ms |
208 KB |
correct |
49 |
Correct |
9 ms |
336 KB |
correct |
50 |
Correct |
12 ms |
464 KB |
correct |
51 |
Correct |
19 ms |
968 KB |
correct |
52 |
Correct |
18 ms |
848 KB |
correct |
53 |
Correct |
17 ms |
848 KB |
correct |
54 |
Correct |
19 ms |
1104 KB |
correct |
55 |
Correct |
19 ms |
976 KB |
correct |
56 |
Correct |
20 ms |
976 KB |
correct |
57 |
Correct |
20 ms |
976 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
0 ms |
204 KB |
correct |
3 |
Correct |
76 ms |
4312 KB |
correct |
4 |
Correct |
126 ms |
6048 KB |
correct |
5 |
Correct |
138 ms |
5984 KB |
correct |
6 |
Correct |
120 ms |
5960 KB |
correct |
7 |
Correct |
116 ms |
5960 KB |
correct |
8 |
Correct |
132 ms |
6028 KB |
correct |
9 |
Correct |
122 ms |
5980 KB |
correct |
10 |
Correct |
134 ms |
5960 KB |
correct |
11 |
Correct |
122 ms |
5976 KB |
correct |
12 |
Correct |
133 ms |
5960 KB |
correct |
13 |
Correct |
0 ms |
208 KB |
correct |
14 |
Correct |
128 ms |
5924 KB |
correct |
15 |
Correct |
120 ms |
5988 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
0 ms |
204 KB |
correct |
3 |
Correct |
0 ms |
288 KB |
correct |
4 |
Correct |
0 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
288 KB |
correct |
6 |
Correct |
0 ms |
204 KB |
correct |
7 |
Correct |
0 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
204 KB |
correct |
9 |
Correct |
0 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
204 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
14 |
Correct |
1 ms |
356 KB |
correct |
15 |
Correct |
1 ms |
336 KB |
correct |
16 |
Correct |
1 ms |
292 KB |
correct |
17 |
Correct |
1 ms |
296 KB |
correct |
18 |
Correct |
1 ms |
208 KB |
correct |
19 |
Correct |
2 ms |
336 KB |
correct |
20 |
Correct |
1 ms |
336 KB |
correct |
21 |
Correct |
1 ms |
336 KB |
correct |
22 |
Correct |
1 ms |
336 KB |
correct |
23 |
Correct |
1 ms |
208 KB |
correct |
24 |
Correct |
1 ms |
208 KB |
correct |
25 |
Correct |
1 ms |
292 KB |
correct |
26 |
Correct |
1 ms |
208 KB |
correct |
27 |
Correct |
1 ms |
208 KB |
correct |
28 |
Correct |
1 ms |
208 KB |
correct |
29 |
Correct |
1 ms |
208 KB |
correct |
30 |
Correct |
1 ms |
284 KB |
correct |
31 |
Correct |
1 ms |
292 KB |
correct |
32 |
Correct |
1 ms |
336 KB |
correct |
33 |
Correct |
1 ms |
208 KB |
correct |
34 |
Correct |
26 ms |
1616 KB |
correct |
35 |
Correct |
25 ms |
1596 KB |
correct |
36 |
Correct |
23 ms |
1324 KB |
correct |
37 |
Correct |
8 ms |
336 KB |
correct |
38 |
Correct |
28 ms |
1628 KB |
correct |
39 |
Correct |
25 ms |
1412 KB |
correct |
40 |
Correct |
22 ms |
1360 KB |
correct |
41 |
Correct |
28 ms |
1616 KB |
correct |
42 |
Correct |
25 ms |
1616 KB |
correct |
43 |
Correct |
19 ms |
1104 KB |
correct |
44 |
Correct |
17 ms |
848 KB |
correct |
45 |
Correct |
19 ms |
848 KB |
correct |
46 |
Correct |
16 ms |
856 KB |
correct |
47 |
Correct |
12 ms |
560 KB |
correct |
48 |
Correct |
4 ms |
208 KB |
correct |
49 |
Correct |
9 ms |
336 KB |
correct |
50 |
Correct |
12 ms |
464 KB |
correct |
51 |
Correct |
19 ms |
968 KB |
correct |
52 |
Correct |
18 ms |
848 KB |
correct |
53 |
Correct |
17 ms |
848 KB |
correct |
54 |
Correct |
19 ms |
1104 KB |
correct |
55 |
Correct |
19 ms |
976 KB |
correct |
56 |
Correct |
20 ms |
976 KB |
correct |
57 |
Correct |
20 ms |
976 KB |
correct |
58 |
Correct |
1 ms |
204 KB |
correct |
59 |
Correct |
0 ms |
204 KB |
correct |
60 |
Correct |
76 ms |
4312 KB |
correct |
61 |
Correct |
126 ms |
6048 KB |
correct |
62 |
Correct |
138 ms |
5984 KB |
correct |
63 |
Correct |
120 ms |
5960 KB |
correct |
64 |
Correct |
116 ms |
5960 KB |
correct |
65 |
Correct |
132 ms |
6028 KB |
correct |
66 |
Correct |
122 ms |
5980 KB |
correct |
67 |
Correct |
134 ms |
5960 KB |
correct |
68 |
Correct |
122 ms |
5976 KB |
correct |
69 |
Correct |
133 ms |
5960 KB |
correct |
70 |
Correct |
0 ms |
208 KB |
correct |
71 |
Correct |
128 ms |
5924 KB |
correct |
72 |
Correct |
120 ms |
5988 KB |
correct |
73 |
Correct |
1 ms |
208 KB |
correct |
74 |
Correct |
130 ms |
6004 KB |
correct |
75 |
Correct |
133 ms |
5856 KB |
correct |
76 |
Correct |
66 ms |
2368 KB |
correct |
77 |
Correct |
131 ms |
5976 KB |
correct |
78 |
Correct |
124 ms |
5972 KB |
correct |
79 |
Correct |
131 ms |
5956 KB |
correct |
80 |
Correct |
146 ms |
5832 KB |
correct |
81 |
Correct |
115 ms |
5376 KB |
correct |
82 |
Correct |
123 ms |
5880 KB |
correct |
83 |
Correct |
99 ms |
3460 KB |
correct |
84 |
Correct |
98 ms |
4016 KB |
correct |
85 |
Correct |
93 ms |
3928 KB |
correct |
86 |
Correct |
82 ms |
2520 KB |
correct |
87 |
Correct |
69 ms |
2196 KB |
correct |
88 |
Correct |
66 ms |
1564 KB |
correct |
89 |
Correct |
62 ms |
1456 KB |
correct |
90 |
Correct |
59 ms |
1320 KB |
correct |
91 |
Correct |
27 ms |
464 KB |
correct |
92 |
Correct |
16 ms |
304 KB |
correct |
93 |
Correct |
98 ms |
3828 KB |
correct |
94 |
Correct |
81 ms |
2512 KB |
correct |
95 |
Correct |
83 ms |
2616 KB |
correct |
96 |
Correct |
62 ms |
1456 KB |
correct |
97 |
Correct |
68 ms |
1560 KB |
correct |
98 |
Correct |
70 ms |
2128 KB |
correct |
99 |
Correct |
65 ms |
1564 KB |
correct |
100 |
Correct |
40 ms |
560 KB |
correct |
101 |
Correct |
17 ms |
384 KB |
correct |
102 |
Correct |
96 ms |
3196 KB |
correct |
103 |
Correct |
93 ms |
3196 KB |
correct |
104 |
Correct |
92 ms |
3172 KB |
correct |
105 |
Correct |
94 ms |
3276 KB |
correct |