Advertisement
i_love_rao_khushboo

D. Distance in Tree

Sep 10th, 2021
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.86 KB | None | 0 0
  1. // Problem: D. Distance in Tree
  2. // Contest: Codeforces - VK Cup 2012 Round 1
  3. // URL: https://codeforces.com/contest/161/problem/D
  4. // Memory Limit: 512 MB
  5. // Time Limit: 3000 ms
  6. // Parsed on: 10-09-2021 11:19:46 IST (UTC+05:30)
  7. // Author: Kapil Choudhary
  8. // ********************************************************************
  9. // कर्मण्येवाधिकारस्ते मा फलेषु कदाचन |
  10. // मा कर्मफलहेतुर्भूर्मा ते सङ्गोऽस्त्वकर्मणि || १.४७ ||
  11. // ********************************************************************
  12.  
  13. #include<bits/stdc++.h>
  14. using namespace std;
  15.  
  16. #define ll long long
  17. #define ld long double
  18. #define ull unsigned long long
  19. #define pb push_back
  20. #define ppb pop_back
  21. #define mp make_pair
  22. #define F first
  23. #define S second
  24. #define PI 3.1415926535897932384626
  25. #define sz(x) ((int)(x).size())
  26. #define vset(v, n, val) v.clear(); v.resize(n, val)
  27.  
  28. typedef pair<int, int> pii;
  29. typedef pair<ll, ll> pll;
  30. typedef vector<int> vi;
  31. typedef vector<ll> vll;
  32. typedef vector<ull> vull;
  33. typedef vector<bool> vb;
  34. typedef vector<char> vc;
  35. typedef vector<string> vs;
  36. typedef vector<pii> vpii;
  37. typedef vector<pll> vpll;
  38. typedef vector<vi> vvi;
  39. typedef vector<vll> vvll;
  40. typedef vector<vull> vvull;
  41. typedef vector<vb> vvb;
  42. typedef vector<vc> vvc;
  43. typedef vector<vs> vvs;
  44.  
  45. /************************************************** DEBUGGER *******************************************************************************************************/
  46.  
  47. #ifndef ONLINE_JUDGE
  48. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  49. #else
  50. #define debug(x)
  51. #endif
  52.  
  53. void _print(ll t) { cerr << t; }
  54. void _print(int t) { cerr << t; }
  55. void _print(string t) { cerr << t; }
  56. void _print(char t) { cerr << t; }
  57. void _print(ld t) { cerr << t; }
  58. void _print(double t) { cerr << t; }
  59. void _print(ull t) { cerr << t; }
  60.  
  61. template <class T, class V> void _print(pair <T, V> p);
  62. template <class T> void _print(vector <T> v);
  63. template <class T> void _print(vector <vector<T>> v);
  64. template <class T> void _print(set <T> v);
  65. template <class T, class V> void _print(map <T, V> v);
  66. template <class T> void _print(multiset <T> v);
  67. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  68. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  69. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  70. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  71. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  72. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  73.  
  74. /*******************************************************************************************************************************************************************/
  75.  
  76. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  77. int rng(int lim) {
  78.     uniform_int_distribution<int> uid(0,lim-1);
  79.     return uid(rang);
  80. }
  81.  
  82. const int INF = 0x3f3f3f3f;
  83. const int mod = 1e9+7;
  84.  
  85. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  86.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  87.                          
  88. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  89. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  90.  
  91. /******************************************************************************************************************************/
  92.  
  93. vvi g;
  94.  
  95. // NOTE: this dp[][] will not calculate the actual result, but will help us
  96. //       in reaching the overall answer.
  97.  
  98. // dp[i][k] = #nodes (in the subtree rooted at node i) which are exactly at
  99. //            distance k from i
  100. int dp[50001][501];
  101.  
  102. // #vertices, k value
  103. int n, k;
  104.  
  105. // final result
  106. ll res;
  107.  
  108. void dfs(int cur, int par) {
  109.     // base case (not only for leaves but for all nodes)
  110.     // the node at distance 0 will be the node itself
  111.     dp[cur][0] = 1;
  112.    
  113.     // calculate dp[][] values for all the children of cur node
  114.     for(auto x: g[cur]) {
  115.         if(x == par) continue;
  116.         dfs(x, cur);
  117.     }
  118.    
  119.     // now, compute dp[][] values of cur node with help of it's children
  120.     for(auto x: g[cur]) {
  121.         if(x == par) continue;
  122.         for(int d = 1; d <= k; d++) {
  123.             dp[cur][d] += dp[x][d-1];
  124.         }
  125.     }
  126.    
  127.     // helper code to find overall result
  128.     res += dp[cur][k];
  129.     ll tmp = 0LL;
  130.    
  131.     for(auto x: g[cur]) {
  132.         if(x == par) continue;
  133.         for(int d = 1; d <= (k - 1); d++) {
  134.             tmp += (dp[x][d - 1] * (dp[cur][k - d] - dp[x][k - d - 1]));
  135.         }
  136.     }
  137.    
  138.     res += (tmp / 2LL);
  139. }
  140.  
  141. // https://www.youtube.com/watch?v=de2NZ2maFO0&list=PLzVLIdIx9dQxCKaiktxELrtXtnItgAAIr&index=9
  142. void solve()
  143. {
  144.     cin >> n >> k;
  145.    
  146.     vset(g, n + 1, vi(0));
  147.     memset(dp, 0, sizeof dp);
  148.     res = 0LL;
  149.    
  150.     for(int i = 1; i < n; i++) {
  151.         int x, y; cin >> x >> y;
  152.         g[x].pb(y);
  153.         g[y].pb(x);
  154.     }
  155.    
  156.     dfs(1, 0);
  157.    
  158.     cout << res << "\n";
  159. }
  160.  
  161. int main()
  162. {
  163.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  164.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  165.  
  166.     // #ifndef ONLINE_JUDGE
  167.     //     freopen("input.txt", "r", stdin);
  168.     //     freopen("output.txt", "w", stdout);
  169.     // #endif
  170.    
  171.     // #ifndef ONLINE_JUDGE
  172.     //      freopen("error.txt", "w", stderr);
  173.     // #endif
  174.    
  175.     int t = 1;
  176.     // int test = 1;
  177.     // cin >> t;
  178.     while(t--) {
  179.         // cout << "Case #" << test++ << ": ";
  180.         solve();
  181.     }
  182.  
  183.     return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement