#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){
memset(vis,false,sizeof(vis));
memset(d,0,sizeof(d));
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);
mx=0;
dfs3(git,i,1);
ggmx=max(ggmx,mx);
}
for(int j=0;j<(int)v[i].size();j++){
int git=v[i][j];
if(sz[git]+1==(n+1)/2) {mx=0;dfs3(git,i,1);ggmx=mx;}
//mx=0;
//dfs3(git,i,1);
//ggmx=max(ggmx,d[git]);
}
printf("%lld\n",topla-ggmx);
}
}
return 0;
}
Compilation message
ins.cpp: In function 'int main()':
ins.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&n);
~~~~~^~~~~~~~~~~
ins.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld",&x,&y);
~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
23924 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
24000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
24916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
47 ms |
25860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
89 ms |
28824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
604 ms |
48180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1307 ms |
72328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1299 ms |
72328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1295 ms |
72428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |