zoukankan      html  css  js  c++  java
  • Avito Code Challenge 2018 C

    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≤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.

    题意  分成任意条路   任意两条路至少有一个交点     而且任意两条路不能有相同的边  输出 路径

    解析  可以得到  所有路只有一个交点  所以最多只有一个点的度数大于等于3 才有解决方案 那个点就是交点  以它为起点枚举路径就好了

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 const ll mod=998244353,maxn=1e5+50;
     5 #define ios() ios::sync_with_stdio(false),cin.tie(),cout.tie()
     6 vector<int> g[maxn];
     7 int vis[maxn],ans[maxn],cnt;
     8 void dfs(int x)
     9 {
    10     vis[x]=1;
    11     int flagg=0;
    12     for(int i=0;i<g[x].size();i++)
    13     {
    14         int u=g[x][i];
    15         if(vis[u]==0)
    16         {
    17             flagg++;
    18             dfs(u);
    19         }
    20     }
    21     if(flagg==0)
    22         ans[cnt++]=x;
    23 }
    24 int main()
    25 {
    26     ios();
    27     int n;
    28     cin>>n;
    29     for(int i=0;i<n-1;i++)
    30     {
    31         int x,y;
    32         cin>>x>>y;
    33         g[x].push_back(y);
    34         g[y].push_back(x);
    35     }
    36     int flag=1,sum=0;
    37     for(int i=1;i<=n;i++)
    38     {
    39         if(g[i].size()>=3)
    40             sum++,flag=i;
    41     }
    42     if(sum>=2)
    43         cout<<"No"<<endl;
    44     else
    45     {
    46         memset(vis,0,sizeof(vis));
    47         cnt=0;
    48         dfs(flag);
    49         cout<<"Yes"<<'
    '<<cnt<<endl;
    50         for(int i=0;i<cnt;i++)
    51         {
    52             cout<<flag<<" "<<ans[i]<<endl;
    53         }
    54     }
    55 }
  • 相关阅读:
    viewpaper
    mfc ui 3 swf
    mfc ui3
    mfc ui2
    mfc ui库
    将Cocos2dX渲染到MFC窗口上
    MFC 框架技术简单研讨
    不可忽略的数据库缓存重建
    google bookmarket api
    android 加载大图片
  • 原文地址:https://www.cnblogs.com/stranger-/p/9108265.html
Copyright © 2011-2022 走看看