Is It A Tree?

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

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
 
判断有向图是否为树:
1,每个点的入度都不大于一
2,已经标记的点树根是自己的数量只能等于1,不然就是森林
3,不能成环
4,0 0也是树
package 并查集;

import java.util.Scanner;

public class hdu_1325 {
static int [] father;
static int [] indegree;
static boolean [] flag;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = 1;
while(true){
boolean istree=true;
father = new int [1000];
indegree = new int [1000];
flag = new boolean [1000];
int x = sc.nextInt();
int y =sc.nextInt();
if(x<0&&y<0) break;
if(x==0&&y==0){ //空树也是树
System.out.println("Case "+(k++)+" is a tree.");
continue;
}
for(int i=1;i<1000;i++){
father[i] = i;
}
while(true){
if(x==0&&y==0) break;
int a = find(x);
int b = find(y);
flag[x] = true;
flag[y] = true;
if(a!=b) {
father[a] = b;
indegree[y]++; //入度加一
}
if(a==b) istree = false; //成环
x = sc.nextInt();
y = sc.nextInt();
}
int ans = 0;
for(int i=1;i<1000;i++){
if(indegree[i]>1) {
istree = false; //度大于一
}
if(flag[i]&&father[i]==i) ans++; //这个点被标记过了并且自己还是一棵树的根
if(ans>1) istree = false; //森林
}
if(istree) System.out.println("Case "+(k++)+" is a tree.");
else System.out.println("Case "+(k++)+" is not a tree.");
}
}
private static int find(int x) {
if(x==father[x]) return x;
return find(father[x]);
}
}

hdu 1325(并查集)的更多相关文章

  1. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  2. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  3. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  4. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  5. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  6. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  7. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  8. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

  9. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

随机推荐

  1. XML 树结构,语法规则,元素,属性,验证及其解析

    XML 文档形成了一种树结构,它从"根部"开始,然后扩展到"枝叶". 一个 XML 文档实例 XML 文档使用简单的具有自我描述性的语法: <?xml v ...

  2. win7无法登陆linux samba共享

    网上查了一下资料,总共有以下几种做法: 1.防火墙 2. Open the Run command and type "secpol.msc". Press "conti ...

  3. Android SearchView设置与用法的那点事儿

    // 设置该SearchView默认是否自动缩小为图标 mSearchView.setIconifiedByDefault(false); // 为该SearchView组件设置事件监听器 mSear ...

  4. vijos 1069 新年趣事之红包 Prim水题

    描述 xiaomengxian一进门,发现外公.外婆.叔叔.阿姨……都坐在客厅里等着他呢.经过仔细观察,xiaomengxian发现他们所有人正好组成了一个凸多边形.最重要的是,他们每个人手里都拿着一 ...

  5. salt总结

    安装jdk jdk: file.managed: - source: salt://service/zabbix/files/jdk1.8.0_121.tar.gz - name: /usr/loca ...

  6. 【vijos】P1448 校门外的树

    [题意]两种操作,[L,R]种新的树(不覆盖原来的),或查询[L,R]树的种类数.n<=50000. [算法]树状数组||线段树 [题解]这题可以用主席树实现……不过因为不覆盖原来的,所以有更简 ...

  7. Yii2实现读写分离(MySQL主从数据库)

    读写分离(Read/Write Splitting). 1.原理: 让主数据库(master)处理事务性增.改.删操作(INSERT.UPDATE.DELETE),而从数据库(slave)处理SELE ...

  8. pythonTensorFlow实现yolov3训练自己的目标检测探测自定义数据集

    1.数据集准备,使用label标注好自己的数据集. https://github.com/tzutalin/labelImg 打开连接直接下载数据标注工具, 2.具体的大师代码见下链接 https:/ ...

  9. kolakoski序列

                   搜狐笔试=.= 当时少想一个slow的指针..呜呜呜哇的一声哭出来 function kolakoski(token0, token1) { token0 = token ...

  10. Windows平台下搭建Git服务器的图文教程

    Git没有客户端服务器端的概念,但是要共享Git仓库,就需要用到SSH协议(FTP , HTTPS , SFTP等协议也能实现Git共享,此文档不讨论),但是SSH有客户端服务器端,所以在window ...