#include <bits/stdc++.h>
using namespace std;
const int MN = 1e6+5;
int N, i, x, y, sz[MN], zs[MN], md[MN], mx[MN], idx, cur;
vector<int> adj[MN];
long long ans[MN];
int dfs(int n,int p){
sz[n] = 1;
for(auto v : adj[n]){
if(v==p) continue;
sz[n] += dfs(v, n);
mx[n] = max(mx[n], sz[v]);
}
return sz[n];
}
void acc(int n,int p,int d,int r){
ans[idx] += 2*d; cur = max(cur, d);
md[r] = max(md[r], d);
zs[r] ++;
for(auto v : adj[n]){
if(v==p) continue;
acc(v, n, d+1, r);
}
}
void dfs2(int n,int p){
if(2*mx[n]<=N&&2*(N-sz[n])<=N){
idx = n, cur = 0;
int mx = 0, hm = 0;
for(auto v : adj[n]){
zs[v] = md[v] = 0;
acc(v, n, 1, v);
if(zs[v]>N-zs[v]-1){
hm = 1;
mx = md[v];
}
}
if(!hm) mx = max(mx, cur);
ans[n] -= mx;
}
else ans[n] = -1;
for(auto v : adj[n]){
if(v==p) continue;
dfs2(v, n);
}
}
int main(){
scanf("%d",&N);
for(i=1;i<N;i++){
scanf("%d%d",&x,&y);
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 0); dfs2(1, 0);
for(i=1;i<=N;i++)
printf("%lld\n",ans[i]);
return 0;
}
Compilation message
ins.cpp: In function 'int main()':
ins.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
52 | scanf("%d",&N);
| ~~~~~^~~~~~~~~
ins.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
54 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
23808 KB |
Output is correct |
3 |
Correct |
16 ms |
23808 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
20 ms |
23808 KB |
Output is correct |
6 |
Correct |
16 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
23936 KB |
Output is correct |
3 |
Correct |
16 ms |
23808 KB |
Output is correct |
4 |
Correct |
17 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23936 KB |
Output is correct |
2 |
Correct |
18 ms |
23936 KB |
Output is correct |
3 |
Correct |
17 ms |
24064 KB |
Output is correct |
4 |
Correct |
20 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
24960 KB |
Output is correct |
2 |
Correct |
31 ms |
25208 KB |
Output is correct |
3 |
Correct |
31 ms |
25472 KB |
Output is correct |
4 |
Correct |
29 ms |
24960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
25976 KB |
Output is correct |
2 |
Correct |
45 ms |
26616 KB |
Output is correct |
3 |
Correct |
44 ms |
27136 KB |
Output is correct |
4 |
Correct |
43 ms |
25848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
29108 KB |
Output is correct |
2 |
Correct |
117 ms |
31736 KB |
Output is correct |
3 |
Correct |
112 ms |
33016 KB |
Output is correct |
4 |
Correct |
86 ms |
29048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
816 ms |
49784 KB |
Output is correct |
2 |
Correct |
759 ms |
57392 KB |
Output is correct |
3 |
Correct |
711 ms |
64888 KB |
Output is correct |
4 |
Correct |
564 ms |
49288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1685 ms |
75096 KB |
Output is correct |
2 |
Correct |
1610 ms |
90520 KB |
Output is correct |
3 |
Correct |
1567 ms |
131072 KB |
Output is correct |
4 |
Correct |
1229 ms |
88020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1689 ms |
75128 KB |
Output is correct |
2 |
Correct |
1594 ms |
90720 KB |
Output is correct |
3 |
Correct |
1561 ms |
131072 KB |
Output is correct |
4 |
Correct |
1214 ms |
88056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1702 ms |
75128 KB |
Output is correct |
2 |
Correct |
1599 ms |
90616 KB |
Output is correct |
3 |
Correct |
1570 ms |
131072 KB |
Output is correct |
4 |
Correct |
1229 ms |
88312 KB |
Output is correct |