并查集,是否成树,Poj(1308)
思路:
对于每一条新的边的两个端点,是否是属于一颗树,要是的话,就不是一颗树。否则,就合并。
这里要注意的是,不能是森林,我这里WA了两次了。只不过在最后,查看每个节点的祖先是否是同一个就可以了。
#include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std; const int maxn = ; int father[maxn];
bool vis[maxn]; int Find_set(int x)
{
if(x!=father[x])
father[x]=Find_set(father[x]);
return father[x];
} void Union(int x,int y)
{
father[y] = x;
} int main()
{
bool flag;
int x,y;
int t=;
while(scanf("%d%d",&x,&y),x!=-)
{
flag=true;
if(x==&&y==)
{
printf("Case %d is a tree.\n",++t);
continue;
} else {
for(int i=;i<maxn;i++)
{
father[i] = i;
vis[i] = false;
} int first = x;
vis[x]=vis[y]=true;
if(Find_set(x)==Find_set(y))
flag=false;
else Union(x,y); while(scanf("%d%d",&x,&y),x!=)
{
vis[x]=vis[y]=true;
int fx=Find_set(x);
int fy=Find_set(y);
if(fx==fy)
flag=false;
else Union(fx,fy);
}
for(int i=;i<maxn;i++)
{
if(vis[i]&&Find_set(first)!=Find_set(i))
{
flag=false;
break;
}
}
if(flag)
printf("Case %d is a tree.\n",++t);
else printf("Case %d is not a tree.\n",++t); } }
return ;
}
并查集,是否成树,Poj(1308)的更多相关文章
- 并查集判树 poj 1308
例题: poj 1308 题目大意比较简单,对任意两个点,有且仅有一条道路,也就是一棵树. 题解:一棵树中,肯定是不能有环的,而且只能由一个根节点.(没认真读题,只知道在那里判环....),所以这个题 ...
- POJ 1308&&HDU 1272 并查集判断图
HDU 1272 I - 小希的迷宫 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- (并查集)POJ 1308 & HDU 1325
一开始以为两道题是一样的,POJ的过了直接用相同代码把HDU的交了,结果就悲剧了.最后发现HDU的没有考虑入度不能大于一. 题意:用树的定义来 判断吧,无环,n个结点最多有n-1条边,不然就会有环.只 ...
- POJ 1308/并查集
题目链接 /* 判断一棵树: * 1.There is exactly one node, called the root, to which no directed edges point. * 2 ...
- Is It A Tree? POJ - 1308(并查集判树)
Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) ...
- poj 并查集
http://poj.org/problem?id=1611 水题 题意:就是找一共有多少个人感染了,0是感染学生的编号. #include <stdio.h> #include < ...
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- [并查集] POJ 2236 Wireless Network
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 25022 Accepted: 103 ...
- poj 1797(并查集)
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
随机推荐
- 5.SpringMVC
1.SpringMVC概述 概述: SpringMVC是基于请求驱动,围绕一个核心Servlet 转发请求到对应的Controller而设计的优点:是一个典型的教科书式的MVC构架,易学易用提供了清晰 ...
- 2 Sum
Problem Given an array of integers, find two numbers such that they add up to aspecific target numbe ...
- speex编译
首先去官网 https://www.speex.org/downloads/ 下载解压 将include.libspeex文件夹复制到自己新建工程的jni目录下 speex有关的类 package c ...
- java 与 数据库的连接
Eclipse中使用SQL server 2017数据库 一.准备材料 要能够使用数据库就要有相应的JDBC,所以我们要去Microsoft官网下载https://www.microsoft.com/ ...
- 牛客网Java刷题知识点之字节流练习之从A处复制文本文件到B处(FileReader、FileWriter )、复制文本文件的原理图解
不多说,直接上干货! CopyTextTest.java package zhouls.bigdata.DataFeatureSelection.test; import java.io.FileRe ...
- 《nginx 三》实现nginx的动态负载均衡——实战
Http动态负载均衡 什么是动态负载均衡 传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件, 因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upst ...
- C# 多线程之线程同步
多线程间应尽量避免同步问题,最好不要线程间共享数据.如果必须要共享数据,就需要使用同步技术,确保一次只有一个线程访问和改变共享状态. 一::lock语句 lock语句事设置锁定和接触锁定的一种简单方法 ...
- jquery拖拽排序,针对后台列表table进行拖拽排序(超实用!)
现在很多后台列表为了方便均使用拖拽排序的功能,对列表进行随意的排序. 话不多说 ,我在网上找了一些demo,经过对比,现在把方便实用的一个demo列出来,基于jqueryUI.js 先上html代码, ...
- iOS 8 提供 TestFlight 方便开发者测试软件 (转)
原文地址:http://tech2ipo.com/66893 TestFlight / via iMore 作者: Nick Arnott 译者:翛凌 原文:iMore iOS 应用程序的测试对 ...
- [精校版]The Swift Programming Language--语言指南--字符串和字符 (转)
今天装了10.10.马上就可以实际编写swift了.还是很兴奋啊. 哈哈.字符串和字符是大家最容易打交道的.今天就转一下讲解swift中字符串和字符的文章.希望对大家有帮助. 原文地址:http:// ...