rahulpadhy

ADDAP SPOJ

Jun 17th, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int mod = 1e9 + 7;
  5. int arr[100003];
  6. vector < vector <int> > graph;
  7.  
  8. void bfs(int src, int x, int y)
  9. {
  10.     int i, val, d = 0;
  11.     queue <int> q;
  12.     q.push(src);
  13.     q.push(-1);
  14.     while(! q.empty())
  15.     {
  16.         val = q.front();
  17.         q.pop();
  18.         if(val == -1)
  19.         {
  20.             d ++;
  21.             if(! q.empty())
  22.                 q.push(-1);
  23.         }
  24.         else
  25.         {
  26.             arr[val] += (x + d * y) % mod;
  27.             for(i = 0; i < graph[val].size(); i ++)
  28.                 q.push(graph[val][i]);
  29.         }
  30.     }
  31. }
  32.  
  33. int main()
  34. {
  35.     ios::sync_with_stdio(0);
  36.     int i, n, u, v, que, x, y;
  37.     cin >> n;
  38.     graph.resize(n + 1);
  39.     for(i = 1; i < n; i ++)
  40.     {
  41.         cin >> u >> v;
  42.         graph[u].push_back(v);
  43.     }
  44.     cin >> que;
  45.     while(que --)
  46.     {
  47.         cin >> u >> x >> y;
  48.         bfs(u, x, y);
  49.     }
  50.     for(i = 1; i <= n; i ++)
  51.         cout << arr[i] << '\n';
  52.     return 0;
  53. }
Add Comment
Please, Sign In to add comment