#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define ll long long
//cin.tie(0);ios_base::sync_with_stdio(0);
const int MAXN = 1e6 + 5;
int curLongest = 0;
bool cent[MAXN];
int s[MAXN];
vector<int> adjList[MAXN];
int n;
void dfs(int src, int par) {
s[src] = 1;
bool bad = false;
for (auto i : adjList[src]) {
if (i == par) {
continue;
}
dfs(i, src);
s[src] += s[i];
if (s[i] > n / 2) {
bad = true;
}
}
if (n - s[src] > n / 2) {
bad = true;
}
cent[src] = !bad;
}
int dfs1(int src, int par, int dist) {
int ret = 2*dist;
curLongest = max(curLongest, dist);
for (auto i : adjList[src]) {
if (i == par) {
continue;
}
ret += dfs1(i, src, dist + 1);
}
return ret;
}
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--; b--;
adjList[a].push_back(b);
adjList[b].push_back(a);
}
dfs(0, 0);
for (int i = 0; i < n; i++) {
if (!cent[i]) {
cout << -1 << endl;
}
else {
cout << dfs1(i, i, 0) - curLongest << endl;
}
curLongest = 0;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
23788 KB |
Output is correct |
2 |
Incorrect |
17 ms |
23788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
23916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
24804 KB |
Output is correct |
2 |
Incorrect |
78 ms |
25336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
141 ms |
25828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
350 ms |
29156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1906 ms |
50676 KB |
Output is correct |
2 |
Incorrect |
1885 ms |
60496 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3920 ms |
77956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3900 ms |
77652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3885 ms |
77928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |