思路:

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

这里要注意的是,不能是森林,我这里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. python3 rjust()函数笔记

    #rjust(12,'l')"12是字符串的长度,l是当字符串不够长的时候,用l填充.并且字符串右对齐".返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串.如果 ...

  2. oracle service name connect

    oracle service name connect dest_ip=100.100.100.100 dest_port= dest_dbname=server_name connect=" ...

  3. windows 远程到ubuntu桌面

    Windows remote connect ubuntu desktop 1. install xRDP sudo apt-get update sudo apt-get install xrdp ...

  4. java生成复杂word文档

    在Web应用中,有时需要按照固定的模板将数据导出到Word,如流程审批单,在流程处理完成后将处理过程按照流程单的要求导出,有时程序中需要实现生成 标准Word文档,要求能够打印,并且保持页面样式不变, ...

  5. junit使用中的一些问题

    之前开发过程中的测试,不是使用main方法,就是启动项目调用地址,尤其是后者,测试起来非常不方便,今天配置了下junit,中间遇到些问题,记录如下. 首先下载spring-test.jar包和juni ...

  6. CentOS 7安装Perl环境

    平台信息 Description: CentOS Linux release 7.6.1810 (Core) 安装步骤 安装支持 $ yum install perl* #安装perl相关支持 $ y ...

  7. 卸载 maya

    AUTO Uninstaller 更新下载地址 1.选择MAYA 2.选择版本 3.点击[开始卸载]

  8. php 入门

    <?php $username = $_POST["username"]; $passcode = $_POST["passcode"]; # 一定要my ...

  9. POJ 3177——Redundant Paths——————【加边形成边双连通图】

    Redundant Paths Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  10. Android串口操作,简化android-serialport-api的demo(转载)

    原帖地址:点击打开 最近在做android串口的开发,找到一个开源的串口类android-serialport-api.其主页在这里http://code.google.com/p/android-s ...