HDU 5423 Rikka with Tree(水题)

Rikka with Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 292    Accepted Submission(s): 149

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

Hint

For the second testcase, this tree is similiar with the given tree:
4
1 2
1 4
3 4

Source

BestCoder Round #53 (div.2)

中文题意:

问题描述

众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:

对于一棵树TT,令F(T,i)F(T,i)为点1到点ii的最短距离(边长是1). 

两棵树AA和BB是相似的当且仅当他们顶点数相同且对于任意的ii都有F(A,i)=F(B,i)F(A,i)=F(B,i).

两棵树AA和BB是不同的当且仅当他们定点数不同或者存在一个ii使得以1号点为根的时候ii在两棵树中的父亲不同。

一棵树AA是特殊的当且仅当不存在一棵和它不同的树和它相似。

现在勇太想知道一棵树到底是不是特殊的。

当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?

输入描述

数据组数不超过100组。每组数据的第一行一个整数n(2 \leq n \leq 1000)n(2≤n≤1000)。

接下来n-1n?1行。每行两个整数u,v(1 \leq u,v \leq n)u,v(1≤u,v≤n),代表给定树上的一条边。

输出描述

对于每一组数据,如果给定树是特殊的输出"YES"否则输出"NO"。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<stdlib.h>

using namespace std;

int n,m;
int v[1010];
int map[1001][1001];

int main() {
    while(scanf("%d",&n)!=EOF) {
        memset(map,0,sizeof(map));
        memset(v,0,sizeof(v));
        int x,y;
        int pf;
        for(int i=0; i<n-1; i++) {
            scanf("%d%d",&x,&y);
            map[x][y] = 1;
            map[y][x] = 1;
            if(x == 1) {
                pf = y;
            } else if(y == 1) {
                pf = x;
            }
            v[x]++;
            v[y]++;
        }
        int rt = 0;
        int cnt = 0,ans = 0,pt = 0;
        for(int i=1; i<=n; i++) {
            if(v[i] == 1) {
                ans++;
            } else if(v[i] == 2) {
                cnt++;
            }else{
                rt = i;
                pt++;
            }
        }
        if(pt>=2){
            printf("NO\n");
            continue;
        }
        if(v[1]!=1) {
            if(v[1] == n-1) {
                printf("YES\n");
            } else {
                printf("NO\n");
            }
        }else{
            if(cnt + ans +1 >= n && v[pf] <=2){
                int flag = 0;
                if(pt>0){
                    for(int i=1;i<=n;i++){
                        if(map[rt][i] == 1 && v[i]!=1){
                            flag++;
                        }
                    }
                }
                if(flag <= 1){
                    printf("YES\n");
                }else{
                    printf("NO\n");
                }

            }else{
                if(v[pf] == n-1){
                    printf("YES\n");
                    continue;
                }
                printf("NO\n");
            }
        }

    }
    return 0;
}

版权声明:本文为博主原创文章,如有特殊需要请与博主联系 QQ : 793977586。

时间: 2024-10-29 03:55:09

HDU 5423 Rikka with Tree(水题)的相关文章

HDU 5423:Rikka with Tree Dijkstra算法

Rikka with Tree Accepts: 207 Submissions: 815 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 对于一棵树TT,令F(T,i)F(T,i)为点1到点ii的最短距离(边长是1). 两棵树AA和BB是相似的当且仅当他们顶点数相同且对于任意的ii都有

hdu 5423 Rikka with Tree 树的性质

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

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

hdu 1999 不可摸数 水题。

不可摸数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7966    Accepted Submission(s): 2024 Problem Description s(n)是正整数n的真因子之和,即小于n且整除n的因子和.例如s(12)=1+2+3+4+6=16.如果任何数m,s(m)都不等于n,则称n为不可摸数. Input 包

hdu 5425 Rikka with Tree II(暴力)

题目链接:hdu 5425 Rikka with Tree II 直接枚举就好了,当概率极小时贡献值可以忽略. #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxn = 1e5 + 5; int N,

HDU Senior&#39;s Gun (水题)

题意:给n把枪,m个怪兽,每把枪可消灭1怪兽,并获得能量=枪的攻击力-怪兽的防御力.求如何射杀能获得最多能量?(不必杀光) 思路:用最大攻击力的枪杀防御力最小的怪兽明显可获得最大能量.如果每把枪都去射杀刚好1点能量都拿不到的怪物,那简直等于把枪全丢掉. 1 //#pragma comment(linker,"/STACK:102400000,102400000") 2 #include <iostream> 3 #include <stdio.h> 4 #inc

HDU 5590 ZYB&#39;s Biology 水题

ZYB's Biology Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5590 Description ZYB(ZJ−267)在NOIP拿到600分之后开始虐生物题,他现在扔给你一道简单的生物题:给出一个DNA序列和一个RNA序列,问它们是否配对. DNA序列是仅由A,C,G,T组成的字符串,RNA序列是仅由A,C,G,U组成的字符串. DNA和RNA匹配当且仅当每

hdu 4847 Wow! Such Doge! 水题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4847 统计文本中一共有多少个“Doge” 水题 #include <cstring> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <cstdio> #includ