首页 > 其他分享 >E. Two Chess Pieces -- (codeforces) 树形DP

E. Two Chess Pieces -- (codeforces) 树形DP

时间:2023-07-11 16:33:24浏览次数:38  
标签:codeforces -- s2 s1 Two son int flag 棋子

原题链接:https://codeforces.com/contest/1774/problem/E

题意:两颗棋子,给出两颗棋子必须要去的顶点,且给出两颗棋子的相隔距离不能大于d,算出两颗棋子完成目标后走的距离。最后两颗棋子都要回到顶点1上。

思路:刚开始没想出来,顺着官方题解写的,大意就是我用数组 s1 和 s2 代表两颗棋子标记要去的顶点,最后算答案的时候直接计算就行了,标记要去的点主要是第一个 dfs1 就是用来标记两个棋子距离大于d后,会带动领一个棋子一定要走的路。

AC代码:

const int N = 1e6 +  10;
vector<int> v[N];
int s1[N], s2[N];
int a[N], b[N];
int n, d;

void dfs1(int u, int fa, int dis) // b数组用于标记当一个棋子走后至少另一个棋子也要在的点。
{
	a[dis] = u;
	if (dis > d) b[u] = a[dis - d];
	else b[u] = 1;
	
	for (int i = 0; i < v[u].size(); i ++)
	{
		int son = v[u][i];
		if (son == fa) continue;
		dfs1(son, u, dis + 1);
	}
}

void dfs2(int u, int fa) // 很模板的遍历走整棵树的路,当子节点有必须要走的点时,当前这个节点也要走
{
	bool flag = 0;
	for (int i = 0; i < v[u].size(); i ++)
	{
		int son = v[u][i];
		if (son == fa) continue;
		dfs2(son, u);
		flag |= s1[son]; // 判断子节点是否为要走的点
	}
	s1[u] |= flag;
}

void dfs3(int u, int fa) // 同理
{
	bool flag = 0;
	for (int i = 0; i < v[u].size(); i ++)
	{
		int son = v[u][i];
		if (son == fa) continue;
		dfs3(son, u);
		flag |= s2[son];
	}
	s2[u] |= flag;
}

void solve()
{
	cin >> n >> d;
	for (int i = 1; i < n; i ++)
	{
		int x, y;
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	
	int m1, m2;
	dfs1(1, -1, 0);	
	cin >> m1;
	for (int i = 1; i <= m1; i ++) {
		int x;
		cin >> x, s1[x] = 1;	
		s2[b[x]] = 1;
	}
	
	cin >> m2;
	for (int i = 1; i <= m2; i ++) {
		int x;
		cin >> x, s2[x] = 1;
		s1[b[x]] = 1;	
	}
	dfs2(1, -1);
	dfs3(1, -1);
	int ans = 0;
	for (int i = 2; i <= n; i ++)
	{
		if (s1[i]) ans += 2;
		if (s2[i]) ans += 2;
	}
	cout << ans << '\n';
}

标签:codeforces,--,s2,s1,Two,son,int,flag,棋子
From: https://www.cnblogs.com/NNNZZZ/p/17545146.html

相关文章

  • chmod
    chmod用来变更文件或目录的权限概要chmod[OPTION]...MODE[,MODE]...FILE...chmod[OPTION]...OCTAL-MODEFILE...chmod[OPTION]...--reference=RFILEFILE...主要用途通过符号组合的方式更改目标文件或目录的权限。通过八进制数的方式更改目标文件或目录的权限。......
  • 进程与线程
    简而言之,一个程序至少有一个进程,一个进程至少有一个线程. 线程的划分尺度小于进程,使得多线程程序的并发性高。另外,进程在执行过程中拥有独立的内存单元,而多个线程共享内存,从而极大地提高了程序的运行效率。线程在执行过程中与进程还是有区别的。每个独立的线程有一个程序运......
  • poj 1064 高精度 二分
    CablemasterTimeLimit: 1000MSMemoryLimit: 10000KTotalSubmissions: 32191Accepted: 6888DescriptionInhabitantsoftheWonderlandhavedecidedtoholdaregionalprogrammingcontest.TheJudgingCommitteehasvolunteeredandhaspromisedtoorganizethe......
  • Find a way bfs搜索 容易出错
    题目链接:题意:给你一个图,图中有不能走的障碍物,和两人,以及n个(n>=1)KFC,现在要求找到其中一个KFC,让两个人人走到这个KFC的时间总和最小;#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>......
  • poj 2236 Wireless Network 并查集
    WirelessNetworkTimeLimit: 10000MSMemoryLimit: 65536KTotalSubmissions: 20499Accepted: 8608DescriptionAnearthquaketakesplaceinSoutheastAsia.TheACM(AsiaCooperatedMedicalteam)havesetupawirelessnetworkwiththelapcomputers,butanun......
  • POJ 2109 Power of Cryptography 数学题 double和float精度和范围
    PowerofCryptographyTimeLimit:1000MSMemoryLimit:30000KTotalSubmissions:21354Accepted:10799DescriptionCurrentworkincryptographyinvolves(amongotherthings)largeprimenumbersandcomputingpowersofnumbersamongtheseprimes.Workint......
  • poj 1182 食物链 并查集
    食物链TimeLimit:1000MSMemoryLimit:10000KTotalSubmissions:56297Accepted:16500Description动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B,B吃C,C吃A。现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种......
  • 文章人工审核
    需求:自媒体文章如果没有自动审核成功,而是到了人工审核(自媒体文章状态为3),需要在admin端人工处理文章的审核平台管理员可以查看待人工审核的文章信息,如果存在违规内容则驳回(状态改为2,文章审核失败)平台管理员可以查看待人工审核的文章信息,如果不存在违规,则需要创建app端的文......
  • zlm+wvp+redis搭建视频平台
    Windows下安装redis下载地址:https://github.com/tporadowski/redis/releases zlm视频服务搭建请参考https://www.cnblogs.com/yebinghuai/p/ZLMediaKit.html运行界面 wvp视频信念搭建依赖环境需要安装Node.js请参考https://www.cnblogs.com/yebinghuai/p/17544969.......
  • 如何科学的拍摄星空? by VEmatches
    架好三脚架,调整参数,对准星空,按下快门键。20秒后,一张星空图片就拍摄完成了。如何拍摄星空?准备工作有哪些?手机拍摄星空首先,拍摄地点要远离光污染的城区。你可以在“天文通”网站上查看当地光污染指数,其中绿色表示为四级光污染,低于四级为最佳拍摄地点。(新手限制无法发布网站)选......