Advertisement
i_love_rao_khushboo

THOUSES

May 9th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. // Problem: Tree House
  2. // Contest: CodeChef - May Challenge 2021 Division 2 (Rated)
  3. // URL: https://www.codechef.com/MAY21B/problems/THOUSES
  4. // Memory Limit: 256 MB
  5. // Time Limit: 1000 ms
  6. // Parsed on: 09-05-2021 18:36:27 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 ull unsigned long long
  18. #define pb push_back
  19. #define mp make_pair
  20. #define F first
  21. #define S second
  22. #define PI 3.1415926535897932384626
  23. #define deb(x) cout << #x << "=" << x << endl
  24. #define deb2(x, y) cout << #x << "=" << x << ", " << #y << "=" << y << endl
  25. typedef pair<int, int> pii;
  26. typedef pair<ll, ll> pll;
  27. typedef vector<int> vi;
  28. typedef vector<ll> vll;
  29. typedef vector<ull> vull;
  30. typedef vector<pii> vpii;
  31. typedef vector<pll> vpll;
  32. typedef vector<vi> vvi;
  33. typedef vector<vll> vvll;
  34. typedef vector<vull> vvull;
  35. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  36. int rng(int lim) {
  37.     uniform_int_distribution<int> uid(0,lim-1);
  38.     return uid(rang);
  39. }
  40.  
  41. const int INF = 0x3f3f3f3f;
  42. const ll mod = 1e9+7;
  43. const int N = 3e5;
  44.  
  45. vvi g;
  46. vi cnt;
  47. vll val;
  48. vector<bool> vis;
  49. ll res;
  50. int n, x;
  51.  
  52. void dfs(int cur, int par) {
  53.     cnt[cur] += 1;
  54.     for(auto child: g[cur]) {
  55.         if(child != par) {
  56.             dfs(child, cur);
  57.             cnt[cur] += cnt[child];
  58.         }
  59.     }
  60. }
  61.  
  62. void bfs() {
  63.     queue<pair<int, ll>> q;
  64.     q.push({1, x});
  65.     vis[1] = 1;
  66.    
  67.     while(!q.empty()) {
  68.         pair<int, ll> cur = q.front();
  69.         q.pop();
  70.         val[cur.F] = cur.S;
  71.        
  72.         vpii tmp;
  73.         for(auto child: g[cur.F]) {
  74.             if(!vis[child]) {
  75.                 vis[child] = 1;
  76.                 tmp.pb({cnt[child], child});
  77.             }
  78.         }
  79.        
  80.         sort(tmp.rbegin(), tmp.rend());
  81.         for(int i = 0; i < (int)tmp.size(); i++) {
  82.             ll value = cur.S * (i + 1);
  83.             value %= mod;
  84.             q.push({tmp[i].S, value});
  85.         }
  86.     }
  87. }
  88.  
  89. void solve()
  90. {  
  91.     cin >> n >> x;
  92.     g.clear(); g.resize(n + 1);
  93.     cnt.clear(); cnt.resize(n + 1, 0);
  94.     val.clear(); val.resize(n + 1, 0);
  95.     vis.clear(); vis.resize(n + 1, 0);
  96.     res = 0LL;
  97.    
  98.     for(int i = 0; i < n - 1; i++) {
  99.         int u, v; cin >> u >> v;
  100.         g[u].pb(v);
  101.         g[v].pb(u);
  102.     }
  103.    
  104.     dfs(1, 0);     
  105.     bfs();
  106.    
  107.     for(int i = 1; i <= n; i++) {
  108.         res = (res + val[i]) % mod;
  109.     }
  110.    
  111.     cout << res << "\n";
  112. }
  113.  
  114. int main()
  115. {
  116.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  117.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  118.  
  119.     // #ifndef ONLINE_JUDGE
  120.     //     freopen("input.txt", "r", stdin);
  121.     //     freopen("output.txt", "w", stdout);
  122.     // #endif
  123.  
  124.     int t = 1;
  125.     // int test = 1;
  126.     cin >> t;
  127.     while(t--) {
  128.       // cout << "Case #" << test++ << ": ";
  129.       solve();
  130.     }
  131.  
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement