美文网首页
(思维)A - Useful Decomposition Cod

(思维)A - Useful Decomposition Cod

作者: laochonger | 来源:发表于2018-07-27 20:15 被阅读0次

    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 n<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi></math> (2≤n≤105<math xmlns="http://www.w3.org/1998/Math/MathML"><mn>2</mn><mo>≤</mo><mi>n</mi><mo>≤</mo><msup><mn>10</mn><mrow class="MJX-TeXAtom-ORD"><mn>5</mn></mrow></msup></math>) the number of nodes in the tree.

    Each of the next n−1<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi><mo>−</mo><mn>1</mn></math> lines contains two integers ai<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>a</mi><mi>i</mi></msub></math> and bi<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>b</mi><mi>i</mi></msub></math> (1≤ai,bi≤n<math xmlns="http://www.w3.org/1998/Math/MathML"><mn>1</mn><mo>≤</mo><msub><mi>a</mi><mi>i</mi></msub><mo>,</mo><msub><mi>b</mi><mi>i</mi></msub><mo>≤</mo><mi>n</mi></math>, ai≠bi<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>a</mi><mi>i</mi></msub><mo>≠</mo><msub><mi>b</mi><mi>i</mi></msub></math>) — 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<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi></math>.

    Each of the next m<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi></math> lines should contain two integers ui<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>u</mi><mi>i</mi></msub></math>, vi<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>v</mi><mi>i</mi></msub></math> (1≤ui,vi≤n<math xmlns="http://www.w3.org/1998/Math/MathML"><mn>1</mn><mo>≤</mo><msub><mi>u</mi><mi>i</mi></msub><mo>,</mo><msub><mi>v</mi><mi>i</mi></msub><mo>≤</mo><mi>n</mi></math>, ui≠vi<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>u</mi><mi>i</mi></msub><mo>≠</mo><msub><mi>v</mi><mi>i</mi></msub></math>) denoting that one of the paths in the decomposition is the simple path between nodes ui<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>u</mi><mi>i</mi></msub></math> and vi<math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>v</mi><mi>i</mi></msub></math>.

    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

    <pre id="id0040983202872322533" style="margin: 0px; padding: 0.25em; font-size: 12.6px; font-family: Consolas, "Lucida Console", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier; line-height: 1.25em; white-space: pre-wrap; word-wrap: break-word; color: rgb(136, 0, 0); background-color: rgb(239, 239, 239);">4
    1 2
    2 3
    3 4
    </pre>

    output

    Copy

    <pre id="id0041575267104323266" style="margin: 0px; padding: 0.25em; font-size: 12.6px; font-family: Consolas, "Lucida Console", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier; line-height: 1.25em; white-space: pre-wrap; word-wrap: break-word; color: rgb(136, 0, 0); background-color: rgb(239, 239, 239);">Yes
    1
    1 4
    </pre>

    input

    Copy

    <pre id="id005932156758374372" style="margin: 0px; padding: 0.25em; font-size: 12.6px; font-family: Consolas, "Lucida Console", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier; line-height: 1.25em; white-space: pre-wrap; word-wrap: break-word; color: rgb(136, 0, 0); background-color: rgb(239, 239, 239);">6
    1 2
    2 3
    3 4
    2 5
    3 6
    </pre>

    output

    Copy

    <pre id="id003087586451489035" style="margin: 0px; padding: 0.25em; font-size: 12.6px; font-family: Consolas, "Lucida Console", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier; line-height: 1.25em; white-space: pre-wrap; word-wrap: break-word; color: rgb(136, 0, 0); background-color: rgb(239, 239, 239);">No
    </pre>

    input

    Copy

    <pre id="id009134373329995309" style="margin: 0px; padding: 0.25em; font-size: 12.6px; font-family: Consolas, "Lucida Console", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier; line-height: 1.25em; white-space: pre-wrap; word-wrap: break-word; color: rgb(136, 0, 0); background-color: rgb(239, 239, 239);">5
    1 2
    1 3
    1 4
    1 5
    </pre>

    output

    Copy

    <pre id="id0033977819484307115" style="margin: 0px; padding: 0.25em; font-size: 12.6px; font-family: Consolas, "Lucida Console", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier; line-height: 1.25em; white-space: pre-wrap; word-wrap: break-word; color: rgb(136, 0, 0); background-color: rgb(239, 239, 239);">Yes
    4
    1 2
    1 3
    1 4
    1 5
    </pre>

    Note

    The tree from the first example is shown on the picture below: image

    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: image

    We can show that there are no valid decompositions of this tree.

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

    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。
    • 思路: 只有两种情况输出Yes,一种是一条链,即只有两个点出(入)度为1,其余皆为2;另一种是有h个点出(入)度为1,而只有一个点出(入)度为h,且如果无第二个出(入)度大于2的点,那么最大出(入)度点即为所有链的起(终)点。
      另外,此题是无向的,无需记录链上点的先后顺序。

    代码:

    #include <iostream>
    #include<cstdlib>
    #include<stdio.h>
    #include<algorithm>
    #include<set>
    #include<vector>
    #include<assert.h>
    #include<string>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    #define rep(i,a,n) for (int i=a;i<=n;i++)
    #define per(i,a,n) for (int i=n;i>=a;i--)
    #define pb push_back
    #define mp make_pair
    #define all(x) (x).begin(),(x).end()
    #define fi first
    #define se second
    #define SZ(x) ((int)(x).size())
    typedef long long ll;
    const ll mod = 1000000007;
    ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
    
    const int maxn = 1e5+5;
    
    int a[maxn];
    int b[maxn];
    
    int main(){
        int n;
        int x,y;
        memset(a,0,sizeof(a));
        scanf("%d", &n);
        rep(i,1,n-1){
            scanf("%d%d", &x,&y);
            a[x]++;
            a[y]++;
        }
        int sign;
        int s1 = 0,s2 = 0,m = 0;
        rep(i,1,n){
            if(a[i] == 1){
                s1++;
                b[s1] = i;
            }
            if(a[i]>m){
                 m = a[i];
                 sign = i;
            }
            if(a[i] > 2) s2++;
        }
        if(s2>1){
            printf("No");
        }else if(s1 == 2){
            printf("Yes\n%d\n", 1);
            printf("%d %d",b[1],b[2] );
        }else{
            printf("Yes\n%d\n",m);
            rep(i,1,s1){
                printf("%d %d",sign,b[i]);
                if(i != s1) printf("\n");
            }
        }
        
        return 0;
    } 
    

    相关文章

      网友评论

          本文标题:(思维)A - Useful Decomposition Cod

          本文链接:https://www.haomeiwen.com/subject/hjggmftx.html