Advertisement
_rashed

SPOJ PT07X

Jul 26th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int OO = 1e9;
  6. const double EPS = 1e-9;
  7.  
  8. int n;
  9. vector<vector<int>> graph;
  10.  
  11. int mem[1000000][2];
  12.  
  13. int solve(int idx, int rem, int p) {
  14.     if(mem[idx][rem] != -1) {
  15.         return mem[idx][rem];
  16.     }
  17.     int &ret = mem[idx][rem];
  18.     ret = rem;
  19.     for(int e : graph[idx]) {
  20.         if(e != p)
  21.             if(rem)
  22.                 ret += min(solve(e,0,idx),solve(e,1,idx));
  23.             else
  24.                 ret += solve(e,1,idx);
  25.     }
  26.     return ret;
  27. }
  28.  
  29. int main()
  30. {
  31.     ios_base::sync_with_stdio(NULL);
  32.     cin.tie(0);
  33.     cout.tie(0);
  34.     cin >> n;
  35.     memset(mem,-1,sizeof(mem));
  36.     graph.resize(n);
  37.     for(int i = 0; i < n-1; i++) {
  38.         int a,b;
  39.         cin >> a >> b;
  40.         a--,b--;
  41.         graph[a].push_back(b);
  42.         graph[b].push_back(a);
  43.     }
  44.     cout << min(solve(0,0,-1),solve(0,1,-1)) << "\n";
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement