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. SGU - 282

    SGU - 282 题解 题意: 本质不同的集合:不存在两个方案重新编号之后对应的边集相同(对于所有x,y,,(x,y)边颜色都相同). (1≤ N≤ 53, 1≤ M≤ 1000) 对P取模 本质不 ...

  2. Semphore信号量的使用

    前言:在多线程环境的同步中,我们为了让每个线程具有同步的作用,经常采用synchronize.reetrantlock等同步手段进行上锁,以便在同一时间只能有一个线程具有访问变量和读写变量的权力.然而 ...

  3. jquery实现奇偶行赋值不同css值

    <html> <head> <title>jquery奇偶行css效果</title> <script src="../../jquer ...

  4. 背景建模技术(四):视频分析(VideoAnalysis)模块

    视频分析模块主要包含两个函数,一个是VideoAnalysis::setup(....),其主要功能就是确定测试的视频是视频文件或摄像头输入亦或是采用命令行参数:第二个函数是VideoAnalysis ...

  5. Ubuntu16.04 U盘安装Ubuntu16.04制作 光盘刻录 安装与简介

    从今天开始,我会把我遇到过的技术问题一一记录下来,从而分享给有需要的朋友,尽量希望你们少走弯路! 一.首先从官网上下载Ubuntu16.04镜像,下载最好从官网上下载(http://www.ubunt ...

  6. git与svn与github与码云的区别

    1.git与github(https://www.oschina.net/)的区别 Git(https://git-scm.com/)是一个版本控制工具 github是一个用git做版本控制的项目托管 ...

  7. ios开关按钮

    .al-toggle-button{ appearance: none; -webkit-appearance: none; position: relative; width: 52px; heig ...

  8. 文件上传文件的权限--lnmp 环境配置,尤其整个项目复制过来动~~~

    site_upload  需要是自己才建立动才会是root  root  所有者:---不是root :  root   上传不了文件,不是  777 就都可以上传的,也要看看是谁建立的文件夹: 打包 ...

  9. git fatal: 拒绝合并无关的历史的错误解决

    本地初始化的项目 与 github 版本不一致, 导致无法提交 $ git pull origin master 来自 https://github.com/itaken/python-login-d ...

  10. Activity及Intent

    1.Activity 在一个Android应用程序中,Activity是为用户操作而展示的可视化界面.比如你要打电话,这个时候的拨号界面就是一个Activity,你要发短信给你的女朋友,这个短信窗口就 ...