之前写过一篇博客,讲的是怎样把树区间化表示,https://mp.csdn.net/postedit/88614894
于是遇到这题:https://codeforces.com/problemset/problem/1076/E
也开始像这么想,但是还是有问题,像前一题,由于是针对一个子树的,所以可以根据根据先/中/后序遍历,
把一个子树区间化表示出来,
(后来还想到,先/中/后序遍历都可以表示一棵子树,其中先序遍历可以表示?,中序遍历可以表示大小关系,;
除此之外,还可以使用层次遍历)
但是这题不太一样,题目要的是结点x的k层以内的树,这个不太好区间化,于是就没有办法了。
。。。
然而我们还有dfs,所以处理以下就好了
//Problem:
//Date:
//Skill:
//Bug:
/////////////////////////////////////////Definations/////////////////////////////////////////////////
//循环控制
#define CLR(a) memset((a),0,sizeof(a))
#define F(i,a,b) for(int i=a;i<=int(b);++i)
#define F2(i,a,b) for(int i=a;i>=int(b);--i)
#define RE(i,n) for(int i=0;i<int(n);i++)
#define RE2(i,n) for(int i=1;i<=int(n);i++)
//输入输出
//#define INC(c) do{scanf("%c",&c);}while(isspace(c))
//#define ON cout<<endl
#define PII pair<int,int>
#define fi first
#define se second
using namespace std;
const int inf = 0x3f3f3f3f;
const long long llinf = 0x3f3f3f3f3f3f3f3f;
////////////////////////////////////////Options//////////////////////////////////////////////////////
typedef long long ll;
#define stdcpph
#define CPP_IO
#ifdef stdcpph
#include<bits/stdc++.h>
#else
#include<ctype.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<algorithm>
#include<functional>
#ifdef CPP_IO
#include<iostream>
#include<iomanip>
#include<string>
#else
#include<stdio.h>
#endif
#endif
////////////////////////////////////////Basic Functions//////////////////////////////////////////////
template<typename INint>
inline void IN(INint &x)
{
x = 0; int f = 1; char c; cin.get(c);
while (c<'0' || c>'9') { if (c == '-')f = -1; cin.get(c); }
while (c >= '0'&&c <= '9') { x = x * 10 + c - '0'; cin.get(c); }
x *= f;
}
template<typename INint>
inline void OUT(INint x)
{
if (x > 9)OUT(x / 10); cout.put(x % 10 + '0');
}
////////////////////////////////////////Added Functions//////////////////////////////////////////////
const int maxn = int(3e5 + 5);
struct q
{
int dep;
int v;
};
vector<q>qs[maxn];
int n, nq;
vector<int>G[maxn];
ll inc[maxn];
ll res[maxn];
void dfs(int u, int p, int h, ll sum)
{
if (u == 5)
u = u;
for (auto &x : qs[u])
{
inc[h] += x.v;
inc[min(n+1, h + x.dep + 1)] -= x.v;
}
sum += inc[h];
res[u] = sum;
for (auto &x : G[u])if (x != p)
{
dfs(x, u, h + 1, sum);
}
for (auto &x : qs[u])
{
inc[h] -= x.v;
inc[min(n+1, h + x.dep + 1)] += x.v;
}
}
////////////////////////////////////////////Code/////////////////////////////////////////////////////
int main()
{
//freopen("C:\\Users\\VULCAN\\Desktop\\data.in", "r", stdin);
int T(1), times(0);
#ifdef CPP_IO// CPP_IO
std::ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
//cin >> T;
#else
//IN(T);
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////
while (++times, T--)
{
cin >> n;
RE2(i, n - 1)
{
int a, b; cin >> a >> b;
G[a].push_back(b); G[b].push_back(a);
}
cin >> nq;
RE2(i, nq)
{
int pos, dep, v;
cin >> pos >> dep >> v;
qs[pos].push_back({ dep,v });
}
dfs(1, -1, 1, 0);
RE2(i, n)
{
if (i != 1)cout << ' ';
cout << res[i];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
return 0;
}