fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const long long MaxN = 1e5 + 5;
  4. long long n, dp[MaxN];
  5. vector<long long> a[MaxN];
  6. void dfs(long long u, long long par)
  7. {
  8. for (long long v : a[u])
  9. {
  10. if(v==par) continue;
  11. dfs(v,u);
  12. dp[u]+=dp[v];
  13. }
  14. }
  15. void input()
  16. {
  17. cin >> n;
  18. for (long long i=1; i<n ;i++)
  19. {
  20. long long u,v;
  21. cin >> u >> v;
  22. a[u].push_back(v);
  23. a[v].push_back(u);
  24. }
  25. }
  26. void solve()
  27. {
  28. for (long long i=1; i<=n ;i++)
  29. {
  30. dp[i]=1;
  31. }
  32. dfs(1,-1);
  33. }
  34. int main()
  35. {
  36. ios_base::sync_with_stdio(0);
  37. cin.tie(0);
  38. input();
  39. solve();
  40. }
  41.  
Success #stdin #stdout 0.01s 6056KB
stdin
Standard input is empty
stdout
Standard output is empty