Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: Tree House
- // Contest: CodeChef - May Challenge 2021 Division 2 (Rated)
- // URL: https://www.codechef.com/MAY21B/problems/THOUSES
- // Memory Limit: 256 MB
- // Time Limit: 1000 ms
- // Parsed on: 09-05-2021 18:36:27 IST (UTC+05:30)
- // Author: Kapil Choudhary
- // ********************************************************************
- // कर्मण्येवाधिकारस्ते मा फलेषु कदाचन |
- // मा कर्मफलहेतुर्भूर्मा ते सङ्गोऽस्त्वकर्मणि || १.४७ ||
- // ********************************************************************
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define ull unsigned long long
- #define pb push_back
- #define mp make_pair
- #define F first
- #define S second
- #define PI 3.1415926535897932384626
- #define deb(x) cout << #x << "=" << x << endl
- #define deb2(x, y) cout << #x << "=" << x << ", " << #y << "=" << y << endl
- typedef pair<int, int> pii;
- typedef pair<ll, ll> pll;
- typedef vector<int> vi;
- typedef vector<ll> vll;
- typedef vector<ull> vull;
- typedef vector<pii> vpii;
- typedef vector<pll> vpll;
- typedef vector<vi> vvi;
- typedef vector<vll> vvll;
- typedef vector<vull> vvull;
- mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
- int rng(int lim) {
- uniform_int_distribution<int> uid(0,lim-1);
- return uid(rang);
- }
- const int INF = 0x3f3f3f3f;
- const ll mod = 1e9+7;
- const int N = 3e5;
- vvi g;
- vi cnt;
- vll val;
- vector<bool> vis;
- ll res;
- int n, x;
- void dfs(int cur, int par) {
- cnt[cur] += 1;
- for(auto child: g[cur]) {
- if(child != par) {
- dfs(child, cur);
- cnt[cur] += cnt[child];
- }
- }
- }
- void bfs() {
- queue<pair<int, ll>> q;
- q.push({1, x});
- vis[1] = 1;
- while(!q.empty()) {
- pair<int, ll> cur = q.front();
- q.pop();
- val[cur.F] = cur.S;
- vpii tmp;
- for(auto child: g[cur.F]) {
- if(!vis[child]) {
- vis[child] = 1;
- tmp.pb({cnt[child], child});
- }
- }
- sort(tmp.rbegin(), tmp.rend());
- for(int i = 0; i < (int)tmp.size(); i++) {
- ll value = cur.S * (i + 1);
- value %= mod;
- q.push({tmp[i].S, value});
- }
- }
- }
- void solve()
- {
- cin >> n >> x;
- g.clear(); g.resize(n + 1);
- cnt.clear(); cnt.resize(n + 1, 0);
- val.clear(); val.resize(n + 1, 0);
- vis.clear(); vis.resize(n + 1, 0);
- res = 0LL;
- for(int i = 0; i < n - 1; i++) {
- int u, v; cin >> u >> v;
- g[u].pb(v);
- g[v].pb(u);
- }
- dfs(1, 0);
- bfs();
- for(int i = 1; i <= n; i++) {
- res = (res + val[i]) % mod;
- }
- cout << res << "\n";
- }
- int main()
- {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- srand(chrono::high_resolution_clock::now().time_since_epoch().count());
- // #ifndef ONLINE_JUDGE
- // freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- // #endif
- int t = 1;
- // int test = 1;
- cin >> t;
- while(t--) {
- // cout << "Case #" << test++ << ": ";
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement