The Preliminary Contest for ICPC Asia Shenyang 2019 D. Fish eating fruit 树形DP 换根与二次扫描

题源:https://nanti.jisuanke.com/t/41403

代码借鉴于:https://blog.csdn.net/weixin_44282912/article/details/100833858

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<stack>
#include<cstring>
#include<set>
#include<iterator>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<cmath>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<iomanip>
//#include<unordered_set>
using namespace std;
#define lowbit(x) x&(-x)
typedef long long ll;
typedef pair<int, int> P;
const int N = 1e4 + 10;
const int M = 1e4 + 10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
ll dis[N][3], num[N][3];
ll ans[3];
int tot;
int head[N], ver[2 * N], edge[2 * N], Next[2 * N];
inline void addedge(int x, int y, int z)
{
	ver[++tot] = y, edge[tot] = z, Next[tot] = head[x], head[x] = tot;
	ver[++tot] = x, edge[tot] = z, Next[tot] = head[y], head[y] = tot;
}
void solve(int u)
{
	for (int i = 0; i < 3; i++)
	{
		ans[i] = (ans[i] + dis[u][i]) % mod;
	}
}
void dfs(int u, int fa)
{
	dis[u][0] = dis[u][1] = dis[u][2] = 0;
	num[u][0] = num[u][1] = num[u][2] = 0;
	for (int i = head[u]; i; i = Next[i])
	{
		int v = ver[i], w = edge[i];
		if (v == fa)
			continue;
		dfs(v, u);
		num[v][0]++; 
		for (int j = 0; j < 3; j++)
		{
			dis[u][(j + w) % 3] += dis[v][j] + w * num[v][j];
			num[u][(j + w) % 3] += num[v][j];
		}
	}
}
void dfs2(int u, int fa)
{
	for (int i = head[u]; i; i = Next[i])
	{
		int v = ver[i], w = edge[i];
		if (v == fa)
			continue;
		ll tdis[2][3], tnum[2][3];
		for (int j = 0; j < 2; j++)
		{
			for (int k = 0; k < 3; k++)
			{
				if (!j)
				{
					tdis[j][k] = dis[u][k];
					tnum[j][k] = num[u][k];
				}
				else
				{
					tdis[j][k] = dis[v][k];
					tnum[j][k] = num[v][k];
				}
			}
		}
		for (int j = 0; j < 3; j++)
		{
			dis[u][(w + j) % 3] -= num[v][j] * w + dis[v][j];
			num[u][(w + j) % 3] -= num[v][j];
		}
		num[v][0]--;
		num[u][0]++;
		for (int j = 0; j < 3; j++)
		{
			dis[v][(w + j) % 3] += num[u][j] * w + dis[u][j];
			num[v][(w + j) % 3] += num[u][j];
		}
		solve(v);
		dfs2(v, u);
		for (int j = 0; j < 2; j++)
		{
			for (int k = 0; k < 3; k++)
			{
				if (!j)
				{
					dis[u][k] = tdis[j][k];
					num[u][k] = tnum[j][k];
				}
				else
				{
					dis[v][k] = tdis[j][k];
					num[v][k] = tnum[j][k];
				}
			}
		}
	}
}
int main()
{
	int n;
	while (scanf("%d", &n) != EOF)
	{
		tot = 0;
		memset(ans, 0, sizeof(ans));
		memset(head, 0, sizeof(head));
		for (int i = 1; i < n; i++)
		{
			int x, y, z;
			scanf("%d%d%d", &x, &y, &z);
			x++, y++;
			addedge(x, y, z);
		}
		dfs(1, 0);
		solve(1);
		dfs2(1, 0);
		printf("%lld %lld %lld\n", ans[0], ans[1], ans[2]);
	}
	return 0;
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I'd be happy to give you some ideas for organizing a speech contest. Here are some suggestions: 1. Determine the theme and rules: Decide on the theme of the contest and the rules for participants. Will it be an open topic, or will there be a specific theme? What is the maximum length of the speech? Will there be any specific guidelines for language or content? 2. Decide on the judging criteria: Determine how the speeches will be evaluated. Will judges be looking for content, delivery, or both? Will there be a score sheet or rubric that judges will use to score the speeches? 3. Recruit judges: Find people who are qualified to judge the speeches. Ideally, they should have experience in public speaking or have a background in the theme of the contest. 4. Promote the contest: Advertise the contest to potential participants, such as students, professionals, or members of a specific community. Use social media, flyers, and other methods to get the word out. 5. Registration and selection: Set a deadline for registration and selection of participants. Consider having a preliminary round to narrow down the field before the final competition. 6. Prepare the venue: Ensure that the venue is suitable for the contest. Make sure that there is adequate seating, sound equipment, and lighting for the speakers. 7. Hold the contest: Set a date and time for the contest, and make sure that all participants and judges are aware of the schedule. Encourage audience participation and provide refreshments. 8. Award ceremony: After the contest, hold an award ceremony to recognize the winners and participants. Provide certificates or other prizes to the top performers. I hope these ideas help you in organizing a successful speech contest!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值