• [简单路径] Useful Decomposition


    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

    (2n105

    ) the number of nodes in the tree.

    Each of the next n1

    lines contains two integers ai and bi (1ai,bin, aibi

    ) — 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

    .

    Each of the next m

    lines should contain two integers ui, vi (1ui,vin, uivi) denoting that one of the paths in the decomposition is the simple path between nodes ui and 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
    4
    1 2
    2 3
    3 4
    Output
    Yes
    1
    1 4
    Input
    6
    1 2
    2 3
    3 4
    2 5
    3 6
    Output
    No
    Input
    5
    1 2
    1 3
    1 4
    1 5
    Output
    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.

     
    题意:给出n个结点成一颗树,问是否存在一些过一个公共结点的简单路径,如果存在则输出Yes并输出这些路径,否则输出No
    思路:简单路径就是在一个路径中同一个边只能出现一次.如果存在一些过一个公共结点的简单路径则最多只能有一个结点的度数大于2
    我们选一个度数最大的结点作为根结点,根结点到叶子结点的简单路径就是合法的简单路径,dfs求一下就完事了,然后就test5超时了.
    其实我们要输出根结点和叶子结点,而叶子结点是度数为1的结点,所以我们统计所有度数为1的结点,把它和根节点一起输出就好了
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<queue>
     5 #include<algorithm>
     6 using namespace std;
     7 typedef long long ll;
     8 const int amn=1e5+5;
     9 int n,ans,idx[amn],root;
    10 vector<int> eg[amn];
    11 struct node{
    12     int i,val;
    13 }cnt[amn];
    14 bool cmp(node a,node b){
    15     if(a.val==b.val)return a.i<b.i;
    16     return a.val>b.val;
    17 }
    18 int main(){
    19     scanf("%d",&n);
    20     int x,y;
    21     for(int i=1;i<=n-1;i++){
    22         scanf("%d%d",&x,&y);
    23         eg[x].push_back(y);
    24         eg[y].push_back(x);
    25     }
    26     for(int i=1;i<=n;i++){
    27         cnt[i].val=eg[i].size();
    28         cnt[i].i=i;
    29     }
    30     sort(cnt+1,cnt+1+n,cmp);
    31     if(cnt[1].val>2&&cnt[2].val>2){
    32         printf("No
    ");
    33     }
    34     else{
    35         if(cnt[1].val>2)root=cnt[1].i;
    36         else{
    37             for(int i=1;i<=n;i++){
    38                 if(cnt[i].val<2){
    39                     root=cnt[i].i;
    40                     break;
    41                 }
    42             }
    43         }
    44         memset(idx,0,sizeof idx);
    45         printf("Yes
    %d
    ",eg[root].size());
    46         int st;
    47         for(int i=n;i>=1;i--){
    48             if(cnt[i].val>1)break;
    49             st=i;
    50         }
    51         for(int i=st;i<=n;i++){
    52             if(cnt[i].i==root)continue;
    53             printf("%d ",root);
    54             printf("%d
    ",cnt[i].i);
    55         }
    56     }
    57 }
    58 /**
    59 题意:给出n个结点成一颗树,问是否存在一些过一个公共结点的简单路径,如果存在则输出Yes并输出这些路径,否则输出No
    60 思路:简单路径就是在一个路径中同一个边只能出现一次.如果存在一些过一个公共结点的简单路径则最多只能有一个结点的度数大于2
    61 我们选一个度数最大的结点作为根结点,根结点到叶子结点的简单路径就是合法的简单路径,dfs求一下就完事了,然后就test5超时了.
    62 其实我们要输出根结点和叶子结点,而叶子结点是度数为1的结点,所以我们统计所有度数为1的结点,把它和根节点一起输出就好了
    63 **/
  • 相关阅读:
    文本框输入内容提示消失的两种实现
    改变选择网页文字背景色
    软件增加一键处理功能
    全自动相机标定软件
    近景/低空摄影测量系统DPMatrix增加新功能
    DPMatrix软件新增了DEM/DOM制作模块,功能基本齐整!
    DPMatrix软件新增了图像匀光和色彩匹配批处理功能
    基于GPU/CUDA的全景图拼接
    基于GPU\CUDA并行处理技术的摄影测量
    软件与PATB平差数据交换
  • 原文地址:https://www.cnblogs.com/Railgun000/p/11827586.html
Copyright © 2020-2023  润新知