题目链接

/*
判断一棵树:
* 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. Xshell4连接,Linux系统中文显示乱码解决办法

    Xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET NetSarang Xshell 4 Build 0120议.使用 ...

  2. protected-broadcast的作用

    protected-broadcast(暂时从网上搜到这么多,也不知对错,先留着) “保护性广播”,在一些AndroidManifest.xml中的一级标记<protected-broadcas ...

  3. javascript动画效果之多物体透明度

    html和css 仅为布局,需要注意的是filter对应的是老版本的ie浏览器透明度,而opacity对应的其他浏览器的透明度 filter: alpha(opacity: 50); opacity: ...

  4. python 日历

    上章总结了python中time模块的使用,这次总结日历模块 calendar >>> import calendar >>> cal = calendar.mon ...

  5. mybatis 一点整理

    mapper指定对应的接口 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper ...

  6. python多线程,多进程

    线程是公用内存,进程内存相互独立 python多线程只能是一个cpu,java可以将多个线程平均分配到其他cpu上 以核为单位,所以GIL(全局锁,保证线程安全,数据被安全读取)最小只能控制一个核,很 ...

  7. caogao

    https://css-tricks.com/ 在safari浏览器中添加transform规则,如果没有效果,把该规则的原始元素以块状显示 http://webdesignerwall.com/tr ...

  8. 如何使用kaptcha验证码组件

    kaptcha是基于SimpleCaptcha的验证码开源项目. kaptcha是纯配置的,使用起来比较友好.如使用了Servlet,所有配置都在web.xml中.如果你在项目中使用了开源框架(比如S ...

  9. .net core 时间戳转换

    /// <summary> /// 时间戳和日期的转化 /// </summary> public class DateTimeUtility { /// <summar ...

  10. 如何将excel导入到数据库中并在gridview中显示

    在页面上导入个excel文件,将该excel中的数据导入到数据库中,并且在页面的gridview中把数据显示出来. .在Asp.net中怎样将Excel文件中的数据导入到GridView中呢? 首先我 ...