#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];
vector<int> adj[MN], dis;
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){
dis.push_back(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){
dis.clear();
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];
}
}
for(auto v : dis){
ans[n] += 2*v;
if(!hm) mx = max(mx, v);
}
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:55:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
55 | scanf("%d",&N);
| ~~~~~^~~~~~~~~
ins.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
57 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
23808 KB |
Output is correct |
3 |
Correct |
17 ms |
23808 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
16 ms |
23808 KB |
Output is correct |
6 |
Correct |
16 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23936 KB |
Output is correct |
2 |
Correct |
16 ms |
23808 KB |
Output is correct |
3 |
Correct |
17 ms |
23808 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24064 KB |
Output is correct |
2 |
Correct |
18 ms |
24064 KB |
Output is correct |
3 |
Correct |
20 ms |
24064 KB |
Output is correct |
4 |
Correct |
18 ms |
24064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
25216 KB |
Output is correct |
2 |
Correct |
33 ms |
25592 KB |
Output is correct |
3 |
Correct |
31 ms |
26232 KB |
Output is correct |
4 |
Correct |
29 ms |
25344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
26232 KB |
Output is correct |
2 |
Correct |
46 ms |
27812 KB |
Output is correct |
3 |
Correct |
49 ms |
28536 KB |
Output is correct |
4 |
Correct |
43 ms |
26744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
29684 KB |
Output is correct |
2 |
Correct |
106 ms |
33772 KB |
Output is correct |
3 |
Correct |
104 ms |
35184 KB |
Output is correct |
4 |
Correct |
90 ms |
30708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
793 ms |
51640 KB |
Output is correct |
2 |
Correct |
771 ms |
63336 KB |
Output is correct |
3 |
Correct |
735 ms |
81380 KB |
Output is correct |
4 |
Correct |
583 ms |
58268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1710 ms |
79196 KB |
Output is correct |
2 |
Correct |
1663 ms |
115932 KB |
Output is correct |
3 |
Runtime error |
1076 ms |
131076 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1690 ms |
79196 KB |
Output is correct |
2 |
Correct |
1616 ms |
116060 KB |
Output is correct |
3 |
Runtime error |
1064 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1713 ms |
79268 KB |
Output is correct |
2 |
Correct |
1624 ms |
115932 KB |
Output is correct |
3 |
Runtime error |
1066 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |