Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33083 Accepted Submission(s): 7574

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1325

Problem Description

A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.

There is exactly one node, called the root, to which no directed edges point.

Every node except the root has exactly one edge pointing to it.

There is a unique sequence of directed edges from the root to each node.

For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.

In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.

Input

The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.

Output

For each test case display the line Case k is a tree." or the line Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).

Sample Input

6 8 5 3 5 2 6 4

5 6 0 0

8 1 7 3 6 2 8 9 7 5

7 4 7 8 7 6 0 0

3 8 6 8 6 4

5 3 5 6 5 2 0 0

-1 -1

Sample Output

Case 1 is a tree.

Case 2 is a tree.

Case 3 is not a tree.

Source

North Central North America 1997

题意

给你一些有向边,问你这些点是否构成一颗有根树。

题解

有根树特点:

1.除根点入度为0外,其余点入度均为1

2.没有环

无根树特点:

1.没有环

2.联通

奇环内向数特点:

1.入度全为1

2.联通

我们要做的是有根树,那么用并查集处理环,用set或者map处理每个点的入度即可。

代码

#include<bits/stdc++.h>
using namespace std;
#define N 100050
int find(int fa[],int x)
{
if (x==fa[x])return x;
return fa[x]=find(fa,fa[x]);
}
bool pd()
{
static int f[N],bl[N];
set<int>Q;
memset(f,0,sizeof(f));
for(int i=1;i<N;i++)bl[i]=i;
int num=0,x,y,flag=1;
while(1)
{
scanf("%d %d",&x,&y);
if (x+y==0)break;
if (x<0)exit(0);
Q.insert(x);
Q.insert(y);
f[y]++;
int fx=find(bl,x),fy=find(bl,y);
if (fx==fy||f[y]>1)flag=0;
bl[fx]=fy;
}
for(auto it=Q.begin();it!=Q.end();it++) if (f[*it]==0)num++;
if (flag&&num<=1)return 1;
return 0;
}
void work()
{
static int cas=0;
bool k=pd();
printf("Case %d is ",++cas);
if (!k)printf("not ");
printf("a tree.\n");
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
while(1)work();
}

HDU 1325 有根树的判断的更多相关文章

  1. hdu 1325 判断有向图是否为树

    题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 #include<cstdio> #include<iostream& ...

  2. hdu 2108:Shape of HDU(计算几何,判断多边形是否是凸多边形,水题)

    Shape of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. (并查集)POJ 1308 & HDU 1325

    一开始以为两道题是一样的,POJ的过了直接用相同代码把HDU的交了,结果就悲剧了.最后发现HDU的没有考虑入度不能大于一. 题意:用树的定义来 判断吧,无环,n个结点最多有n-1条边,不然就会有环.只 ...

  4. HDU 1325,POJ 1308 Is It A Tree

    HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...

  5. hdu 1325 && poj 1308 Is It A Tree?(并查集)

    Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...

  6. HDU 1878 欧拉回路(判断欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878 题目大意:欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一 ...

  7. HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 Brit ...

  8. HDU 1325(并查集)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. hdu 3622(二分+2-sat判断可行性)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3622 思路:二分是容易想到的,由于题目中有明显的矛盾关系,因此可以用2-sat来验证其可行性.关键是如 ...

随机推荐

  1. SDUT2176 -> 递归的函数

    递归的函数                                         Time Limit: 1000 msMemory Limit: 65536 KiB Problem Des ...

  2. c#简单的SQLHelp

    public abstract class SQLHelper { //只读的静态数据库连接字符串 //需添加引用System.Configuration; public static readonl ...

  3. dosbox+masm汇编环境的安装和使用

    1. 下载dosbox安装程序:DOSBox0.74-win32-installer.exe 链接:https://pan.baidu.com/s/1gXPKTT-xKb6BpjOJdhmudA 密码 ...

  4. FOFA 批量采集url 图形化界面编写

    这是脚本 # coding:utf- import requests,re import time import sys import getopt import base64 guizhe='' s ...

  5. Codeforces 23A You're Given a String...

    A. You're Given a String... time limit per test 2 seconds memory limit per test 256 megabytes input ...

  6. 浅谈 es6 箭头函数, reduce函数介绍

    今天来谈一下箭头函数, es6的新特性 首先我们来看下箭头函数长什么样子, let result = (param1, param2) => param1+param2; 上述代码 按照以前书写 ...

  7. Redis使用Docker镜像安装

    详细见本人以下文档: https://www.cnblogs.com/zyc-blogs/p/9621727.html

  8. ReactiveCocoa实践

    1.按钮addTarget [[self.aDepositBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNe ...

  9. Linux 之Shell for循环

    @代表所有参数所以如果后面跟上echo $v你会发现他会一次显示user userdebug eng $poo -le ${#prodlist[@]} 这句话是说 $poo小于等于prodlist中的 ...

  10. 转载:OutOfMemoryError系列(2): GC overhead limit exceeded

    这是本系列的第二篇文章, 相关文章列表: OutOfMemoryError系列(1): Java heap space OutOfMemoryError系列(2): GC overhead limit ...