zoukankan      html  css  js  c++  java
  • Codeforces Round #397 E题Tree Folding(BFS)解题报告

    E. Tree Folding
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    Vanya wants to minimize a tree. He can perform the following operation multiple times: choose a vertex v, and two disjoint (except for v) paths of equal length a0 = va1, ..., ak, and b0 = vb1, ..., bk. Additionally, vertices a1, ..., akb1, ..., bk must not have any neighbours in the tree other than adjacent vertices of corresponding paths. After that, one of the paths may be merged into the other, that is, the vertices b1, ..., bk can be effectively erased:

    Help Vanya determine if it possible to make the tree into a path via a sequence of described operations, and if the answer is positive, also determine the shortest length of such path.

    Input

    The first line of input contains the number of vertices n (2 ≤ n ≤ 2·105).

    Next n - 1 lines describe edges of the tree. Each of these lines contains two space-separated integers u and v (1 ≤ u, v ≤ nu ≠ v) — indices of endpoints of the corresponding edge. It is guaranteed that the given graph is a tree.

    Output

    If it is impossible to obtain a path, print -1. Otherwise, print the minimum number of edges in a possible path.

    Examples
    input
    6
    1 2
    2 3
    2 4
    4 5
    1 6
    output
    3
    input
    7
    1 2
    1 3
    3 4
    1 5
    5 6
    6 7
    output
    -1
    Note

    In the first sample case, a path of three edges is obtained after merging paths 2 - 1 - 6 and 2 - 4 - 5.

    It is impossible to perform any operation in the second sample case. For example, it is impossible to merge paths 1 - 3 - 4 and 1 - 5 - 6, since vertex 6 additionally has a neighbour 7 that is not present in the corresponding path.

    从每个叶结点开始走。如果某点连的不同子长度个数大于等于3则不存在。

     1 #include <iostream>
     2 #include<bits/stdc++.h>
     3 #include <queue>
     4 #include <cstdio>
     5 #include <cstring>
     6 #include <algorithm>
     7 using namespace std;
     8 typedef long long ll;
     9 typedef unsigned long long ull;
    10 const int MAX=2e5+5;
    11 set <int> edge[MAX];
    12 set <int> dis[MAX];
    13 queue <int> que;
    14 bool vi[MAX];
    15 int re(int x)
    16 {
    17     while(x%2==0)
    18         x/=2;
    19     return x;
    20 }
    21 int solve()
    22 {
    23     memset(vi,false,sizeof(vi));
    24     while(!que.empty())
    25     {
    26         int u=que.front();
    27         que.pop();
    28         if(vi[u])
    29             continue;
    30         if(edge[u].size()==1)
    31         {
    32             if(dis[u].size()==1)
    33             {
    34                 int v=*edge[u].begin();
    35                 edge[u].erase(v);
    36                 edge[v].erase(u);
    37                 dis[v].insert(*dis[u].begin()+ 1 );
    38                 que.push(v);
    39                 vi[u]=true;
    40             }
    41         }
    42         else if(edge[u].empty())
    43         {
    44             if(dis[u].size()>=3)
    45             {
    46                 return -1;
    47             }
    48             if(dis[u].size()==1)
    49                 return re(*dis[u].begin());
    50             return re(*dis[u].begin()+*dis[u].rbegin());
    51 
    52         }
    53 
    54     }
    55     return -1;
    56 }
    57 int n;
    58 int main()
    59 {
    60     scanf("%d",&n);
    61     int i;
    62     int u,v;
    63     for(i=1;i<=n-1;i++)
    64     {
    65         scanf("%d %d",&u,&v);
    66         edge[u].insert(v);
    67         edge[v].insert(u);
    68     }
    69     for(i=1;i<=n;i++)
    70     {
    71         if(edge[i].size()==1)
    72         {
    73             que.push(i);
    74             dis[i].insert(0);
    75         }
    76     }
    77     printf("%d
    ",solve());
    78 }
  • 相关阅读:
    Springboot源码 bean的注册
    Vue源码之 watch
    Vue源码之 slot
    Vue computed 的嵌套
    Vue的子组件绑定的方法中传入自定义参数
    Vue源码之 Vue的生命周期
    Vue源码之-----computed
    Vue源码之----为什么Vue中Array的pop,push等方法可以reactive,而Array[0]='a'这样的方法不会reactive?
    ReSharper 8.1支持Visual Studio 2013的特色——超强滚动条
    Python开发环境Wing IDE使用教程:部分调试功能介绍
  • 原文地址:https://www.cnblogs.com/quintessence/p/6399618.html
Copyright © 2011-2022 走看看