1 solutions
-
0
C++ :
#include <bits/stdc++.h> using namespace std; const int N=2e5+5; bool st[N]; int e[N<<1],ne[N<<1],h[N],idx; int n,cnt; void add(int a,int b) { e[idx]=b,ne[idx]=h[a],h[a]=idx++; } void dfs(int u,int step) { if(st[u]) return; if(step%2 == 0) { st[u]=1; cnt++; } for(int i=h[u]; i!=-1; i=ne[i]) { int j=e[i]; dfs(j,step+1); } } int main() { cin>>n; memset(h,-1,sizeof h); for(int i=1; i<n; i++) { int a,b; cin>>a>>b; add(a,b), add(b,a); } dfs(1,0); for(int i=1; i<=n; i++) { if(st[i]) cout<<cnt<<" "; else cout<<n-cnt<<" "; } cout<<endl; return 0; } /* in: 3 1 3 2 3 out: 2 2 1 */
- 1
Information
- ID
- 109
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By