Codeforces 981 C. Useful Decomposition [思维]

C. Useful Decomposition
time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)!

He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help!

The decomposition is the splitting the edges of the tree in some simple paths in such a way that each two paths have at least one common vertex. Each edge of the tree should be in exactly one path.

Help Remesses, find such a decomposition of the tree or derermine that there is no such decomposition.

Input

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

Each of the next n1n − 1 lines contains two integers aiai and bibi (1ai,bin1≤ai,bi≤naibiai≠bi) — the edges of the tree. It is guaranteed that the given edges form a tree.

Output

If there are no decompositions, print the only line containing "No".

Otherwise in the first line print "Yes", and in the second line print the number of paths in the decomposition mm.

Each of the next mm lines should contain two integers uiuivivi (1ui,vin1≤ui,vi≤nuiviui≠vi) denoting that one of the paths in the decomposition is the simple path between nodes uiui and vivi.

Each pair of paths in the decomposition should have at least one common vertex, and each edge of the tree should be presented in exactly one path. You can print the paths and the ends of each path in arbitrary order.

If there are multiple decompositions, print any.

Examples
input
Copy
4
1 2
2 3
3 4
output
Copy
Yes
1
1 4
input
Copy
6
1 2
2 3
3 4
2 5
3 6
output
Copy
No
input
Copy
5
1 2
1 3
1 4
1 5
output
Copy
Yes
4
1 2
1 3
1 4
1 5
Note

The tree from the first example is shown on the picture below:The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.

The tree from the second example is shown on the picture below:We can show that there are no valid decompositions of this tree.

The tree from the third example is shown on the picture below:

The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.

题目:给定一棵树,要求拆成若干条简单路径,并且这些路径都经过一个公共节点。给出任意一个解决方案,如不存在输出No。

分析:所有简单路径都经过一个公共节点,我们倒推回去,就是把公共节点看成根节点,然后从这个节点出发,没有分叉的节点;

那么只要先判断分叉节点是不是唯一的,如果唯一,那么把所有路径拆下来就可以啦!!!

代码:

#include<bits/stdc++.h>
#define ll long long

using namespace std;
const int maxn = 1e5+7;
int cnt[maxn], endv[maxn], n, tot;


int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d", &n))
    {
        tot = 0;
        for(int i = 0; i < n - 1; i++)
        {
            int a, b;
            scanf("%d%d", &a,&b);
            cnt[a]++;
            cnt[b]++;
        }
        int aim = -1;
        bool f = true;
        for(int i = 1; i <= n; i++){
            if(cnt[i] >= 3)
            {
                if(aim == -1) aim = i;
                else {
                    puts("No");
                    f = false;
                    break;
                }
            }
            if(cnt[i] == 1) endv[tot++] = i;
        }
        if(!f) continue;
        puts("Yes");
        if(aim == -1) printf("1\n%d %d\n", endv[0], endv[1]);
        else {
                printf("%d\n", tot);
                for(int i = 0; i < tot; i++)
                    printf("%d %d\n", aim, endv[i]);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值