跳到正文

个人记录

1

whr_1230约 1 分钟
原文

这是 2025年10月22日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年10月22日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 1 分钟

版本

  1. v5当前
  2. v4
  3. v3
  4. v2
更早的 1 个版本
  1. v1
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+10;
vector<int>e[maxn];
int n,m;
int val[maxn],a[maxn];
void dfs(int u,int ff)
{
	a[u]=a[ff]+val[u];
	for(auto v:e[u])
	{
		if(v==ff)continue;
		dfs(v,u);
	}
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n-1;i++)
	{
		int x,y;
		cin>>x>>y;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	while(m--)
	{
		int pos,x;
		cin>>pos>>x;
		val[pos]+=x;
	}
	dfs(1,0);
	for(int i=1;i<=n;i++)
	{
		cout<<a[i]<<" ";
	}
	return 0;
}