CF 1189D1和1189D2-树的边权-思维+搜索

8 篇文章 0 订阅
5 篇文章 0 订阅

共有两问!

第一问:D1. Add on a Tree(传送门

Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.

You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.

For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding −0.5 on the path from 4 to 5.
在这里插入图片描述Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?

Leaf is a node of a tree of degree 1. Simple path is a path that doesn’t contain any node twice.

Input

The first line contains a single integer n (2≤n≤105) — the number of nodes.

Each of the next n−1 lines contains two integers u and v (1≤u,v≤n, u≠v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.

Output

If there is a configuration of real numbers written on edges of the tree that we can’t achieve by performing the operations, output “NO”.

Otherwise, output “YES”.

You can print each letter in any case (upper or lower).

Sample Input1

2
1 2

Sample Output1

YES

Sample Input2

3
1 2
2 3

Sample Output2

NO

Sample Input3

5
1 2
1 3
1 4
2 5

Sample Output3

NO

Sample Input4

6
1 2
1 3
1 4
2 5
2 6

Sample Output4

YES

题目大意:

注意,这是两个类似问题中的第一个问题。只有同时解决这两个问题,你才有资格为这个问题增加测试数据。
给定一个有n个节点的树。一开始,所有的边都是0。在一个操作中,您可以选择任意两个不同的叶节点u、v和任意实数x,并将x加到u和v路径上的所有边上。

给定一棵树,我们可以通过有限的运算来使这棵树的所有边的权值成任意构型吗?

叶是一个度为1的树的节点。简单路径是不包含任何节点两次的路径。

核心思想:

存在度为2的结点,则输出NO,否则输出YES。
对于某个度为2的结点,和它直接相连的两条边的权值一定是相同的。这两条边的权值一定相同,那么就不能成任意构型

代码如下:

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int N=2e5+10;
int du[N];
int main()
{
	int n,x,y;
	scanf("%d",&n);
	for(int i=1; i<n; i++)
	{
		scanf("%d%d",&x,&y);
		du[x]++;
		du[y]++;
	}
	for(int i=1; i<=n; i++)
		if(du[i]==2)
		{
			printf("NO\n");
			return 0;
		}
	printf("YES\n");
	return 0;
}

第二问:D2. Add on a Tree: Revolution(传送门

Note that this is the second problem of the two similar problems. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.

You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any integer number x and add x to values written on all edges on the simple path between u and v. Note that in previous subtask x was allowed to be any real, here it has to be integer.

For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding −1 on the path from 4 to 5.
在这里插入图片描述You are given some configuration of nonnegative integer pairwise different even numbers, written on the edges. For a given configuration determine if it is possible to achieve it with these operations, and, if it is possible, output the sequence of operations that leads to the given configuration. Constraints on the operations are listed in the output format section.

Leave is a node of a tree of degree 1. Simple path is a path that doesn’t contain any node twice.

Input

The first line contains a single integer n (2≤n≤1000) — the number of nodes in a tree.

Each of the next n−1 lines contains three integers u, v, val (1≤u,v≤n, u≠v, 0≤val≤10000), meaning that there is an edge between nodes u and v with val written on it. It is guaranteed that these edges form a tree. It is guaranteed that all val numbers are pairwise different and even.

Output

If there aren’t any sequences of operations which lead to the given configuration, output “NO”.

If it exists, output “YES” in the first line. In the second line output m — number of operations you are going to apply (0≤m≤105). Note that you don’t have to minimize the number of the operations!

In the next m lines output the operations in the following format:
u,v,x(1≤u,v≤n, u≠v, x — integer, −109≤x≤109), where u,v — leaves, x — number we are adding.

It is guaranteed that if there exists a sequence of operations producing given configuration, then there exists a sequence of operations producing given configuration, satisfying all the conditions above.

Sample Input1

5
1 2 2
2 3 4
3 4 10
3 5 18

Sample Output1

NO

Sample Input2

6
1 2 6
1 3 8
1 4 12
2 5 2
2 6 4

Sample Output2

YES
4
3 6 1
4 6 3
3 4 7
4 5 2

题目大意:

给定一棵具有最终构型的树,每条边的权值都是偶数
问:根据第一问的构造方法,能否将这棵树的构型构造出来?
如果不能,只输出NO;
如果可以,输出YES,操作次数,以及每次是怎样操作的。

核心思想:

判断能否还是根据是否存在度为2的结点。
YES的操作方法
对于某条边side[i],假设边的端点为x和y,权值为v
1、从x端找两个路径完全不同的叶子结点,记为x1,x2;从y端找两个路径完全不同的叶子结点,记为y1,y2。
2、x1到y1的路径都加权值v/2,x2到y2的路径也都加权值v/2,那么边side[i]的权值就是v了。
3、x1到x2的路径(不经过边side[i])都减权值v/2,y1到y2的路径(不经过边side[i])也都减权值v/2。
至此,这些操作产生的影响就是边side[i]的权值加上了v,而其他边的权值都没有改变
另外:
1、第1步中路径完全不同的原因是:如果路径重合了一部分,第3步将操作不到此部分
2、如果某条边的端点是叶子结点,那么此端点就充当两个点就可以了,并且还不用进行第3步
详见代码。

代码如下:

#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
const int N=1e3+50;
//存n-1条边的最终构型信息 
struct bian{
	int x,y,v;
}side[N];
//存操作过程,准备输出 
struct node{
	int x,y,v;
	node()
	{
	}
	node(int xx,int yy,int vv)
	{
		x=xx;
		y=yy;
		v=vv;
	}
}out[N<<2];
//存点的邻点 
vector<int>vec[N];
//用于dfs标记点 
bool vis[N];
//b数组存找到的叶子结点,cnt是b数组元素数量,co是操作次数 
int b[3],cnt,co;
//深搜找叶子结点 
void dfs(int x)
{
	if(vec[x].size()==1)
	{
		b[cnt++]=x;
		return;
	}
	vis[x]=1;
	int len=vec[x].size();
	for(int i=0;i<len;i++)
	{
		int tx=vec[x][i];
		if(vis[tx])continue;
		dfs(tx);
		break;//两个叶子结点的路径要完全不同 
	}
	vis[x]=0;
	return;
}
//处理第k条边
void fun(int k)
{
	int x=side[k].x;
	int y=side[k].y;
	int x1,x2,y1,y2;
	//求x端的叶子结点 
	if(vec[x].size()==1)
		x1=x2=x;
	else
	{
		cnt=0;
		vis[x]=1;
		int len=vec[x].size();
		for(int i=0;i<len;i++)
		{
			int tx=vec[x][i];
			if(tx==y)continue;
			dfs(tx);
			if(cnt>=2)
				break;
		}
		vis[x]=0;
		x1=b[0];
		x2=b[1];
	}
	//求y端的叶子结点 
	if(vec[y].size()==1)
		y1=y2=y;
	else
	{
		cnt=0;
		vis[y]=1;
		int len=vec[y].size();
		for(int i=0;i<len;i++)
		{
			int ty=vec[y][i];
			if(ty==x)continue;
			dfs(ty);
			if(cnt>=2)
				break;
		}
		vis[y]=0;
		y1=b[0];
		y2=b[1]; 
	}
	//准备输出 
	int v=side[k].v>>1;
	out[co++]=node(x1,y1,v);
	out[co++]=node(x2,y2,v);
	if(x1!=x2)
		out[co++]=node(x1,x2,-v);
	if(y1!=y2)
		out[co++]=node(y1,y2,-v);
	return;
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d%d",&side[i].x,&side[i].y,&side[i].v);
		vec[side[i].x].push_back(side[i].y);
		vec[side[i].y].push_back(side[i].x);
	}
	//NO
	for(int i=1;i<=n;i++)
		if(vec[i].size()==2)
		{
			printf("NO\n");
			return 0;
		}
	//YES
	printf("YES\n");
	for(int i=1;i<n;i++)
		fun(i);
	printf("%d\n",co);
	for(int i=0;i<co;i++)
		printf("%d %d %d\n",out[i].x,out[i].y,out[i].v);
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值