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. SIGCHLD函数

    SIGCHLD的产生条件 子进程终止时 子进程接收到SIGSTOP信号停止时 子进程处在停止态,接受到SIGCONT后唤醒时 借助SIGCHLD信号回收子进程 子进程结束运行,其父进程会收到SIGCH ...

  2. [secureCRT] 关闭secureCRT的auto print功能

    说明:每次不小心按到secureCRT的auto print时,secure总是会自动打印,结果打印出一堆没用的日志.....切换选项页时不小心按到的,还是挺浪费纸的.怎样才能关闭这个功能呢 实现:想 ...

  3. qemu for win64 下载

    下载地址:https://qemu.weilnetz.de/w64/ 安装完成后,将安装目录加入到系统环境变量.

  4. HDU 4386 Quadrilateral(四边形的海伦公式的应用)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=115760#problem/G 题目大意是给出四条边,问能否组成一个四边形,如果 ...

  5. Js 之复制到剪贴板 clipboard.js

    一.下载 https://github.com/zenorocha/clipboard.js/archive/master.zip 二.Demo示例 <!DOCTYPE html> < ...

  6. mongodb高可用部署linux

    准备三台服务器,部署方案如下: 1.安装mongodb,详细不用说: 2.创建实例分别创建目录config.configServer.shard1.shard2.shard3.logs,分别存放实例配 ...

  7. 学PHP应注意的问题与知识点

    编出一手好代码,这个是需要你在平时开发中日积月累的,平时如果你有注意到以下的那些代码的编码,那么祝贺你,你在技能提升这方面已经垫下了一些基础,编写出一手好代码,说白了就是你特么注意到性能这块的问题,代 ...

  8. Linux下批量ping某个网段的脚本

    比如现在需要对192.168.0.0/24网段的ip进行检查,检查哪些ip现在被占用,哪些ip没有被占用,可以通过ping命令来检查,也可以通过nmap接参数来检查 ping命令脚本如下: [root ...

  9. Android与JS交互,json传参问题

    一.JS调用Android的方法 JS调用安卓的方法,并且传递的参数为json格式的字符串(JSONObject.toString()), 例如: var json = {"name&quo ...

  10. Android:高通平台性能调试

    1.GPU / CPU 信息打印脚本 gpu_cpu_info.bat @echo off echo "==================GPU====================== ...