CodeForce - 1189 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 nn nodes. In the beginning, 00 is written on all edges. In one operation, you can choose any 22 distinct leaves uu, vvand any real number xx and add xx to values written on all edges on the simple path between uu and vv.

For example, on the picture below you can see the result of applying two operations to the graph: adding 22 on the path from 77 to 66, and then adding 0.5−0.5 on the path from 44 to 55.

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 11. Simple path is a path that doesn't contain any node twice.

Input

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

Each of the next n1n−1 lines contains two integers uu and vv (1u,vn1≤u,v≤n, uvu≠v), meaning that there is an edge between nodes uu and vv. 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).

Examples
input
Copy
2
1 2
output
Copy
YES
input
Copy
3
1 2
2 3
output
Copy
NO
 
 
 
題意:
給定一棵樹,樹上的邊權初始為0,你可以在任意兩個葉子之間的簡單路徑上的邊上加上一個權值實數x。
問:能否在有限次數的操作內,得到邊權任意組合的樹
 
思路:
假設有葉子節點L1,L2,L3,並且有L1的鄰居U,L1,L2和L3在以U為根節點的樹的不同子樹上。
現在想要U到L1邊權為x,而其他邊權不變。那么只需要L1-L2的路徑上加上X/2,L1-L3的路徑上加上X/2,L2-L3上的路徑減去想x/2 即可。
顯而易見要完成這個操作,U的度數要大於等於3
所以只需要判斷有沒有度數為2的點行了。
 
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>

#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a, x) cout<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define ls (t<<1)
#define rs ((t<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int maxm = 100086;
const int inf = 0x3f3f3f3f;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);

int n;
int num[maxn];


int main() {

    scanf("%d",&n);

    for(int i=1;i<n;i++){
        int x;
        scanf("%d",&x);
        num[x]++;
        scanf("%d",&x);
        num[x]++;
    }
    bool flag = true;
    for(int i=0;i<maxn;i++){
        if(num[i]==2){
            flag=false;
        }
    }

    if(flag){
        printf("YES\n");
    }
    else{
        printf("NO\n");
    }

    return 0;
}
View Code


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM