题目链接

/*
判断一棵树:
* 1、There is exactly one node, called the root, to which no directed edges point.
* 2、Every node except the root has exactly one edge pointing to it.
* 3、There is a unique sequence of directed edges from the root to each node.
并查集应用
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=10000+5;
int f[maxn];
bool vis[maxn];
bool flag;
int a,b;
int r1,r2;
int ma;
void init()
{
memset(vis, false, sizeof(vis));
for(int i=0;i<maxn;i++)
f[i]=i;
ma=0;
flag=true;
}
int find(int t)//并查集
{
if(f[t]!=t)
return f[t]=find(f[t]);
else
return f[t];
}
int main ()
{
int k=0;
init();
while(~scanf("%d%d",&a,&b))//a->b
{
if(a<0||b<0)
break;
ma=max(ma,max(a,b));
vis[a]=vis[b]=true;
if(a==0&&b==0)
{
if(!flag)
printf("Case %d is not a tree.\n",++k);
else
{
int t=0;
for(int i=1;i<=ma;i++)
{
if(vis[i]&&f[i]==i)
t++;
}
if(t>=2)
flag=false;
if(flag)
printf("Case %d is a tree.\n",++k);
else
printf("Case %d is not a tree.\n",++k);
}
init();
continue;
}
else if(flag)
{
r1=find(a);
r2=find(b);
if(r1==r2)//
flag=false;
else
{
if(f[b]==b)//每个节点只能被指向一次
f[b]=r1;
else
flag=false;
}
}
}
return 0;
}

POJ 1308/并查集的更多相关文章

  1. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  2. poj 1797(并查集)

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

  3. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  4. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  5. POJ 3228 [并查集]

    题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...

  6. poj 1733 并查集+hashmap

    题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...

  7. poj 3310(并查集判环,图的连通性,树上最长直径路径标记)

    题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...

  8. POJ 3657 并查集

    题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...

  9. poj 2236 并查集

    并查集水题 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring& ...

随机推荐

  1. B树(B-树)

    1.什么是B树(B-树)? B树是一种m阶树,m>=2 性质: 1)树中每个结点至多m个孩子: 2)对于根结点,子树个树取值范围为[2,m],关键字个数范围[1,m-1]: 3)对于非根非叶结点 ...

  2. Zeppelin使用phoenix解释器

    Interpreters设置

  3. 读取memo中某行内容

    方法1 可用以下代码读取Memo中指定行的内容: var   aLine:String; begin   aLine:=Memo1.Lines[2]; end; 在使用中,读取的行在Memo中需要保证 ...

  4. PHP文本路径转换为链接文字

    <?php /** * 文本路径转换为有链接的文字 * @param string $str 转换内容 * @return string */ function urlToLink($str) ...

  5. 禁用 VS2010 的 vcpkgsrv.exe 运行

    VS2010经常使用一段时间后巨卡,发现vcpkgsrv.exe这个进程相当占内存,但是结束后又会自己启动,百度之原来是IntelliSense的问题,关闭之即可,设置如下

  6. sublime 3 最新 LICENSE

    http://blog.sina.com.cn/s/blog_68e267e10102v76h.html

  7. MC- 挂单STOP交易

    using System; using System.Drawing; using System.Linq; using PowerLanguage.Function; using ATCenterP ...

  8. iOS开发的一些奇巧淫技(转载)

    iOS开发的一些奇巧淫技 http://www.cocoachina.com/ios/20141229/10783.html iOS开发的一些奇巧淫技2 http://www.cocoachina.c ...

  9. mysql高级查询

    高级查询: 1.连接查询 select * from Info,Nation #得出的结果称为笛卡尔积select * from Info,Nation where Info.Nation = Nat ...

  10. G - 小晴天老师系列——可恶的墨水瓶

    G - 小晴天老师系列——可恶的墨水瓶 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Othe ...