Advertisement
pratiyush7

CodeAgon Problem 2

Sep 28th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #define ll long long int
  2.  
  3. ll mod = 1000000007;
  4. ll ans = 2;
  5.  
  6. void bfs(vector<vector<ll>> &adj,int k)
  7. {
  8.     int n = adj.size()-1;
  9.     vector<bool> vis(n+1,false);
  10.     queue<ll> q;
  11.     q.push(1);
  12.     unordered_map<ll,ll> m;
  13.     vis[1] = true;
  14.     while(!q.empty())
  15.     {
  16.         int curr = q.front();
  17.         q.pop();
  18.         for(int x:adj[curr])
  19.         {
  20.             if(vis[x])
  21.                 continue;
  22.             m[x] = m[curr] + 1;
  23.             vis[x] = true;
  24.             q.push(x);
  25.             if(m[x]>=k)
  26.                 continue;
  27.             ans = (ans*2ll)%mod;
  28.         }
  29.     }
  30. }
  31.  
  32.  
  33.  
  34. int Solution::solve(vector<vector<int> > &edge, int k) {
  35.     int n = edge.size()+1;
  36.     vector<vector<ll>> adj(n+1);
  37.     for(vector<int> v:edge)
  38.     {
  39.         adj[v[0]].push_back(v[1]);
  40.         adj[v[1]].push_back(v[0]);
  41.     }
  42.     bfs(adj,k);
  43.     return ans;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement