思路:

对于每一条新的边的两个端点,是否是属于一颗树,要是的话,就不是一颗树。否则,就合并。

这里要注意的是,不能是森林,我这里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)的更多相关文章

  1. 并查集判树 poj 1308

    例题: poj 1308 题目大意比较简单,对任意两个点,有且仅有一条道路,也就是一棵树. 题解:一棵树中,肯定是不能有环的,而且只能由一个根节点.(没认真读题,只知道在那里判环....),所以这个题 ...

  2. POJ 1308&&HDU 1272 并查集判断图

      HDU 1272 I - 小希的迷宫 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  3. (并查集)POJ 1308 & HDU 1325

    一开始以为两道题是一样的,POJ的过了直接用相同代码把HDU的交了,结果就悲剧了.最后发现HDU的没有考虑入度不能大于一. 题意:用树的定义来 判断吧,无环,n个结点最多有n-1条边,不然就会有环.只 ...

  4. POJ 1308/并查集

    题目链接 /* 判断一棵树: * 1.There is exactly one node, called the root, to which no directed edges point. * 2 ...

  5. Is It A Tree? POJ - 1308(并查集判树)

    Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) ...

  6. poj 并查集

    http://poj.org/problem?id=1611 水题 题意:就是找一共有多少个人感染了,0是感染学生的编号. #include <stdio.h> #include < ...

  7. [并查集] POJ 1703 Find them, Catch them

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: ...

  8. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  9. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

随机推荐

  1. Office 下载地址

    Office Professional Plus 2013 64位简体中文版文件名: cn_office_professional_plus_2013_x64_dvd_1134006.iso语言: C ...

  2. my22_mydumper 使用总结

    1. mydumper 的安装依赖于mysql软件,要使用mydumper 则服务器上必须先安装mysql 2. mydumper 安装时会使用mysql软件的动态链接库文件,如果服务器上mysql版 ...

  3. my03_使用空数据库搭建Mysql主从复制

    无数据的主从复制,就搭建一套主从结构的空库,这个是最简单的,先说说这种主从的搭建思路,有利于理解Mysql主从复制1. 安装两套mysql单实例数据库,一个作为主库.一个作为从库:注意要设置两个数据库 ...

  4. nginx 资源重定向

    背景:有时候我一些资源(.js .css etc.)等不想访问我本地的,我想重定向到其他 URL 解决:直接修改 nginx 的配置文件 conf,添加下面的代码 #avoid processing ...

  5. Microsoft使用技巧

    1.拍摄屏幕内容的截图 按 Win + Shift + S 以打开截图栏,然后将光标拖动到要捕获的区域. 截图区域将保存到剪贴板. 2.使用键盘添加表情符号 随心随处表达自我. 按 Ctrl + Sh ...

  6. android Activity启动过程(四)startActivityUncheckedLocked

    final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord, IVoiceInteract ...

  7. select获取到option的value和text方法

    function getSelectval(id){ var selId = document.getElementById(id); //获取select的id var seleIndex =sel ...

  8. Murano py27和py34的兼容处理

    tox.ini envlist = py27,py34,pep8 1. django.utils.encoding.force_unicode替换成django.utils.encoding.forc ...

  9. POJ 2289——Jamie's Contact Groups——————【多重匹配、二分枚举匹配次数】

    Jamie's Contact Groups Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  10. P2626 斐波那契数列(升级版)

    题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数). 题目描述 ...