codeforces 981 C Useful Decomposition

http://www.elijahqi.win/archives/3527
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 n

n
(2≤n≤105

2≤n≤105
) the number of nodes in the tree.

Each of the next n−1

n − 1
lines contains two integers ai

ai
and bi

bi
(1≤ai,bi≤n

1≤ai,bi≤n
, ai≠bi

ai≠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 m

m
.

Each of the next m

m
lines should contain two integers ui

ui
, vi

vi
(1≤ui,vi≤n

1≤ui,vi≤n
, ui≠vi

ui≠vi
) denoting that one of the paths in the decomposition is the simple path between nodes ui

ui
and vi

vi
.

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.

题意:判定一个图是否是菊花图

辣鸡elijahqi做法是 设dp[x]表示x节点管辖的叶子节点是多少然后强行钦定根每次转移 的时候把我这层的叶子节点个数转移给我的子树中 注意判断转移的点是否是叶子节点的情况

网上有其他优美好看的代码 欢迎学习

#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<algorithm>
#define pa pair<int,int>
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
    return x*f;
}
const int N=1e5+10;
struct node{
    int y,next;
}data[N<<1];
int h[N],st,n,num,size[N],dp[N],d[N];bool flag=0;
inline void dfs_init(int x,int fa){
    bool flag1=0;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if (y==fa) continue;
        flag1=1;dfs_init(y,x);size[x]+=size[y];
    }if (!flag1) size[x]=1;
}
inline void dfs(int x,int fa){
    bool flag1=1;int tot=dp[x];
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if(y==fa) continue;
        tot+=size[y];
    }dp[x]=tot;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if (y==fa){
            if (dp[y]-size[x]>1) flag1=0; 
        }else{dp[y]+=dp[x]-size[y]+(d[x]==1);
            dfs(y,x);if(flag) return;
            if(size[y]>1) flag1=0;
        }
    }if (flag1) {flag=1;st=x;return;}
}
vector<pa>pd;
inline void dfs1(int x,int fa){
    bool flag1=0;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if (y==fa) continue;
        flag1=1;dfs1(y,x);
    }if (!flag1) pd.push_back(make_pair(st,x));
}
int main(){
//  freopen("c.in","r",stdin);
    n=read();
    for (int i=1;i<n;++i){
        int x=read(),y=read();++d[x];++d[y];
        data[++num].y=y;data[num].next=h[x];h[x]=num;
        data[++num].y=x;data[num].next=h[y];h[y]=num;
    }dfs_init(1,0);
    dfs(1,0);
    if (!flag) {puts("No");return 0;}puts("Yes");

    for (int i=h[st];i;i=data[i].next){
        int y=data[i].y;//printf("%d ",st);
        dfs1(y,st);
    }printf("%d\n",pd.size());
    for (int i=0;i<pd.size();++i) printf("%d %d\n",pd[i].first,pd[i].second);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/elijahqi/article/details/80485038
981