并查集,是否成树,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个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
随机推荐
- pageX,clientX,offsetX,screenX,offsetLeft,style.left,offsetWidth,scrollWidth的区别以及使用详解
https://www.cnblogs.com/echolun/p/9231760.html
- JavaSE---内部类
1.概述 1.1 内部类:一个类定义在其他类的内部,这个类被称为内部类: 1.1.1 内部类可以放在外部类的任何位置,方法中也可以(称为局部内部类): 1.1.2 一般将内部类作为 成员内部类 使用 ...
- Dropping Balls UVA - 679(二叉树的遍历)
题目链接:https://vjudge.net/problem/UVA-679 题目大意:t组样例,每组包括D M 层数是D 问第M个小球落在哪个叶子节点? 每个节点有开关 刚开始全都 ...
- 3d Max 2015安装失败怎样卸载3dsmax?错误提示某些产品无法安装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- DEDE利用Ajax实现调用当前登录会员的信息简要说明
其实这个功能在dede默认的模板上就有,只能算是在原有的功能上进行改造而已. 1.首先需要加载一个ajax的js文件进来 <script language="javascript&qu ...
- css 动画 和 响应式布局和兼容性
14.动画 -moz-:火狐, -ms-:IE,-webkit-:谷歌,-o-:欧朋 transform旋转 rotate旋转 scale放大 translate偏移量 skew倾斜度 transfo ...
- winfrom C#树勾选等
AfterCheck /// <summary> /// 树勾选 /// </summary> /// <param name="sender"> ...
- Winform窗体“空闲事件”
Application.Idle += Application_Idle; void Application_Idle(object sender, EventArgs e){ } 当应用程序完成处理 ...
- 连接字符串(web.config)
data source=ip; initial catalog=db1; user id=sa; password=*** <connectionStrings> <add name ...
- jQuery 显示与隐藏 tab选项卡
方法一:使用display样式:block.none来控制文本的显示与隐藏 <div class="explain_text"> 移动互联网为企业提供了连接用户的新方式 ...