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条臭 ...
随机推荐
- PHP 接收筛选项包含0的select下拉菜单的处理
这种情况下,PHP的判断方法如下: $where = "1=1"; if ($get['status'] !== '' && $get['status'] !== ...
- SVN: is scheduled for addition, but is missing
删除文件夹后点commit提交,但是报错,报错内容如下:提示 "svn: Commit failed (details follow): svn: '/***/xxx.c' is sched ...
- POJ:2049Finding Nemo(bfs+优先队列)
http://poj.org/problem?id=2049 Description Nemo is a naughty boy. One day he went into the deep sea ...
- soapUI-JDBC Request
1.1.1 JDBC Requet 1.1.1.1 概述 – JDBC Request Option Description JDBC Request TestStep Toolbar 对JDB ...
- Python线程,进程,携程,I/O同步,异步
只有本人能看懂的-Python线程,进程,携程,I/O同步,异步 举个栗子: 我想get三个url,先用普通的for循环 import requests from multiprocessing im ...
- 2:3 Action的配置
< 一 作用> 一:封装工作单元(相当于是控制层,封装出modelAndView) 二:定义name属性接受前台传过来的数据,再定义message属性,用于存放返回前台页面展示的数 据,实 ...
- Verilog篇(三)仿真原理
首先引入一个例子: `timescale 1ns/100ps module TB; ...
- MFC六大核心机制之四:永久保存(串行化)
永久保存(串行化)是MFC的重要内容,可以用一句简明直白的话来形容其重要性:弄懂它以后,你就越来越像个程序员了! 如果我们的程序不需要永久保存,那几乎可以肯定是一个小玩儿.那怕我们的记事本.画图等小程 ...
- 1初识QT
创建程序步骤: 打开Qt Creator 界面选择 New Project或者选择菜单栏 [文件]-[新建文件或项目]菜单项 弹出New Project对话框,选择Qt Widgets Applica ...
- 首屏渲染时间获取 performance.now()
Performance — 前端性能监控利器 最近在写一个监控脚本,终于有机会接触到了这一块,整理后写下了本文.Performance是一个做前端性能监控离不开的API,最好在页面完全加载完成之后 ...