hdu 1325 Is It A Tree? 并查集
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
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.
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
Case 2 is a tree.
Case 3 is not a tree.
思路:给你一个树,判断是不是树;并查集只要判断是否只有一个集合;并没有说是从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? 并查集的更多相关文章
- 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 ... 
- hdu 5458 Stability(树链剖分+并查集)
		Stability Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total ... 
- [HDU 3712] Fiolki (带边权并查集+启发式合并)
		[HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ... 
- HDU 5606 tree 并查集
		tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ... 
- tree(并查集)
		tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ... 
- hdu 5652 India and China Origins 并查集
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ... 
- Is It A Tree?(并查集)
		Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ... 
- CF109 C. Lucky Tree  并查集
		Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ... 
- 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条臭 ... 
随机推荐
- [py]django第三方分页器django-pure-pagination实战
			第三方分页模块: django-pure-pagination 是基于django的pagination做的一款更好用的分页器 参考 配置django-pure-pagination模块 安装 pip ... 
- oracle 之创建用户,表空间,授权,修改用户密码
			1.创建表空间 create tablespace ilinkcargoagent logging datafile 'D:\app\Administrator\oradata\ilinkcargoa ... 
- 从Maven仓库中导出jar包
			从Maven仓库中导出jar包:进入工程pom.xml 所在的目录下,输入以下命令:mvn dependency:copy-dependencies -DoutputDirectory=lib更简单的 ... 
- ios一些问题
			多线程,加锁,如何互斥. http里面的get put post的差别 sockect tcp udp 
- iOS UI基础-5.0 QQ框架(Storyboard)
			1.拉入TabBarController和4个Navigation 2.TabBarController关联Navigation 3.设置消息,拉入一个Button,设置背影 4.联系人,拉入一个Se ... 
- MVC5+Easyui1.3.6+EF6 开发部分备忘笔记
			一点一点增加,后面继续. 1.Row Editing in DataGrid 编辑,总是绑定不了checkbox的问题 
- php传值,传地址,传引用的区别
			传值, 是把实参的值赋值给行参 那么对行参的修改,不会影响实参的值 传地址 是传值的一种特殊方式,只是他传递的是地址,不是普通的如int 那么传地址以后,实参和行参都指向同一个对象 传 ... 
- checkBox的使用和事件监听
			直接上代码: <!DOCTYPE html> <html> <head> <title></title> </head> < ... 
- 【安装vsftpd】安装vsftpd工具步骤
			1 安装vsftpd组件 [root@bogon ~]# yum -y install vsftpd 安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件. 2 添 ... 
- php PDO遇到的坑
			<?php $dbConn = new PDO( "mysql:host=localhost;dbname=adtuu",'root','root', array( // 强 ... 
