File size: 2,350 Bytes
c4b0eef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
/*
****************************************************************
****************************************************************
-> Coded by Stavros Chryselis
-> Visit my github for more solved problems over multiple sites
-> https://github.com/StavrosChryselis
-> Feel free to email me at [email protected]
****************************************************************
****************************************************************
*/
#include <stdio.h>
#include <vector>
#include <deque>
#include <ctype.h>
#define MOD 1000000007
#define gc() getchar_unlocked()
using namespace std;
vector< vector< int > > TREE;
int BASE[100007], STEP[100007];
bool V[100007];
int N, Q;
inline int FAST_IO()
{
char ch;
int val = 0;
ch = gc();
while (isspace(ch) && ch != EOF)
ch = gc();
val = 0;
while (isdigit(ch))
{
val = (val * 10) + (ch - '0');
ch = gc();
}
return val;
}
inline void init()
{
int i, f, s, u, base, step;
//scanf("%d", &N);
N = FAST_IO();
TREE.resize(N + 1);
for (i = 0; i < N - 1; i++)
{
//scanf("%d %d", &f, &s);
f = FAST_IO();
s = FAST_IO();
TREE[f].push_back(s);
TREE[s].push_back(f);
}
//scanf("%d", &Q);
Q = FAST_IO();
for (i = 0; i < Q; i++)
{
//scanf("%d %d %d", &u, &base, &step);
u = FAST_IO();
base = FAST_IO();
step = FAST_IO();
BASE[u] += base;
if (BASE[u] >= MOD)
BASE[u] -= MOD;
STEP[u] += step;
if (STEP[u] >= MOD)
STEP[u] -= MOD;
}
}
inline void BFS()
{
deque< pair<int, int> > Q; //node, parent
pair<int, int> curr;
int i;
V[1] = 1;
Q.push_back(make_pair(1, 0));
while (!Q.empty())
{
curr = Q.front();
Q.pop_front();
STEP[curr.first] += STEP[curr.second];
if (STEP[curr.first] >= MOD)
STEP[curr.first] -= MOD;
BASE[curr.first] += BASE[curr.second];
if (BASE[curr.first] >= MOD)
BASE[curr.first] -= MOD;
BASE[curr.first] += STEP[curr.second];
if (BASE[curr.first] >= MOD)
BASE[curr.first] -= MOD;
for (i = 0; i < TREE[curr.first].size(); i++)
if (!V[TREE[curr.first][i]])
{
V[TREE[curr.first][i]] = 1;
Q.push_back(make_pair(TREE[curr.first][i], curr.first));
}
}
}
inline void print()
{
int i;
for (i = 1; i <= N; i++)
printf("%d\n", BASE[i]);
}
int main()
{
//freopen("input.txt", "r", stdin);
init();
BFS();
print();
return 0;
}
|