HUD5423 Rikka with Tree(DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423

Rikka with Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 321    Accepted Submission(s): 162

Problem Description

As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
For a tree T
, let F(T,i)
be the distance between vertice 1 and vertice i
.(The length of each edge is 1).
Two trees A
and B
are similiar if and only if the have same number of vertices and for each i
meet F(A,i)=F(B,i)
.
Two trees A
and B
are different if and only if they have different numbers of vertices or there exist an number i
which vertice i
have different fathers in tree A
and tree B
when vertice 1 is root.
Tree A
is special if and only if there doesn‘t exist an tree B
which A
and B
are different and A
and B
are similiar.
Now he wants to know if a tree is special.
It is too difficult for Rikka. Can you help her?

Input

There are no more than 100 testcases.
For each testcase, the first line contains a number n(1≤n≤1000)
.
Then n−1
lines follow. Each line contains two numbers u,v(1≤u,v≤n)
, which means there is an edge between u
and v
.

Output

For each testcase, if the tree is special print "YES" , otherwise print "NO".

Sample Input

3

1 2
2 3

4

1 2
2 3
1 4

Sample Output

YES

NO

此题满足条件的只有一种情况,即: 树从上到下的结点数依次为: 1, 1, 1, ,,,,x(x为任意). 也即是说,只有最后一层的结点数才能大于 1 .

dfs 求出每层的结点数, 就能判断出答案。

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

bool ok;
vector<int> a[1010];
int num[1010];

void dfs(int u, int fa, int d)
{
    num[d]++;
    int len = a[u].size();
    for(int i=0; i<len; i++)
    {
        if(a[u][i]==fa) continue;
        dfs(a[u][i], u, d+1);
    }
}

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        for(int i=0; i<1010; i++)
        a[i].clear();
        memset(num, 0, sizeof(num));
        for(int i=1; i<n; i++)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            a[x].push_back(y);
            a[y].push_back(x);
        }
        ok = false;
        dfs(1, -1, 1);
        for(int i=1; i<1010; i++)
        {
            if(num[i-1]>1&&num[i])
            ok = true;
        }
        if(ok) printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}
时间: 2024-11-07 11:42:23

HUD5423 Rikka with Tree(DFS)的相关文章

hdu 5423 Rikka with Tree(dfs)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: For a tree T, let F(T,i) be the distance between vertice 1 and vertice i.(The length of

hdu 5423 Rikka with Tree(dfs)bestcoder #53 div2 1002

题意: 输入一棵树,判断这棵树在以节点1为根节点时,是否是一棵特殊的树. 相关定义: 1.  定义f[A, i]为树A上节点i到节点1的距离,父节点与子节点之间的距离为1. 2.  对于树A与树B,如果A与B的节点数相同,且无论i为何值,f[A, i]与f[B, i]都相等,则A与B为两棵相似的树. 3.  对于一棵树A,在以节点1为根节点的情况下,如果不存在与其它树与A相似,则A是一棵特殊的树. 输入: 包含多组输入样例. 每组输入样例中首先输入一个整数n,表示一棵含有n个节点的树. 接下来n

hdu5423 Rikka with Tree(DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423 题目大意:BC round# 53上有中文题意= = 思路:画图可以发现,其实题目本身不难.因为他有n个点,n条边,所以可以确定只有一棵树. 对于树中的某一个点来说,如果他有多个分支,然后分支下面又有边连出去,那么这种情况就一定是NO了. 比如1和2.3相连,然后2又与4相连,3与5相连,那么4和5就可以调换位置,此时这颗树就不是特殊的了. 所以我可以利用深搜记录下所有点的深度,对深度排序以后

ACM学习历程—HDU5423 Rikka with Tree(搜索)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: For a tree T, let F(T,i) be the distance between vertice 1 and vertice i.(The length of

HDU - 5202 - Rikka with string (DFS)

Rikka with string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 214    Accepted Submission(s): 109 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation

hdu Rikka with string (dfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char a[1000+100]; int vis[1000+100]; int n; int cnt; int ok; bool huiwen() { int l=0; int r=strlen(a)-1; while(l<r) { if(a[l]!=a[r])

POJ 3321 Apple Tree (dfs+线段树)

题目大意: 修改树上的节点,然后求子树的和. 思路分析: dfs 重新编号,烂大街了... #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 100005 #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e using namespace std

CodeForces - 383C Propagating tree(dfs + 线段树)

题目大意: 给出一棵树,树上每个节点都有权值,然后有两个操作. 1 x val 在结点x上加上一个值val,x的儿子加上 -val,x的儿子的儿子加上 - (-val),以此类推. 2 x 问x节点的值. 思路分析: 每个节点上加值都是给自己的儿子节点加,而且这个是颗树. 比如样例上的,如果你给node 1加一个值,那么五个节点都加. 再给node 2加个值,2的儿子节点也加了,之前给1加的值也要加到2号节点的儿子. 所以你会发现节点的儿子会存在一个从属的关系. 这样的话,我们可以把所有节点从新

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names