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 ...
随机推荐
- WebService . Schema约束
1. namespace 相当于schema文件的id 2. targetNamespace属性 用来指定schema文件的namespace的值 3. xmlns属性 引入一个约束, 它的值是一个s ...
- 面向连接的tcp 编程
from socket import * serverSocket=socket(AF_INET,SOCK_STREAM) serverSocket.bind(("",8899)) ...
- 如何使用 Visual C# .NET 处理 Excel 事件
事件处理概述 Visual C# .NET 使用委派处理来自组件对象模型 (COM) 服务器的事件.委派是 Microsoft Visual Studio .NET 中的一个新概念.对于 COM 事件 ...
- python 字符串和字节数互转
在 python 中字符 是 str 类型, 字节是 bytes 类型 b = b'hello' # bytes 字节 s= 'hello' # str 字符串 可通过 type() 检查是什么类型 ...
- 前端--vue框架
1.下载 查看已安装好的版本 -------渐进式的JS框架--------- vue是什么 Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的 渐进式框架.与其他重量级框架 ...
- Digital Twin的8种解读!
国际8大主流厂商对digital twin的理解,很有必要来一次汇总! 据IDC预测,2017年世界上将有40%的大型生产商都会应用虚拟仿真技术来为他们的生产过程进行建模,Digital Twin可以 ...
- vue.js简单添加和删除
这只是个简单的添加和删除,没有连接后台数据的 <%@ page language="java" contentType="text/html; charset=UT ...
- Mybatis控制台打印sql
mybatis-config.xml配置如下: <configuration> <settings> <setting name="lazyLoadingEna ...
- vs2015单步调试问题(附加进程)
如果页面有Codebehind的页属,那么前端通过ajax提交到后端代码,无法在后端代码中取到值. 这是一个vs属性标记,用于跟踪管理项目.如果后端代码的自定义指定(Inherits)的话,应该取掉 ...
- Truthy真值
在 JavaScript 中,Truthy (真值)指的是在 布尔值 上下文中转换后的值为真的值.所有值都是真值,除非它们被定义为 falsy (即除了 false,0,"",nu ...