题目大意
给你棵树,允许在两个叶节点的路径上的边权上增加任何实数(初始值为0),问是否存在有限次操作,使每条边权可以为任何值。
分析
对于上图,我们发现无论如何1-3和2-7两条边的权值无论如何都是相等的,所以不行。通过模拟,我们发现如果有一个节点连两条边,就不符合情况,规律简单模拟一下即可。给出简略证明。
如果一个节点有两条边,肯定不是叶子节点,向下可以找到至少一个叶子节点,形成集合U如图中7,向上可以找到其他的叶节点如4,5,6形成集合V。U,V交集为空。任意U中的的叶节点到V都只能经过刚刚找到的节点相连的两条边,所以这两条边边权相等。
题目链接
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
int rud[N], n;
int main() {
ios::sync_with_stdio(false);
cin.tie(), cout.tie();
cin >> n;
for (int i = 0, x, y; i < n - 1; ++i) {
cin >> x >> y;
++rud[x], ++rud[y];
}
bool flag = true;
for (int i = 1; i <= n; ++i) {
if (rud[i] == 2){
flag = false;
break;
}
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}