#include <bits/stdc++.h>
#define fi first
#define se second
#define lo long long
#define inf 1000000009
#define md 1000000007
#define li 1000005
#define mp make_pair
#define pb push_back
using namespace std;
lo int n,x,y,ok[li],sz[li],d[li],mx;
vector<lo int> v[li];
bool vis[li];
void dfs(int node,int ata){
sz[node]=0;
for(int i=0;i<(int)v[node].size();i++){
int go=v[node][i];
if(go==ata) continue;
dfs(go,node);
sz[node]+=sz[go]+1;
}
}
void dfs2(int node,int ata){
lo int flag=0,sum=0;
for(int i=0;i<(int)v[node].size();i++){
int go=v[node][i];
if(go==ata) continue;
sum+=sz[go]+1;
if(sz[go]+1>=(n+1)/2){
flag=1;
}
}
if(n-sum-1<(n+1)/2 && flag==0) ok[node]=1;
for(int i=0;i<(int)v[node].size();i++){
int go=v[node][i];
if(go==ata) continue;
dfs2(go,node);
}
}
lo int bfs(int st){
queue< pair<lo int,int> > q;
q.push(mp(0,st));
lo int genel=0;
while(!q.empty()){
pair<lo int,int> temp=q.front();
q.pop();
int seh=temp.se;
lo int cst=temp.fi;
if(vis[seh]) continue;
d[seh]=cst;
genel+=cst*2;
vis[seh]=true;
for(int i=0;i<(int)v[seh].size();i++){
int go=v[seh][i];
if(vis[go]==false) q.push(mp(cst+1,go));
}
}
return genel;
}
void dfs3(int node,int ata,lo int dep){
mx=max(mx,dep);
for(int i=0;i<(int)v[node].size();i++){
int go=v[node][i];
if(go==ata) continue;
dfs3(go,node,dep+1);
}
}
int main(){
scanf("%lld",&n);
for(int i=1;i<n;i++){
scanf("%lld %lld",&x,&y);
v[x].pb(y);
v[y].pb(x);
}
if(n==1){
printf("0\n");
return 0;
}
dfs(1,-1);
dfs2(1,-1);
lo int ggmx=0;
for(int i=1;i<=n;i++){
if(ok[i]==0) printf("-1\n");
else{
lo int topla=bfs(i);
dfs(i,-1);
lo int istek=0;
for(int j=0;j<(int)v[i].size();j++){
int git=v[i][j];
istek=max(istek,sz[git]+1);
}
for(int j=0;j<(int)v[i].size();j++){
int git=v[i][j];
if(istek!=sz[git]+1) continue;
mx=0;
dfs3(git,i,1);
ggmx=max(ggmx,mx);
}
printf("%lld\n",topla-ggmx);
}
}
return 0;
}
Compilation message
ins.cpp: In function 'int main()':
ins.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&n);
~~~~~^~~~~~~~~~~
ins.cpp:71:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld",&x,&y);
~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
23800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
23932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
23980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
24920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
82 ms |
25888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
90 ms |
28868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
594 ms |
48144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1292 ms |
72448 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1294 ms |
72556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1276 ms |
72556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |