码迷,mamicode.com
首页 > 其他好文 > 详细

Useful Decomposition

时间:2018-05-28 21:47:14      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:ica   html   ems   codeforce   不能   order   XA   sim   sse   

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 n?1n???1 lines contains two integers aiai and bibi (1ai,bin1≤ai,bi≤n, aibiai≠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 uiui, vivi (1ui,vin1≤ui,vi≤n, uiviui≠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.

 

题意是给你一颗树,问你能不能分解成若干条路,每条路之间要有公共点,且每条路不能共用一条边,路必须是简单路(不往回走能走通)。

我们可以发现,如果除了根节点外,其他节点有两个及以上子节点,那么必然无法分解。

那么除了根节点,其余节点最多只有一个子节点。

我们选取一个根节点,此根节点必然是节点数最多的,然后将其与没有子节点的节点作为一条路,就可以达到题目要求。

AC代码:

#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
int sizes[maxn];
int main()
{
    int n,i;
    scanf("%d",&n);
    for(i=1;i<=n-1;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        sizes[u]++;
        sizes[v]++;
    }
    int flag=0;
    int sum=-1,num;
    for(i=1;i<=n;i++)
    {
        if(sizes[i]>=sum)
        {
            num=i;
            sum=sizes[i];
        }
    }
    for(i=1;i<=n;i++)
    {
        if(i!=num)
        {
            if(sizes[i]>=3) flag=1;
        }
    }
    if(flag) printf("No\n");
    else
    {
        printf("Yes\n");
        int cnts=0;
        for(i=1;i<=n;i++)
        {
            if(i!=num)

            {if(sizes[i]==1)
            {   cnts++;
            }}
        }
        printf("%d\n",cnts);
        for(i=1;i<=n;i++)
        {
            if(i!=num)
            {
            if(sizes[i]==1)
            {
                printf("%d %d\n",num,i);
            }
            }
        }
    }
    return 0;
}

  

Useful Decomposition

标签:ica   html   ems   codeforce   不能   order   XA   sim   sse   

原文地址:https://www.cnblogs.com/zyf3855923/p/9102389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!