1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include <bits/stdc++.h> #include<stdint.h> #define int long long #define scan(n) scanf("%lld", &(n)) #define scann(n, m) scanf("%lld%lld", &(n), &(m)) #define scannn(a, b, c) scanf("%lld%lld%lld", &(a), &(b), &(c)) #define prin(n) printf("%lld", (n)) #define pb push_back #define mp make_pair #define ms(a) memset(a, 0, sizeof(a)) #define fo(i, a, b) for (int i = (a); i <= (b); i++) #define ro(i, a, b) for (int i = (a); i >= (b); i--) const int inf = 0x3f3f3f3f; using namespace std; const int maxn = 2e5+100; vector<int>e[maxn]; int siz[maxn]; int ans,n; int u,v; void getsize(int v,int u){ siz[v]=1; for(int x:e[v]){ if(x==u)continue; getsize(x,v); siz[v]+=siz[x]; } } void dfs(int v,int u,int t){ ans=max(ans,t); cout<<v<<" "<<u<<" "<<t<<endl; for(int x:e[v]){ if(x==u)continue; dfs(x,v,t-2*siz[x]+n); } } int32_t main() { scan(n); fo(i,1,n-1){ scann(u,v); e[u].pb(v);e[v].pb(u); } getsize(1,0); int t=0; fo(i,1,n)t+=siz[i]; dfs(1,0,t); printf("%lld",ans); return 0; }
|