POJ-1308 Is It A Tree?(并查集判断是否是树)
http://poj.org/problem?id=1308
Description
There is exactly one node, called the root, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.

In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.
Input
Output
Sample Input
- -
Sample Output
Case is a tree.
Case is a tree.
Case is not a tree.
树应该都很熟悉,简单并查集即可解决此题,做该题主要注意下面几点:
1、判断有没有“环”,即出现“多对一”
2、判断是否有唯一的根结点,即不是多棵树
3、空树也是树
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const int maxn=1e5+;
using namespace std; int fa[];
vector<int> vt; void init(int n)
{
for(int i=;i<=n;i++)
fa[i]=i;
}
int Find(int x)
{
return x==fa[x]? x:fa[x]=Find(fa[x]);
} int main()
{
#ifdef DEBUG
freopen("sample.txt","r",stdin);
#endif
// ios_base::sync_with_stdio(false);
// cin.tie(NULL); int a,b;
int flag=;//判断是否有环,1表示无环
init();
int T=;//样例个数
while(~scanf("%d %d",&a,&b)&&!(a==-&&b==-))
{
if(a==&&b==)//一个样例结束,判断输出并初始化
{
int num=;
for(int i=;i<vt.size();i++)
if(vt[i]==fa[vt[i]]) num++;
if(flag&&num<=) printf("Case %d is a tree.\n",++T);//num=0为空树
else printf("Case %d is not a tree.\n",++T);
flag=;
vt.clear();
init();
continue;
}
int aa=Find(a);
int bb=Find(b);
if(aa==bb) flag=;//有环
else if(flag)//无环再操作,已经判断有环就不用再进行了
{
fa[aa]=bb;
vt.push_back(a);
vt.push_back(b);
}
} return ;
}
-
POJ-1308 Is It A Tree?(并查集判断是否是树)的更多相关文章
- E - Is It A Tree? 并查集判断是否为树
题目链接:https://vjudge.net/contest/271361#problem/E 具体思路:运用并查集,每一次连接上一个点,更新他的父亲节点,如果父亲节点相同,则构不成树,因为入读是2 ...
- hdu 1325 && poj 1308 Is It A Tree?(并查集)
Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...
- POJ 1308 Is It A Tree? (并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24237 Accepted: 8311 De ...
- POJ 1308 Is It A Tree?
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18778 Accepted: 6395 De ...
- POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)
下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...
- POJ - 3657 Haybale Guessing(二分+并查集)
题意:有N个大小各不相同的点,给定Q个询问,格式为q1,q2,A,表示区间q1~q2的最小值是A,问第一个与之前询问结果出现冲突的询问. 分析: 1.二分询问的标号mid,查询1~mid是否出现询问冲 ...
- hdu--1878--欧拉回路(并查集判断连通,欧拉回路模板题)
题目链接 /* 模板题-------判断欧拉回路 欧拉路径,无向图 1判断是否为连通图, 2判断奇点的个数为0 */ #include <iostream> #include <c ...
- 【bzoj4399】魔法少女LJJ 并查集+权值线段树合并
题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...
- HDU - 1272 小希的迷宫 并查集判断无向环及连通问题 树的性质
小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...
随机推荐
- 027、MySQL字符串替换函数,文本替换函数,字符串填充函数
#文本填充 ,'); #ABC12121212121212121 #文本替换 SELECT REPLACE('田攀520','攀','ABC'); #田ABC520 不忘初心,如果您认为这篇文章有价值 ...
- excel表格数据导入导出
/** * 导出数据到excel表格 * Created by shenjianhua on 2018-12-28 */ package com.luer.comm.excel; import jav ...
- flask邮箱注册问题
app/models.py self.confirmed = True db.session.add(self) db.session.commit() 这里的数据修改完后必须commit提交上去,不 ...
- M3U8地址在谷歌浏览器中播放
该案例git码云地址:https://gitee.com/kawhileonardfans/hls-player-example 1.下载插件 插件地址:https://files.cnblogs.c ...
- PHP页面跳转以及伪登录实例
PHP页面跳转一.header()函数header()函数是PHP中进行页面跳转的一种十分简单的方法.header()函数的主要功能是将HTTP协议标头(header)输出到浏览器. header() ...
- error LNK2019: 无法解析的外部符号……
在VS中开发程序的时候遇到一个问题,应该算是比较常见,所以记录下. 在编译程序的时候遇到一个错误,大致提示如下: "error LNK2019: 无法解析的外部符号--" 遇到这个 ...
- 虚拟化安全服务器のIP修改配置
1)进入网卡配置文件目录(以centos7.6为例) cd /etc/sysconfig/network-scripts 2)编辑配置文件(根据自己机器实际网卡进行编辑), 如Vi ifcfg ...
- 剑指offer_2.3_Day_6
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 public class Solution { public int Fibo ...
- 第二阶段scrum-8
1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 消息收发制作中
- python+opencv+dlib瘦脸效果
对实现人脸瘦脸简单功能的一个记录,大概流程如下: 1.使用dlib检测出人脸关键点 2.使用Interactive Image Warping 局部平移算法实现瘦脸 参考:https://blog.c ...