站外题求条!(玄关,先到先得)
  • 板块灌水区
  • 楼主Hope5365
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/10/21 15:47
  • 上次更新2024/10/21 18:54:07
查看原帖
站外题求条!(玄关,先到先得)
1004336
Hope5365楼主2024/10/21 15:47

孩子调傻逼了,救救孩子吧!

#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
using namespace std;
#define int long long

const int N = 1e5 + 10,mod = 1e9 + 7;
int n,m,K,a[N],add1[N],add2[N],dep[N],sizef[N],sizes[N];//add1代表增加父亲,add2代表增加儿子
vector <int> v[N],son[N];
vector <int> fa[N]; 
queue <int> q;

inline void dfs1(int x,int f)
{
	for(int i = 0;i < v[x].size();i ++)
	{
		int g = v[x][i];
		if (g == f) continue;
		dep[g] = dep[x] + 1;
		sizef[g] ++;
		sizes[x] ++;
		son[x].push_back(g);
		fa[g].push_back(x);
		dfs1(g,x);
	}
}

inline void bfs1()
{ //改变儿子 
	while (!q.empty())
	{
		int x = q.front();
		q.pop();
//		a[x] = (a[x] + add2[x]) % mod;
		for(int i = 0;i < son[x].size();i ++)
		{
			int g = son[x][i];
			add2[g] = (add2[x] + add2[g]) % mod;
			sizef[g] --;
			if (sizef[g] == 0) q.push(g);
		}
	}
}

inline void bfs2()
{
	while (!q.empty())
	{
		int x = q.front();
		q.pop();
//		a[x] = (a[x] + add1[x]) % mod;
		for(int i = 0;i < fa[x].size();i ++)
		{
			int g = fa[x][i];
			add1[g] = (add1[g] + add1[x]) % mod;
			sizes[g] --;
			if (sizes[g] == 0) q.push(g);
		}
	}
}

signed main()
{
	freopen("kdl.in","r",stdin);
	freopen("kdl.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m >> K;
	for(int i = 1;i <= n;i ++) cin >> a[i];
	for(int i = 1;i < n;i ++)
	{
		int x,y;
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs1(1,0);
	while (m --)
	{
		int x,y;
		cin >> x >> y;
		if (dep[x] < dep[y])
		{
			son[x].push_back(y);
			fa[y].push_back(x);
			sizes[x] ++;
			sizef[y] ++;
		}
		else
		{
			son[y].push_back(x);
			fa[x].push_back(y);
			sizef[x] ++;
			sizes[y] ++;
		}
	}
	while (K --)
	{
		int t,u,k;
		cin >> t >> u >> k;
		if (t == 1) add1[u] = (add1[u] + k) % mod;
		else add2[u] = (add2[u] + k) % mod;
	}
	for(int i = 1;i <= n;i ++)
		if (sizef[i] == 0)
			q.push(i);
	bfs1();
	for(int i = 1;i <= n;i ++)
		if (sizes[i] == 0)
			q.push(i);
	bfs2();
	for(int i = 1;i <= n;i ++)
		cout << (a[i] + add1[i] + add2[i]) % mod << ' ';
	return 0;
}
2024/10/21 15:47
加载中...