题目链接

/*
判断一棵树:
* 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. hdu_5908_Abelian Period(暴力)

    题目链接:hdu_5908_Abelian Period 题意: 给你n个数字,让你找出所有的k,使得把这n个数字分为k分,并且每份的数字种类和个数必须相同 题解: 枚举k,首先k必须是n的约数,然后 ...

  2. apache 配置

    apache 2.4.9 httpd-vhosts.conf部分 <VirtualHost *:81>             //配置端口 DocumentRoot "E:/H ...

  3. use include to read a file

    #include<iostream> #include<fstream> using namespace std; void process(string filename) ...

  4. Struts国际化

    第一步需要建立配置文件 格式为      文件名_zh_CN.properties    为中文配置文件   文件名_en_US.properties为美式英语配置文件 配置文件里面的值以键值对的形式 ...

  5. ZUFE 1035 字符宽度编码(字符串)

    Time Limit: 1 Sec  Memory Limit: 128 MB Description 你的任务是编写一个程序实现简单的字符宽度编码方法.规则如下:将任何2~9个相同字符的序列编码成2 ...

  6. hdu 5874 Friends and Enemies icpc大连站网络赛 1007 数学

    #include<stdio.h> #include<iostream> #include<algorithm> #include<math.h> #i ...

  7. gcc-config: Active gcc profile is invalid解决办法

    错误描述 Gentoo软件安装错误,提示: gcc-config: Active gcc profile is invalid 解决方法: 列出可用的profile gcc-config -l gcc ...

  8. chapter11_1 Lua数组、列表

    Lua中的table可以表示其他语言提供的数据结构:数组.记录.线性表.队列.集合等. 在Lua中很少编写搜索算法,因为table本身就提供了直接访问任意类型的功能. 数组 使用整数来索引table即 ...

  9. ( ̄y▽ ̄)~ 智能手机II

    ( ̄y▽ ̄)~ 智能手机II TimeLimit: 3000/1000 MS (Java/Others)  MenoryLimit: 32768/32768 K (Java/Others) 64-bi ...

  10. UVA 11021 /概率

    题意: 有k只鸟,每只鸟只能活一天,它可以在死之前生[0,n-1]只鸟,生出x只鸟的概率是p[x].问m天后所有的鸟都时光的概率.(m天之前就死了的也算上). 输入:T.n.k.m. 题解: 每只鸟的 ...