Advertisement
Guest User

Untitled

a guest
Oct 29th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.48 KB | None | 0 0
  1. /*
  2.     $$$$$$$\                      $$\           $$$$$$$\
  3.     $$  __$$\                     \__|          $$  __$$\
  4.     $$ |  $$ | $$$$$$\   $$$$$$\  $$\  $$$$$$$\ $$ |  $$ | $$$$$$\   $$$$$$\   $$$$$$$\ $$$$$$\
  5.     $$$$$$$\ |$$  __$$\ $$  __$$\ $$ |$$  _____|$$$$$$$\ | \____$$\ $$  __$$\ $$  _____|\____$$\
  6.     $$  __$$\ $$ /  $$ |$$ |  \__|$$ |\$$$$$$\  $$  __$$\  $$$$$$$ |$$ |  \__|$$ /      $$$$$$$ |
  7.     $$ |  $$ |$$ |  $$ |$$ |      $$ | \____$$\ $$ |  $$ |$$  __$$ |$$ |      $$ |     $$  __$$ |
  8.     $$$$$$$  |\$$$$$$  |$$ |      $$ |$$$$$$$  |$$$$$$$  |\$$$$$$$ |$$ |      \$$$$$$$\\$$$$$$$ |
  9.     \_______/  \______/ \__|      \__|\_______/ \_______/  \_______|\__|       \_______|\_______|
  10. */
  11. #include <bits/stdc++.h>
  12.  
  13. using namespace std;
  14.  
  15. #define PB push_back
  16. #define MP make_pair
  17. #define INS insert
  18. #define LB lower_bound
  19. #define UB upper_bound
  20. #define pii pair <int,int>
  21. #define pll pair <long long, long long>
  22. #define si pair<string, int>
  23. #define is pair<int, string>
  24. #define X first
  25. #define Y second
  26. #define _ << " " <<
  27. #define sz(x) (int)x.size()
  28. #define all(a) (a).begin(),(a).end()
  29. #define FOR(i, a, b) for (int i = (a); i < (b); ++i)
  30. #define FORD(i, a, b) for (int i = (a); i > (b); --i)
  31. #define FORR(i, l, r) for (int i = (l); i <= (r); ++i)
  32. #define FORP(i, a, b) for ((i) = (a); (i) < (b); ++i)
  33. #define FORA(i, x) for (auto &i : x)
  34. #define REP(i, n) FOR(i, 0, n)
  35. #define BITS(x) __builtin_popcount(x)
  36. #define MSET memset
  37. #define MCPY memcpy
  38. #define SQ(a) (a) * (a)
  39.  
  40. typedef long long ll;
  41. typedef long double ld;
  42. typedef vector <int> vi;
  43. typedef vector <pii> vpi;
  44. typedef vector <ll> vll;
  45. typedef vector <pll> vpl;
  46. typedef vector <double> vd;
  47. typedef vector <ld> vld;
  48. typedef vector<si> vsi;
  49. typedef vector<is> vis;
  50. typedef vector<string> vs;
  51. //((float) t)/CLOCKS_PER_SEC
  52.  
  53. const int MOD = 1e9 + 7;
  54. const int PI = acos(-1);
  55. const int LOG = 32;
  56. const int INF = 1e9 + 10;
  57. const ll INFL = 1e18 + 10;
  58. const int ABC = 30;
  59. const int dx[] = {-1, 1, 0, 0};
  60. const int dy[] = {0, 0, -1, 1};
  61. const int dox[] = {-1, 1, 0, 0, -1, -1, 1, 1};
  62. const int doy[] = {0, 0, -1, 1, -1, 1, -1, 1};
  63.  
  64. inline int sum(int a, int b){
  65.   if (a + b >= MOD)
  66.     return a + b - MOD;
  67.   if (a + b < 0)
  68.     return a + b + MOD;
  69.   return a + b;
  70. }
  71.  
  72. inline void add(int &a, int b){
  73.   a = sum(a, b);
  74. }
  75.  
  76. inline int mul(int a, int b){
  77.   return (ll)a * (ll)b % MOD;
  78. }
  79.  
  80. inline int sub(int a, int b){
  81.     return (a - b + MOD) % MOD;
  82. }
  83.  
  84. inline int pot(ll pot, int n){
  85.     ll ret = 1;
  86.     while (n){
  87.         if (n & 1)
  88.             ret = (ret * pot) % MOD;
  89.         pot = (pot * pot) % MOD;
  90.         n >>= 1;
  91.     }
  92.     return ret;
  93. }
  94.  
  95. inline int divide(int a, int b){
  96.     return mul(a, pot(b, MOD - 2));
  97. }
  98.  
  99. ll lcm(ll a, ll b){
  100.     return abs(a * b) / __gcd(a, b);
  101. }
  102.  
  103. inline double ccw(pii A, pii B, pii C){
  104.     return (A.X * B.Y) - (A.Y * B.X) + (B.X * C.Y) - (B.Y * C.X) + (C.X * A.Y) - (C.Y * A.X);
  105. }
  106.  
  107. inline int CCW(pii A, pii B, pii C){
  108.     double val = ccw(A, B, C);
  109.     double eps = max(max(abs(A.X), abs(A.Y)), max(max(abs(B.X), abs(B.Y)), max(abs(C.X), abs(C.Y)))) / 1e9;
  110.     if (val <= -eps)
  111.         return -1;
  112.     if (val >= eps)
  113.         return 1;
  114.     return 0;
  115. }
  116.  
  117. void to_upper(string &x){
  118.     REP(i, sz(x))
  119.         x[i] = toupper(x[i]);
  120. }
  121.  
  122. void to_lower(string &x){
  123.     REP(i, sz(x))
  124.         x[i] = tolower(x[i]);
  125. }
  126.  
  127. string its(ll x){
  128.     if (x == 0)
  129.         return "0";
  130.     string ret = "";
  131.     while (x > 0){
  132.         ret += (x % 10) + '0';
  133.         x /= 10;
  134.     }
  135.     reverse(all(ret));
  136.     return ret;
  137. }
  138.  
  139. ll sti(string s){
  140.     ll ret = 0;
  141.     REP(i, sz(s)){
  142.         ret *= 10;
  143.         ret += (s[i] - '0');
  144.     }
  145.     return ret;
  146. }
  147.  
  148. void yes(){
  149.     cout << "YES\n";
  150.     exit(0);
  151. }
  152.  
  153. void no(){
  154.     cout << "NO\n";
  155.     exit(0);
  156. }
  157.  
  158. const int N = 1e4 + 10;
  159.  
  160. int t, n, dp[2][N];
  161. vi e[N];
  162.  
  163. void dfs(int node, int parent){
  164.     if (sz(e[node]) == 1 && e[node][0] == parent){
  165.         dp[0][node] = 0;
  166.         dp[1][node] = 1;
  167.         return;
  168.     }
  169.     FORA(nxt, e[node]){
  170.         if (nxt == parent)
  171.             continue;
  172.         dfs(nxt, node);
  173.     }
  174.     int f = 0, s = 0;
  175.     FORA(nxt, e[node]){
  176.         if (nxt == parent)
  177.             continue;
  178.         f += dp[0][nxt];
  179.         s += dp[1][nxt];
  180.     }
  181.     if (sz(e[node]) > 2){
  182.         dp[0][node] = min(f, s) + 1;
  183.         dp[1][node] = min(f, s) + sz(e[node]) - 2;
  184.     } else {
  185.         dp[0][node] = min(f, s);
  186.         dp[1][node] = min(f, s);
  187.     }
  188. }
  189.  
  190. int main () {
  191.     ios_base::sync_with_stdio(false);
  192.     cin.tie(0);
  193.     cout.tie(0);
  194.  
  195.     cin >> t;
  196.     while (t--){
  197.         REP(i, N)
  198.             e[i].clear();
  199.         MSET(dp, 0, sizeof dp);
  200.         cin >> n;
  201.         REP(i, n - 1){
  202.             int u, v; cin >> u >> v;
  203.             e[u].PB(v);
  204.             e[v].PB(u);
  205.         }
  206.         dfs(1, -1);
  207.         cout << dp[0][1] + 1 << '\n';
  208.     }
  209.  
  210.     return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement