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条臭 ...
随机推荐
- Elasticsearch Java API—多条件查询(must)
多条件设置 //多条件设置 MatchPhraseQueryBuilder mpq1 = QueryBuilders .matchPhraseQuery("pointid",&qu ...
- windows7触屏操作API
每当用户触摸触敏式 Windows 7 设备时,Windows 7 多点触控平台都会向您的应用程序发送手势消息 WM_GESTURE.这是现成的免费行为,如果您希望停止接收此类消息,则需要选择退出. ...
- python -- 解决If using all scalar values, you must pass an index问题
[问题描述] 在将dict转为DataFrame时会报错:If using all scalar values, you must pass an index 例如: summary = pd.Dat ...
- input的text输入框设置大一点
<input type="text" style="height:51px;width:449px;font-size:12px;">12可以随意改 ...
- http 同步异步请求
在用户交互模式下,当你改变表单中某个组件的值时, 譬如你填写名字.修改性别.选择爱好的时候,浏览器和服 务器至今没有发生任何交互,只有当你点击submit的时候, 浏览器才会把你的参数,也就是form ...
- python绘图之seaborn 笔记
前段时间学习了梁斌老师的数据分析(升级版)第三讲<探索性数据分析及数据可视化>,由于之前一直比较忙没有来得及总结,趁今天是周末有点闲暇时间,整理一下笔记: 什么是seaborn Seabo ...
- yii2csrf攻击
第一种解决办法是关闭Csrf public function init(){ $this->enableCsrfValidation = false; } 第二种解决办法是在form表单中加入隐 ...
- ROS知识(2)----理解ROS系统结构
学习新事物,方法高于技术本身,如果没有把握"BIG PICTURE"的话很难理解进去.通过以下几点进行理解ROS: ROS实际上不是操作系统,他只是一个通信的框架,一个代码管理的架 ...
- MD5验签同一字符串得到不同的MD5签名值可能问题之一
public static String md555(String plainText) throws UnsupportedEncodingException { byte[] secretByte ...
- jar与war 包解压
JAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包.当然也是有区别的,JAR包中有一个META-INF\MANIFEST.MF文件,当你找成JAR包时,它会自动生成.JAR包是 ...