Codeforces Round #518 (Div. 1) [Thanks, Mail.Ru!] B. Multihedgehog (多叉树)

Someone give a strange birthday present to Ivan. It is hedgehog — connected undirected graph in which one vertex has degree at least 3 (we will call it center) and all other vertices has degree 1. Ivan thought that hedgehog is too boring and decided to make himself k-multihedgehog.

Let us define k-multihedgehog as follows:

1-multihedgehog is hedgehog: it has one vertex of degree at least 3 and some vertices of degree 1.
For all k≥2, k-multihedgehog is (k−1)-multihedgehog in which the following changes has been made for each vertex v with degree 1: let u be its only neighbor; remove vertex v, create a new hedgehog with center at vertex w and connect vertices u and w with an edge. New hedgehogs can differ from each other and the initial gift.
Thereby k-multihedgehog is a tree. Ivan made k-multihedgehog but he is not sure that he did not make any mistakes. That is why he asked you to check if his tree is indeed k-multihedgehog.

Input
First line of input contains 2 integers n, k (1≤n≤105, 1≤k≤109) — number of vertices and hedgehog parameter.

Next n−1 lines contains two integers u v (1≤u,v≤n;u≠v) — indices of vertices connected by edge.

It is guaranteed that given graph is a tree.

Output
Print “Yes” (without quotes), if given graph is k-multihedgehog, and “No” (without quotes) otherwise.

Examples
inputCopy
14 2
1 4
2 4
3 4
4 13
10 5
11 5
12 5
14 5
5 13
6 7
8 6
13 6
9 6
outputCopy
Yes
inputCopy
3 1
1 3
2 3
outputCopy
No

题意:
1层数:第一层只有一个节点,而它有至少3个儿子节点。
2层树:第一层只有一个节点,而它有至少3个儿子节点;第二层所有的节点都至少有3个儿子节点。

k层树:第一层只有一个节点,而它有至少3个儿子节点;第二层所有的节点都至少有3个儿子节点…第k层所有的节点都至少有3个儿子节点。
现在有一颗树,判断是否是此类树。

思路: 由于是无相图,所有无法根据入度和出度判断根节点。可以先找到叶子节点,然后bfs向上搜,找到根节点。
再从根节点向下搜,每次记录当前节点儿子个数,并且记录树的深度。
若非叶子节点的儿子数目 >= 3,并且深度 == k,那么即是Yes。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1000100;
typedef pair<int,int>P;
struct Edge
{
    int u,v,next;
}e[MAXN<<1];
int head[MAXN],cnt;
bool vis[MAXN];
int dep[MAXN];
int in[MAXN];

void addedge(int u,int v)
{
    e[cnt] = {u,v,head[u]};
    head[u] = cnt++;
}

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    if(k >= 14){ cout<<"No"<<endl; return 0;}
    memset(head,-1,sizeof(head));
    for(int i = 1; i < n; ++i){
        int u,v;    scanf("%d%d",&u,&v);
        addedge(u,v);
        addedge(v,u);
        in[v]++;    in[u]++;
    }
    queue<int>q;
    for(int i = 1; i <= n; ++i)
        if(in[i] == 1)  q.push(i),vis[i] = true;
    int root;
    while(!q.empty()){
        int u = q.front();  q.pop();
        root = u;
        for(int i = head[u]; ~i; i = e[i].next){
            if(!vis[e[i].v])   q.push(e[i].v),vis[e[i].v] = true;
        }
    }
    queue<pair<int,int> >que;
    que.push(P(root,-1));
    dep[root] = 0;
    int flag = 0, mx;

    while(!que.empty()){
        int u = que.front().first,fa = que.front().second;  que.pop();
        mx = dep[u];
        int sum = 0;
        for(int i = head[u]; ~i; i = e[i].next){
            if(e[i].v != fa){
                que.push(P(e[i].v,u));
                dep[e[i].v] = dep[u] + 1;
                sum++;
            }
        }
        if(dep[u] + 1 <= k && sum < 3){ flag = 1; break; }

    }
    if(flag) { cout<<"No"<<endl;return 0; }
    if(mx == k) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值