/**
* author: wxhtzdy
* created: 03.12.2022 19:26:33
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> dep(n);
function<void(int, int)> Go = [&](int v, int pv) {
for (int u : g[v]) {
if (u == pv) {
continue;
}
dep[u] = dep[v] + 1;
Go(u, v);
}
};
Go(0, 0);
int x = max_element(dep.begin(), dep.end()) - dep.begin();
dep[x] = 0;
Go(x, x);
int y = max_element(dep.begin(), dep.end()) - dep.begin();
const int L = 25;
vector<vector<int>> pr(n, vector<int>(L));
function<void(int, int)> Dfs = [&](int v, int pv) {
pr[v][0] = pv;
for (int u : g[v]) {
if (u == pv) {
continue;
}
dep[u] = dep[v] + 1;
Dfs(u, v);
}
};
Dfs(0, 0);
for (int j = 1; j < L; j++) {
for (int i = 0; i < n; i++) {
pr[i][j] = pr[pr[i][j - 1]][j - 1];
}
}
auto LCA = [&](int u, int v) {
if (dep[u] < dep[v]) {
swap(u, v);
}
for (int i = L - 1; i >= 0; i--) {
if (dep[pr[u][i]] >= dep[v]) {
u = pr[u][i];
}
}
if (u == v) {
return u;
}
for (int i = L - 1; i >= 0; i--) {
if (pr[u][i] != pr[v][i]) {
u = pr[u][i];
v = pr[v][i];
}
}
return pr[u][0];
};
auto GetDist = [&](int u, int v) {
return dep[u] + dep[v] - 2 * dep[LCA(u, v)];
};
for (int i = 0; i < n; i++) {
cout << (GetDist(i, x) != GetDist(i, y) ? 1 : 0) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
2 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
221 ms |
24336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
329 ms |
33140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
2 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |