Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <fstream>
  4. #include <queue>
  5. #include <iomanip>
  6. #include <cstdio>
  7. #include <string>
  8. #include <cmath>
  9. #include <algorithm>
  10. #include <vector>
  11. #include <time.h>
  12. #include <cassert>
  13. #include <map>
  14. #include <set>
  15. #include <stack>
  16. #include <time.h>
  17. #include <cstdlib>
  18. #include <cstring>
  19. #include <string.h>
  20. #include <bitset>
  21. #include <ctype.h>
  22.  
  23. #include <complex>
  24.  
  25. //#include <unordered_map>
  26. //#include <unordered_set>
  27.  
  28. #define llong long long int
  29. #define pb push_back
  30. #define mp make_pair
  31. #define pr pair <int, int>
  32. #define X first
  33. #define Y second
  34. #define endl "\n"
  35. using namespace std;
  36. const int MAXN = 1e6 + 50;
  37. const int INF = 1e9 + 7;
  38. //const llong LINF = 1e18;
  39. const int MOD = 1e9 + 7;
  40.  
  41. int n, d;
  42. vector<int> a[MAXN];
  43. int depth[MAXN];
  44. int component[MAXN];
  45. int get(int x) {
  46.     if (component[x] == x) {
  47.         return x;
  48.     }
  49.     return component[x] = get(component[x]);
  50. }
  51. inline void do_uninon(int x, int y) {
  52.     x = get(x);
  53.     y = get(y);
  54.     if (x != y)
  55.         component[x] = y;
  56. }
  57. int getdiameter(int x, int p = -1) {
  58.     if (p != -1) {
  59.         depth[x] = depth[p] + 1;
  60.     } else {
  61.         depth[x] = 0;
  62.     }
  63.     int cur = x;
  64.     for (auto i: a[x]) {
  65.         if (i != p) {
  66.             int tmp = getdiameter(i, x);
  67.             if (depth[tmp] > depth[cur]) {
  68.                 cur = tmp;
  69.             }
  70.         }
  71.     }
  72.     return cur;
  73. }
  74. int level[MAXN], whoonthislevel[MAXN];
  75. int mx[MAXN];
  76. vector<int> v[MAXN];
  77.  
  78. void dfs(int x, int p = -1) {
  79.     v[x].clear();
  80.     if (p == -1) {
  81.         level[x] = 0;
  82.     } else {
  83.         level[x] = level[p] + 1;
  84.     }
  85.     whoonthislevel[level[x]] = x;
  86.     if (level[x] >= d) {
  87.         do_uninon(x, whoonthislevel[level[x] - d]);
  88.     }
  89.     mx[x] = level[x];
  90.    
  91.     for (auto i: a[x]) {
  92.         if (i == p) {
  93.             continue;
  94.         }
  95.         dfs(i, x);
  96.         if (v[x].size() < v[i].size()) {
  97.             swap(v[x], v[i]);
  98.             swap(mx[x], mx[i]);
  99.         }
  100.         for (auto j: v[i]) {
  101.             int c = level[j] - level[x];
  102.             if (c < d && level[x] >= c && mx[x] - level[x] >= d - c) {
  103.                 do_uninon(j, whoonthislevel[level[x] - c]);
  104.             }
  105.             v[x].pb(j);
  106.         }
  107.         mx[x] = max(mx[x], mx[i]);
  108.     }
  109.     v[x].pb(x);
  110.    
  111. }
  112. int main() {
  113. #ifdef DEBUG
  114.     double beg = clock();
  115.     freopen("input.txt", "r", stdin);
  116.     freopen("output.txt", "w", stdout);
  117. #else
  118.     //freopen("sump.in", "r", stdin);
  119.     //freopen("sump.out", "w", stdout);
  120. #endif
  121.     //ios_base::sync_with_stdio(0);cin.tie();
  122.     scanf("%d%d", &n, &d);
  123.     for (int i = 1; i < n; i++) {
  124.         int x, y;
  125.         scanf("%d%d", &x, &y);
  126.         a[x].pb(y);
  127.         a[y].pb(x);
  128.     }
  129.     for (int i = 1; i <= n; i++) {
  130.         component[i] = i;
  131.     }
  132.     int D1 = getdiameter(1);
  133.     int D2 = getdiameter(D1);
  134.     dfs(1);
  135.     dfs(D1);
  136.     dfs(D2);
  137.     int ans = 0;
  138.     for (int i = 1; i <= n; i++) {
  139.         int x = get(i);
  140.        
  141.         if (x == i) {
  142.             ans++;
  143.         }
  144.     }
  145.     cout << ans;
  146. #ifdef DEBUG
  147.     double end = clock();
  148.     fprintf(stderr, "\n*** Total time = %.3lf ***\n", (end - beg) / CLOCKS_PER_SEC);
  149. #endif
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement