codeforces#572Div2 D1---Add On A Tree【思维】

博客围绕http://codeforces.com/contest/1189/problem/D1题目展开,给定一棵树,可选择两叶子节点对路径增加权值,探讨能否得到任意形式的权值。思路指出,若有节点度为2,其连接的某条边会受另一条边控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目http://codeforces.com/contest/1189/problem/D1

题意:给定一棵树,可以选择任意两个叶子节点对他们的路径增加一定的权值。

问对于给定的这棵树,是否可以得到任意形式的权值。

思路

只要有一个节点的度是2,那么这个节点连接的某一条边一定受到另一条边的控制。

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<map>
 4 #include<set>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<vector>
 8 #include<cmath> 
 9 #include<stack>
10 #include<queue>
11 #include<iostream>
12 
13 #define inf 0x3f3f3f3f
14 using namespace std;
15 typedef long long LL;
16 typedef pair<int, int> pr;
17 
18 int n;
19 const int maxn = 1e5 + 5;
20 int deg[maxn];
21 
22 int main()
23 {
24     scanf("%d", &n);;
25     for(int i = 1; i < n; i++){
26         int u, v;
27         scanf("%d%d", &u, &v);
28         deg[u]++;
29         deg[v]++;
30     }
31     int ans = true;
32     for(int i = 1; i <= n; i++){
33         if(deg[i] == 2){
34             ans = false;
35             break;
36         }
37     }
38     if(ans){
39         printf("YES\n");
40     }
41     else{
42         printf("NO\n");
43     }
44     return 0;
45 }

 

转载于:https://www.cnblogs.com/wyboooo/p/11155682.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值