#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int n;
int sz[N];
vector<int> adj[N];
long long rans[N];
vector<int> xp[N];
int dans[N];
int lp[N];
bool valid[N];
int dfs(int v, int p) {
sz[v] = 1; lp[v] = 0;
for (int w : adj[v]) if (w != p) {
sz[v] += dfs(w,v);
lp[v] = max(lp[v], lp[w]+1);
}
return sz[v];
}
void dfsr(int v, int p, int len) {
rans[v] = v ? rans[p] + (n - sz[v]) - sz[v] : 0;
if (!v)
for (int i = 1; i < n; i++)
rans[v] += sz[i];
xp[v].push_back(len+1);
int max_sz = 0, llen = 0;
for (int w : adj[v]) if (w != p) {
if (sz[w] > max_sz) {
max_sz = sz[w];
llen = lp[w]+1;
}
xp[v].push_back(lp[w]+2);
sort(begin(xp[v]),end(xp[v]),greater<int>());
while (xp[v].size() > 2) xp[v].pop_back();
}
if (n-sz[v] > max_sz) {
max_sz = n-sz[v];
llen = len;
}
valid[v] = max_sz - (n - 1 - max_sz) <= 1;
dans[v] = max(len, lp[v]);
if (max_sz - (n - 1 - max_sz) == 1)
dans[v] = llen;
for (int w : adj[v]) if (w != p) {
int l = xp[v][0];
if (xp[v][0] == lp[w]+2) l = xp[v][1];
dfsr(w,v,l);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for (int i = 0; i < n-1; i++) {
int a, b; cin>>a>>b;
adj[--a].push_back(--b);
adj[b].push_back(a);
}
dfs(0,-1);
dfsr(0,-1, 0);
stringstream ss;
for (int i = 0; i < n; i++)
ss << (valid[i] ? 2*rans[i]-dans[i] : -1) << endl;
cout << ss.str();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
47352 KB |
Output is correct |
2 |
Correct |
44 ms |
47352 KB |
Output is correct |
3 |
Correct |
44 ms |
47352 KB |
Output is correct |
4 |
Correct |
44 ms |
47352 KB |
Output is correct |
5 |
Correct |
44 ms |
47352 KB |
Output is correct |
6 |
Correct |
45 ms |
47352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
47352 KB |
Output is correct |
2 |
Correct |
43 ms |
47352 KB |
Output is correct |
3 |
Correct |
42 ms |
47352 KB |
Output is correct |
4 |
Correct |
42 ms |
47376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
47520 KB |
Output is correct |
2 |
Correct |
45 ms |
47692 KB |
Output is correct |
3 |
Correct |
45 ms |
47736 KB |
Output is correct |
4 |
Correct |
44 ms |
47612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
49300 KB |
Output is correct |
2 |
Correct |
60 ms |
50012 KB |
Output is correct |
3 |
Correct |
61 ms |
50808 KB |
Output is correct |
4 |
Correct |
59 ms |
49472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
51192 KB |
Output is correct |
2 |
Correct |
77 ms |
52660 KB |
Output is correct |
3 |
Correct |
79 ms |
53880 KB |
Output is correct |
4 |
Correct |
78 ms |
51328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
56964 KB |
Output is correct |
2 |
Correct |
168 ms |
59040 KB |
Output is correct |
3 |
Correct |
190 ms |
61968 KB |
Output is correct |
4 |
Correct |
144 ms |
57328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
911 ms |
94300 KB |
Output is correct |
2 |
Correct |
840 ms |
111724 KB |
Output is correct |
3 |
Correct |
845 ms |
129256 KB |
Output is correct |
4 |
Correct |
765 ms |
94564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1568 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1504 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1562 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |