Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2017
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <cstring>
  6. #include <deque>
  7. #include <stack>
  8. #include <stdio.h>
  9. #include <map>
  10. #include <set>
  11. #include <time.h>
  12. #include <string>
  13. #include <fstream>
  14. #include <queue>
  15. #include <bitset>
  16. #include <cstdlib>
  17. #include <assert.h>
  18. #define X first
  19. #define Y second
  20. #define mp make_pair
  21. #define pb push_back
  22. #define pdd pair<double,double>
  23. #define pii pair<ll,ll>
  24. #define PI 3.14159265358979323846
  25. #define MOD 1000000007
  26. #define MOD2 1000000009
  27. #define INF ((ll)1e+18)
  28. #define x1 fldgjdflgjhrthrl
  29. #define x2 fldgjdflgrtyrtyjl
  30. #define y1 fldggfhfghjdflgjl
  31. #define y2 ffgfldgjdflgjl
  32. #define N 100500
  33. #define SUM 23423
  34. #define MAG 10678787
  35. #define OPEN 0
  36. #define CLOSE 1
  37. typedef long long ll;
  38. typedef long double ld;
  39. using namespace std;
  40. ll i,j,n,k,l,m,tot, flag,r,z, K,x1,y1,x2,y2,x3,y3,x,y,h,num,h2,timer,cur_x,cur_y;
  41. vector<ll> g[100500];
  42. vector<ll> f;
  43. set<vector<ll> > ff;
  44. ll a[100500], b[100500], c[100500];
  45. void dfs(ll v, ll lvl, ll p)
  46. {
  47. a[v] = 1;
  48. if (g[v].size() <= 1)
  49. c[v] = 1;
  50. if (lvl == m)
  51. {
  52. c[v] = 1;
  53. return;
  54. }
  55. ll k = 0;
  56. for (int i = 0; i < g[v].size(); i++)
  57. {
  58. int to = g[v][i];
  59. if (to != p)
  60. {
  61.  
  62. k++;
  63. dfs(to, lvl+1, v);
  64. }
  65. }
  66. if (k == 0)
  67. c[v] = 1;
  68. }
  69. void magical_dfs(ll v, ll lvl, ll p)
  70. {
  71. if (c[v] == 0)
  72. c[v] = lvl;
  73. else
  74. c[v] = min(c[v], lvl);
  75. for (int i = 0; i < g[v].size(); i++)
  76. {
  77. int to = g[v][i];
  78. if (to != p && a[to] && (c[to] > lvl+1 || c[to] == 0))
  79. {
  80. magical_dfs(to, lvl+1, v);
  81. }
  82. }
  83. }
  84. ll dfs_hash(ll v, ll p = -1)
  85. {
  86. ll sz = 0;
  87. ll hash = 2347;
  88. ll m = g[v].size();
  89. ll b[m+1];
  90. for (int i = 0; i < g[v].size(); i++)
  91. {
  92. ll to = g[v][i];
  93. if (to != p && a[to])
  94. b[sz++] = dfs_hash(to, v)*533773;
  95. }
  96. sort(b, b+sz);
  97. for (int i = 0; i < sz; i++)
  98. hash = (hash*MAG+b[i]);
  99. return hash;
  100. }
  101. int main() {
  102. //freopen("input.txt","r",stdin);
  103. //freopen("output.txt","w",stdout);
  104. cin >> n >> m;
  105. if (m == 0)
  106. {
  107. cout << 1 << endl;
  108. return 0;
  109. }
  110. for (i = 0; i < n-1; i++)
  111. {
  112. cin >> x >> y;
  113. g[x].push_back(y);
  114. g[y].push_back(x);
  115. }
  116. for (i = 1; i <= n; i++)
  117. {
  118. for (j = 1; j <= n; j++)
  119. a[j] = c[j] = 0;
  120. dfs(i, 0, -1);
  121. f.clear();
  122. for (j = 1; j <= n; j++)
  123. if (c[j] == 1)
  124. magical_dfs(j, 1, -1);
  125. ll max1 = 0;
  126. for (j = 1; j <= n; j++)
  127. max1 = max(max1, c[j]);
  128. for (j = 1; j <= n; j++)
  129. if (c[j] == max1)
  130. {
  131. f.push_back(dfs_hash(j));
  132. }
  133. sort(f.begin(), f.end());
  134. ff.insert(f);
  135. }
  136. cout << ff.size() << endl;
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement