思路:

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

这里要注意的是,不能是森林,我这里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. SqlServer自动备份作业

    /*********完整备份作业*********/ –完整备份,每周一次 USE Master GO declare @str varchar(100) set @str='D:\DBtext\jg ...

  2. python3 + pycharm+requests+HTMLTestRunner接口自动化测试步骤

    1.python3 环境的搭建,pycharm安装 2.想要用requests做自动化接口测试,那么就得先安装requests这个第三方库,在命令窗口执行 pip install requests 3 ...

  3. springBoot学习资料

    转载:http://www.importnew.com/29363.html 官网搭建springBoot项目:https://www.cnblogs.com/lcplcpjava/p/7406253 ...

  4. spring aop execution用法

    代码结构: 1. "execution(* com.ebc..*.*(..))" 与 "execution(*  com.ebc..*(..))" 2019-0 ...

  5. 虚拟机 ---- 最小化安装无法使用tab补全键

    解决方法: 安装 yum -y install bash-completion 然后重启 注意:挂载时使用绝对路径的cdrom挂载,  ls -l /dev/cdromvim /etc/fstab — ...

  6. HDU 5592——ZYB's Premutation——————【线段树单点更新、单点查询】

    ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. C# 本地文件的上传和下载

    本文主要介绍一下,在APS.NET中文件的简单上传于下载,上传是将文件上传到服务器的指定目录下,下载是从存入数据库中的路径,从服务器上下载. 1.上传文件 (1)页面代码 <table alig ...

  8. .NET Core 部署到CentOS–2.创建守护进程, 通过Nginx公网访问

    继上一篇, 我们确定在内网可以通过 "http://localhost:5000",可以访问到站点后,接下来我们要配置"守护进程","Nginx公网8 ...

  9. js获取日期:前天、昨天、今天、明天、后天

    前天,昨天,今天,明天,后天 <html> <head> <meta http-equiv="Content-Type" content=" ...

  10. Asp.Net中ObjectDataSource控件传参绑定数据

    最近在实习,在上头交付的任务中,由于需要使用Asp.Net的ListView控件,因此必然得就使用了ObjectDataSource控件,由于在使用过程中,需要网页中的参数发送到后台后,运行该参数进行 ...