hdu 1325 && poj 1308 Is It A Tree?(并查集)
Description
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
Output
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.
跟小希的迷宫差不多,需要注意的是小希的迷宫是无向图,而本题是有向图。
判断是不是一棵树需要的条件如下:
1. 肯定满足只有一个树根,只有一个入度为0的点。
2. 除了根每个点的入度只能为1
3. 无环
4. n个点只能有n-1个边
直接从小希的迷宫进行改的,不过本代码在poj AC ,但是HDU Wrong Answer
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int set[];
bool vis[];
bool flag;
int find(int x)
{
return set[x]==x?x:set[x]=find(set[x]);
}
int main()
{
int a,b,k=;
while(scanf("%d%d",&a,&b))
{
int maxn=-;
if(a<&&b<)
break;
flag=true;
if(a==&&b==)
{
printf("Case %d is a tree.\n",++k);
continue;
}
for(int i=; i<; i++)
{
set[i]=i;
vis[i]=false;
}
while(a||b)
{
maxn=max(maxn,max(a,b));
vis[a]=true,vis[b]=true;
int x=find(a);
int y=find(b);
if(x!=y)
{
// if(x<y)
set[x]=y;
//else
//set[y]=x;
}
else
flag=false;
scanf("%d%d",&a,&b);
}
int ans=;
for(int i=; i<=maxn; i++)
{
if(vis[i] && set[i]==i)
ans++;
if(ans>)
flag=false;
}
if(flag)
printf("Case %d is a tree.\n",++k);
else
printf("Case %d is not a tree.\n",++k);
}
return ;
}
hdu 1325 && poj 1308 Is It A Tree?(并查集)的更多相关文章
- POJ 1308 Is It A Tree? (并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24237 Accepted: 8311 De ...
- HDU ACM 1325 / POJ 1308 Is It A Tree?
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 3081 Marriage Match II (二分图,并查集)
HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...
- HDU 1325,POJ 1308 Is It A Tree
HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...
- POJ 1308 Is It A Tree?和HDU 1272 小希的迷宫
POJ题目网址:http://poj.org/problem?id=1308 HDU题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1272 并查集的运用 ...
- 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 ...
- POJ 1308 Is It A Tree?
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18778 Accepted: 6395 De ...
- POJ 1308 Is It A Tree?--题解报告
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31092 Accepted: 10549 D ...
- POJ 1308 Is It A Tree? 解题报告
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32052 Accepted: 10876 D ...
随机推荐
- clear 属性
clear属性:规定元素的哪一侧不允许有其他的浮动元素 Example: <html> <head> <style type="text/css"&g ...
- js的快速搜索
公司这几天项目很紧张,然后一直有各种乱七八糟的事,突然说要整个搜索的功能,第一时间想到的就是用php的模糊搜索,但是性能耗的很大,并且调取出的数据的速度贼慢,在百度上找到一个通过js来搜索的功能分享给 ...
- BZOJ 4584 luogu P3643: [Apio2016]赛艇
4584: [Apio2016]赛艇 Time Limit: 70 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 在首尔 ...
- win7 英文版 中文乱码
win7 为了使用英文的 pcb 软件,把语言包改为英文版后,碰到一部分中文会变成乱码.通过下面方法可以识别: control panel --> region and language --& ...
- 01 Python初识
基础: 1.后缀名是py ATT: 单个文件执行,后缀无所谓 2.两种执行方式 终端 python+文件路径 解释器内部: 直接执行 3.解释器路径: #/usr/bin/env pyth ...
- java学习--equals
Object类是所有类的基类. Object类有equals方法.而继承Object中的equals方法判断的是两个对象的引用是否相等,相当于"==",也就是说只有比较的两个对象为 ...
- tensorflow读取本地MNIST数据集
tensorflow读取本地MNIST数据集 数据放入文件夹(不要解压gz): >>> import tensorflow as tf >>> from tenso ...
- 二维凸包 Graham扫描算法
题目链接: http://poj.org/problem?id=1113 求下列点的凸包 求得凸包如下: Graham扫描算法: 找出最左下的点,设为一号点,将其它点对一号点连线,按照与x轴的夹角大小 ...
- IO 、NIO、AIO
Java 中的流 https://ifeve.com/java-nio-vs-io/
- VueJs相关学习网址
麦子学院 http://www.maiziedu.com/course/916/ 慕课网-vue.js入门基础 https://www.imooc.com/learn/694 查阅的网址 ...