Is It A Tree?

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

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开始

    坑点,这是有向的;

1 2 3 2 0 0 //is not tree;

代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
#define m 9973
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
int father[];
int flag[];
int rudu[];
int findfa(int x)
{
return x==father[x]?x:father[x]=findfa(father[x]);
}
void init()
{
for(int i=;i<=;i++)
father[i]=i,flag[i]=,rudu[i]=;
}
int hebing(int u,int v)
{
int xx=findfa(u);
int yy=findfa(v);
if(xx==yy)
return ;
father[xx]=yy;
return ;
}
int check(int x)
{
int i,t;
for(i=;i<=x;i++)
if(flag[i])
break;
for(t=i;t<=x;t++)
if(flag[t]&&findfa(i)!=findfa(t))
return ;
return ;
}
int check1(int x)
{
int ji=;
int lu=;
for(int i=;i<=x;i++)
{
if(flag[i])
{
if(rudu[i]==)
ji++;
else if(rudu[i]>)
return ;
}
}
if(ji!=)
return ;
return ;
}
int main()
{
int x,z,i,t;
int u,v;
int casee=;
while(scanf("%d%d",&u,&v)!=EOF)
{
if(u<||v<)break;
init();
int flagg=,maxx=;
while()
{
maxx=max(maxx,max(u,v));
if(u==&&v==)break;
flag[u]=;
flag[v]=;
rudu[v]++;
if(!hebing(u,v))
flagg=;
scanf("%d%d",&u,&v);
}
printf("Case %d ",casee++);
if(flagg)
printf("is not a tree.\n");
else if(check(maxx)&&check1(maxx))
printf("is a tree.\n");
else printf("is not a tree.\n");
}
return ;
}

hdu 1325 Is It A Tree? 并查集的更多相关文章

  1. Hdu.1325.Is It A Tree?(并查集)

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

  2. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

  3. [HDU 3712] Fiolki (带边权并查集+启发式合并)

    [HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...

  4. HDU 5606 tree 并查集

    tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并 ...

  5. tree(并查集)

    tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  6. hdu 5652 India and China Origins 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...

  7. Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 De ...

  8. CF109 C. Lucky Tree 并查集

    Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...

  9. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

随机推荐

  1. Scala中的数组和集合操作

    package test /* 1.在scala集合中,Iterable是共同的Trait,Iterable要求继承者实现一些共同的方法,例如元素的遍历 * 2.Array是scala基础的数据结构, ...

  2. SqlAlchemy个人学习笔记完整汇总-转载

    使用 sqlalchemy 有3种方式: 方式1, 使用raw sql; 方式2, 使用SqlAlchemy的sql expression; 方式3, 使用ORM.   前两种方式可以统称为 core ...

  3. 国内maven库

    国内maven库 <mirrors> <mirror> <id>CN</id> <name>OSChina Central</name ...

  4. [lr] 常用快捷键

    界面基本操作 F5               : 隐藏/显示上部面板 F6               : 隐藏/显示下部面板 F7               : 隐藏/显示左部面板 F8     ...

  5. 机器学习 python库 介绍

    开源机器学习库介绍 MLlib in Apache Spark:Spark下的分布式机器学习库.官网 scikit-learn:基于SciPy的机器学习模块.官网 LibRec:一个专注于推荐算法的j ...

  6. html06

    1.全局函数alert();console.log();parseInt();parseString();isNaN();eval(); 用于计算表达式字符串 用于执行字符串中的js代码 eval(& ...

  7. OBV_X3

    {OBV_X3[背景]考虑到OBV_X03在情况1的时候,采用的是寻找波段线的同价K线,但是由于此种情况下必须使用CONST(C)或通过输入参数CONSTCC设定固定值,无法当前K线的CLOSE同时变 ...

  8. c语言的字符串拷贝函数的精简

    #include <stdio.h>#include <string.h>void str_cpy(char * to, char *from){    while ((*to ...

  9. sql server数字转字符串出现科学计数法

    在从excel往sql server导入数据,电话.编号等数字呈现float类型,然后向b表中insert后(phone为nvarchar)出现科学计数法,解决方法:需将float等数据类型转为标准的 ...

  10. python 爬取html页面

    #coding=utf-8 import urllib.request def gethtml(url): page=urllib.request.urlopen(url) html=page.rea ...