#include "simurgh.h"
#include "bits/stdc++.h"
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, s, n) for(ll i = ll(s); i < ll(n); i++)
#define rrep(i, n) for(ll i = ll(n)-1; i >= 0; i--)
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vs = vector<string>;
const int inf = 1001001001;
const ll linf = 1001001001001001001;
template<class T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T>
bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
vi find_roads(int n, vi u, vi v) {
int m = u.size();
vvi G(n);
vvi id(n, vi(n));
rep(i, m) {
G[u[i]].pb(v[i]);
G[v[i]].pb(u[i]);
id[u[i]][v[i]] = i;
id[v[i]][u[i]] = i;
}
vi par(n, inf);
vi dep(n);
vb on_tree(m);
vi ori_edge;
auto dfs = [&](auto &dfs, int now, int p, int d) -> void {
par[now] = p;
dep[now] = d;
for (int nx : G[now]) {
if (nx == p) continue;
if (par[nx] != inf) continue;
on_tree[id[now][nx]] = true;
ori_edge.pb(id[now][nx]);
dfs(dfs, nx, now, d + 1);
}
};
dfs(dfs, 0, -1, 0);
auto lca = [&](int i, int j) {
if (dep[i] > dep[j]) swap(i, j);
while (dep[i] < dep[j]) j = par[j];
while (i != j) {
i = par[i];
j = par[j];
}
return i;
};
const int ori = count_common_roads(ori_edge);
auto replace = [&](int add, int er) -> int {
vi tmp = {add};
for (int i : ori_edge) {
if (i != er) tmp.pb(i);
}
return count_common_roads(tmp) - ori;
};
vi ans(m, -1);
rep(i, m) {
if (on_tree[i]) continue;
assert(ans[i] == -1);
int l = lca(u[i], v[i]);
vi es;
int now = u[i];
while (now != l) {
es.pb(id[now][par[now]]);
now = par[now];
}
now = v[i];
while (now != l) {
es.pb(id[now][par[now]]);
now = par[now];
}
int alr = -1;
vi same = {(int) i}, dif;
int self = -1;
for (int j : es) {
if (ans[j] == -1) {
int r = replace(i, j);
assert(-1 <= r and r <= 1);
if (r == 0) same.pb(j);
else {
dif.pb(j);
self = (r > 0 ? 1 : 0);
}
} else {
alr = j;
}
}
if (self != -1) {
for (int j : same) ans[j] = self;
for (int j : dif) ans[j] = self ^ 1;
continue;
}
assert(dif.empty());
if (alr == -1) {
for (int j : same) ans[j] = 0;
} else {
int r = replace(i, alr);
assert(0 <= ans[alr] + r and ans[alr] + r <= 1);
for (int j : same) ans[j] = ans[alr] + r;
}
}
vi res;
rep(i, m) {
if (ans[i] == 1 or ans[i] == -1) res.pb(i);
}
assert(res.size() == n - 1);
return res;
}
Compilation message
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from simurgh.cpp:2:
simurgh.cpp: In function 'vi find_roads(int, vi, vi)':
simurgh.cpp:136:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
136 | assert(res.size() == n - 1);
| ~~~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
204 KB |
correct |
4 |
Correct |
1 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
292 KB |
correct |
7 |
Correct |
1 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
288 KB |
correct |
9 |
Correct |
1 ms |
252 KB |
correct |
10 |
Correct |
1 ms |
288 KB |
correct |
11 |
Correct |
1 ms |
292 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 |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
204 KB |
correct |
4 |
Correct |
1 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
292 KB |
correct |
7 |
Correct |
1 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
288 KB |
correct |
9 |
Correct |
1 ms |
252 KB |
correct |
10 |
Correct |
1 ms |
288 KB |
correct |
11 |
Correct |
1 ms |
292 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
14 |
Correct |
3 ms |
332 KB |
correct |
15 |
Correct |
4 ms |
332 KB |
correct |
16 |
Correct |
4 ms |
332 KB |
correct |
17 |
Correct |
3 ms |
348 KB |
correct |
18 |
Correct |
2 ms |
204 KB |
correct |
19 |
Correct |
3 ms |
332 KB |
correct |
20 |
Correct |
3 ms |
332 KB |
correct |
21 |
Correct |
3 ms |
332 KB |
correct |
22 |
Correct |
3 ms |
332 KB |
correct |
23 |
Correct |
2 ms |
384 KB |
correct |
24 |
Correct |
2 ms |
292 KB |
correct |
25 |
Correct |
1 ms |
256 KB |
correct |
26 |
Correct |
3 ms |
204 KB |
correct |
27 |
Correct |
2 ms |
328 KB |
correct |
28 |
Correct |
1 ms |
208 KB |
correct |
29 |
Correct |
1 ms |
204 KB |
correct |
30 |
Correct |
2 ms |
336 KB |
correct |
31 |
Correct |
2 ms |
296 KB |
correct |
32 |
Correct |
2 ms |
332 KB |
correct |
33 |
Correct |
2 ms |
328 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
204 KB |
correct |
4 |
Correct |
1 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
292 KB |
correct |
7 |
Correct |
1 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
288 KB |
correct |
9 |
Correct |
1 ms |
252 KB |
correct |
10 |
Correct |
1 ms |
288 KB |
correct |
11 |
Correct |
1 ms |
292 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
14 |
Correct |
3 ms |
332 KB |
correct |
15 |
Correct |
4 ms |
332 KB |
correct |
16 |
Correct |
4 ms |
332 KB |
correct |
17 |
Correct |
3 ms |
348 KB |
correct |
18 |
Correct |
2 ms |
204 KB |
correct |
19 |
Correct |
3 ms |
332 KB |
correct |
20 |
Correct |
3 ms |
332 KB |
correct |
21 |
Correct |
3 ms |
332 KB |
correct |
22 |
Correct |
3 ms |
332 KB |
correct |
23 |
Correct |
2 ms |
384 KB |
correct |
24 |
Correct |
2 ms |
292 KB |
correct |
25 |
Correct |
1 ms |
256 KB |
correct |
26 |
Correct |
3 ms |
204 KB |
correct |
27 |
Correct |
2 ms |
328 KB |
correct |
28 |
Correct |
1 ms |
208 KB |
correct |
29 |
Correct |
1 ms |
204 KB |
correct |
30 |
Correct |
2 ms |
336 KB |
correct |
31 |
Correct |
2 ms |
296 KB |
correct |
32 |
Correct |
2 ms |
332 KB |
correct |
33 |
Correct |
2 ms |
328 KB |
correct |
34 |
Correct |
225 ms |
1480 KB |
correct |
35 |
Correct |
200 ms |
1552 KB |
correct |
36 |
Correct |
138 ms |
1332 KB |
correct |
37 |
Correct |
9 ms |
552 KB |
correct |
38 |
Correct |
215 ms |
1576 KB |
correct |
39 |
Correct |
175 ms |
1388 KB |
correct |
40 |
Correct |
136 ms |
1324 KB |
correct |
41 |
Correct |
210 ms |
1572 KB |
correct |
42 |
Correct |
213 ms |
1580 KB |
correct |
43 |
Correct |
115 ms |
1184 KB |
correct |
44 |
Correct |
84 ms |
972 KB |
correct |
45 |
Correct |
95 ms |
972 KB |
correct |
46 |
Correct |
89 ms |
844 KB |
correct |
47 |
Correct |
33 ms |
716 KB |
correct |
48 |
Correct |
2 ms |
460 KB |
correct |
49 |
Correct |
9 ms |
588 KB |
correct |
50 |
Correct |
30 ms |
716 KB |
correct |
51 |
Correct |
100 ms |
1048 KB |
correct |
52 |
Correct |
82 ms |
972 KB |
correct |
53 |
Correct |
77 ms |
932 KB |
correct |
54 |
Correct |
125 ms |
1132 KB |
correct |
55 |
Correct |
98 ms |
1068 KB |
correct |
56 |
Correct |
107 ms |
1060 KB |
correct |
57 |
Correct |
117 ms |
1060 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Incorrect |
168 ms |
4068 KB |
WA in grader: NO |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
204 KB |
correct |
4 |
Correct |
1 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
292 KB |
correct |
7 |
Correct |
1 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
288 KB |
correct |
9 |
Correct |
1 ms |
252 KB |
correct |
10 |
Correct |
1 ms |
288 KB |
correct |
11 |
Correct |
1 ms |
292 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
14 |
Correct |
3 ms |
332 KB |
correct |
15 |
Correct |
4 ms |
332 KB |
correct |
16 |
Correct |
4 ms |
332 KB |
correct |
17 |
Correct |
3 ms |
348 KB |
correct |
18 |
Correct |
2 ms |
204 KB |
correct |
19 |
Correct |
3 ms |
332 KB |
correct |
20 |
Correct |
3 ms |
332 KB |
correct |
21 |
Correct |
3 ms |
332 KB |
correct |
22 |
Correct |
3 ms |
332 KB |
correct |
23 |
Correct |
2 ms |
384 KB |
correct |
24 |
Correct |
2 ms |
292 KB |
correct |
25 |
Correct |
1 ms |
256 KB |
correct |
26 |
Correct |
3 ms |
204 KB |
correct |
27 |
Correct |
2 ms |
328 KB |
correct |
28 |
Correct |
1 ms |
208 KB |
correct |
29 |
Correct |
1 ms |
204 KB |
correct |
30 |
Correct |
2 ms |
336 KB |
correct |
31 |
Correct |
2 ms |
296 KB |
correct |
32 |
Correct |
2 ms |
332 KB |
correct |
33 |
Correct |
2 ms |
328 KB |
correct |
34 |
Correct |
225 ms |
1480 KB |
correct |
35 |
Correct |
200 ms |
1552 KB |
correct |
36 |
Correct |
138 ms |
1332 KB |
correct |
37 |
Correct |
9 ms |
552 KB |
correct |
38 |
Correct |
215 ms |
1576 KB |
correct |
39 |
Correct |
175 ms |
1388 KB |
correct |
40 |
Correct |
136 ms |
1324 KB |
correct |
41 |
Correct |
210 ms |
1572 KB |
correct |
42 |
Correct |
213 ms |
1580 KB |
correct |
43 |
Correct |
115 ms |
1184 KB |
correct |
44 |
Correct |
84 ms |
972 KB |
correct |
45 |
Correct |
95 ms |
972 KB |
correct |
46 |
Correct |
89 ms |
844 KB |
correct |
47 |
Correct |
33 ms |
716 KB |
correct |
48 |
Correct |
2 ms |
460 KB |
correct |
49 |
Correct |
9 ms |
588 KB |
correct |
50 |
Correct |
30 ms |
716 KB |
correct |
51 |
Correct |
100 ms |
1048 KB |
correct |
52 |
Correct |
82 ms |
972 KB |
correct |
53 |
Correct |
77 ms |
932 KB |
correct |
54 |
Correct |
125 ms |
1132 KB |
correct |
55 |
Correct |
98 ms |
1068 KB |
correct |
56 |
Correct |
107 ms |
1060 KB |
correct |
57 |
Correct |
117 ms |
1060 KB |
correct |
58 |
Correct |
1 ms |
204 KB |
correct |
59 |
Correct |
1 ms |
204 KB |
correct |
60 |
Incorrect |
168 ms |
4068 KB |
WA in grader: NO |
61 |
Halted |
0 ms |
0 KB |
- |