#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
class dsu {
public:
vector<int> p;
int n;
dsu(int _n) : n(_n) {
p.resize(n);
iota(p.begin(), p.end(), 0);
}
inline int get(int x) {
return (x == p[x] ? x : (p[x] = get(p[x])));
}
inline bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x != y) {
p[x] = y;
return true;
}
return false;
}
};
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
int m = (int) u.size();
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
g[u[i]].push_back(i);
g[v[i]].push_back(i);
}
dsu d(n);
vector<int> tree;
for (int i = 0; i < m; i++) {
if (d.unite(u[i], v[i])) {
tree.push_back(i);
}
}
vector<vector<int>> t(n);
for (int i : tree) {
t[u[i]].push_back(i);
t[v[i]].push_back(i);
}
int cnt = count_common_roads(tree);
const int L = 20;
vector<vector<int>> pr(n, vector<int>(L));
vector<int> dep(n);
vector<int> id(n);
function<void(int, int)> Dfs = [&](int x, int px) {
dep[x] = dep[px] + 1;
pr[x][0] = px;
for (int i = 1; i < L; i++) {
pr[x][i] = pr[pr[x][i - 1]][i - 1];
}
for (int e : t[x]) {
int y = (x ^ u[e] ^ v[e]);
if (y == px) {
continue;
}
id[y] = e;
Dfs(y, x);
}
};
Dfs(0, 0);
auto LCA = [&](int x, int y) {
if (dep[x] > dep[y]) {
swap(x, y);
}
for (int i = L - 1; i >= 0; i--) {
if (dep[pr[y][i]] >= dep[x]) {
y = pr[y][i];
}
}
if (x == y) {
return x;
}
for (int i = L - 1; i >= 0; i--) {
if (pr[x][i] != pr[y][i]) {
x = pr[x][i];
y = pr[y][i];
}
}
return pr[x][0];
};
vector<int> state(m, 2);
for (int i : tree) {
state[i] = -1;
}
for (int i = 0; i < m; i++) {
if (state[i] != 2) {
continue;
}
vector<int> edges;
int z = LCA(u[i], v[i]);
for (int x = u[i]; x != z; x = pr[x][0]) {
edges.push_back(id[x]);
}
for (int x = v[i]; x != z; x = pr[x][0]) {
edges.push_back(id[x]);
}
vector<pair<int, int>> a;
for (int e : edges) {
if (state[e] != -1) {
continue;
}
vector<int> qv(1, i);
for (int i : tree) {
if (i != e) {
qv.push_back(i);
}
}
a.emplace_back(count_common_roads(qv), e);
}
if (a.empty()) {
continue;
}
for (auto& p : a) {
if (p.first == cnt - 1) {
state[i] = 0;
}
if (p.first == cnt + 1) {
state[i] = 1;
}
}
if (state[i] == 2) {
for (int e : edges) {
if (state[e] == -1) {
continue;
}
vector<int> qv(1, i);
for (int i : tree) {
if (i != e) {
qv.push_back(i);
}
}
state[i] = (state[e] ^ (cnt != count_common_roads(qv) ? 1 : 0));
break;
}
if (state[i] == 2) {
state[i] = 0;
}
}
for (auto& p : a) {
state[p.second] = (state[i] ^ (cnt != p.first ? 1 : 0));
}
}
vector<int> deg(n);
for (int i = 0; i < n; i++) {
dsu d(n);
vector<int> qv;
for (int e : g[i]) {
qv.push_back(e);
d.unite(u[e], v[e]);
}
int ft = 0;
for (int e : tree) {
if (d.unite(u[e], v[e])) {
qv.push_back(e);
ft += state[e];
}
}
deg[i] = count_common_roads(qv) - ft;
}
vector<int> res;
for (int i = 0; i < n; i++) {
int ptr = 0;
for (int foo = 0; foo < deg[i]; foo++) {
int low = ptr, high = (int) g[i].size() - 1, p = 0;
while (low <= high) {
int mid = (low + high) >> 1;
vector<int> qv;
dsu d(n);
for (int j = ptr; j <= mid; j++) {
qv.push_back(g[i][j]);
d.unite(u[g[i][j]], v[g[i][j]]);
}
int ft = 0;
for (int e : tree) {
if (d.unite(u[e], v[e])) {
qv.push_back(e);
ft += state[e];
}
}
if (count_common_roads(qv) - ft > 0) {
p = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
res.push_back(g[i][p]);
ptr = p + 1;
}
}
sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
344 KB |
correct |
6 |
Correct |
0 ms |
348 KB |
correct |
7 |
Correct |
1 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
348 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
344 KB |
correct |
6 |
Correct |
0 ms |
348 KB |
correct |
7 |
Correct |
1 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
348 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 |
2 ms |
348 KB |
correct |
15 |
Correct |
1 ms |
348 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 |
2 ms |
348 KB |
correct |
20 |
Correct |
2 ms |
348 KB |
correct |
21 |
Correct |
1 ms |
480 KB |
correct |
22 |
Incorrect |
2 ms |
348 KB |
WA in grader: NO |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
344 KB |
correct |
6 |
Correct |
0 ms |
348 KB |
correct |
7 |
Correct |
1 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
348 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 |
2 ms |
348 KB |
correct |
15 |
Correct |
1 ms |
348 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 |
2 ms |
348 KB |
correct |
20 |
Correct |
2 ms |
348 KB |
correct |
21 |
Correct |
1 ms |
480 KB |
correct |
22 |
Incorrect |
2 ms |
348 KB |
WA in grader: NO |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
correct |
2 |
Correct |
0 ms |
348 KB |
correct |
3 |
Correct |
134 ms |
3452 KB |
correct |
4 |
Correct |
217 ms |
5008 KB |
correct |
5 |
Correct |
226 ms |
5016 KB |
correct |
6 |
Correct |
208 ms |
4956 KB |
correct |
7 |
Correct |
193 ms |
5020 KB |
correct |
8 |
Correct |
192 ms |
4956 KB |
correct |
9 |
Correct |
212 ms |
4952 KB |
correct |
10 |
Correct |
227 ms |
4952 KB |
correct |
11 |
Correct |
216 ms |
5008 KB |
correct |
12 |
Correct |
220 ms |
4956 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
14 |
Correct |
207 ms |
5024 KB |
correct |
15 |
Correct |
215 ms |
5000 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
344 KB |
correct |
6 |
Correct |
0 ms |
348 KB |
correct |
7 |
Correct |
1 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
348 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 |
2 ms |
348 KB |
correct |
15 |
Correct |
1 ms |
348 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 |
2 ms |
348 KB |
correct |
20 |
Correct |
2 ms |
348 KB |
correct |
21 |
Correct |
1 ms |
480 KB |
correct |
22 |
Incorrect |
2 ms |
348 KB |
WA in grader: NO |
23 |
Halted |
0 ms |
0 KB |
- |