fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define endl "\n"
  4. #define int long long
  5. #define faster() ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  6. const int MOD = 1e9 + 7 ;
  7.  
  8. int n , m , s , t;
  9. vector<int> ke[1005];
  10. bool visited[1005] ;
  11. int parent[1005];
  12. int sz[1005];
  13. int dfs(int u ){
  14. visited[u] = true ;
  15. int res = 1 ;
  16. for(int v : ke[u]){
  17. if(!visited[v]){
  18. res += dfs(v);
  19. }
  20. }
  21. return sz[u] = res ;
  22. }
  23.  
  24.  
  25. void solve(){
  26. cin >> n ;
  27. for(int i = 1 ; i <= n - 1 ; i++){
  28. int x , y ; cin >> x >> y ;
  29. ke[x].push_back(y);
  30. ke[y].push_back(x);
  31. }
  32. dfs(1);
  33. int sum = 0 ;
  34. for(int i = 1 ; i <= n ; i++){
  35. sum += sz[i];
  36. }
  37. cout << sum << endl;
  38. }
  39.  
  40. signed main() {
  41. faster();
  42. int test = 1 ;
  43. // cin >> test ;
  44. while(test--) solve();
  45. return 0;
  46. }
  47.  
  48.  
  49.  
Success #stdin #stdout 0.01s 5284KB
stdin
10
9 10
5 9
7 4
4 5
2 3
6 7
7 8
3 6
1 2
stdout
51