#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;
int n,x,y,ok[li],sz[li],d[li],mx;
vector<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){
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);
}
}
int bfs(int st){
queue< pair<int,int> > q;
q.push(mp(0,st));
int genel=0;
while(!q.empty()){
pair<int,int> temp=q.front();
q.pop();
int seh=temp.se;
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,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("%d",&n);
for(int i=1;i<n;i++){
scanf("%d %d",&x,&y);
v[x].pb(y);
v[y].pb(x);
}
dfs(1,-1);
dfs2(1,-1);
int ggmx=0;
for(int i=1;i<=n;i++){
if(ok[i]==0) printf("-1\n");
else{
int topla=bfs(i);
dfs(i,-1);
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("%d\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("%d",&n);
~~~~~^~~~~~~~~
ins.cpp:71:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&x,&y);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
23928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
24092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
24228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
34 ms |
25048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
45 ms |
26432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
81 ms |
30068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
566 ms |
52404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1295 ms |
85356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1332 ms |
98880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1348 ms |
112492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |