Is It A Tree? POJ - 1308(并查集判树)
Problem Description
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.
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
The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
Output
For each test case display the line "Case k is a tree." or the line "Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).
Sample Input
6 8 5 3 5 2 6 4
5 6 0 0 8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0 3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1
Sample Output
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
题目大意:
给出多组边,让你判断这是不是一棵树。边的信息以( 始点, 末点)的形式给出以(0,0)结束.,数据以(-1,-1)输入结束;
树的定义:树是指任意两个结点之间有且仅有一条路径的无向图。或者说,只要是没有回路的连通无向图就是树。
树的特性:
⒈必有一个特定的称为根(ROOT)的结点;
2.一棵树如果有n个节点,那么它一定恰好有n-1条边。
3.一棵树中的任意两个节点有且仅有唯一的一条路径连通。
4.在一棵树中加一条边将会构成回路;
思路:应用并查集的知识来判断,将节点以此加入看是否与下面几种情况是否吻合。
不满足条件的情况:
1.要加入的子节点已经有父节点。
2.要加入的父节点的子节点,与父节点有相同的父节点。
3.建树成功,查看树不仅仅只有一颗。
如果1,2不满足了,其实第三步,不需要再进行判断了;
代码:
#include<iostream>
#include<map>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxx=;
int f[maxx];
int getf(int v)
{
if(f[v]==v)
return v;
return f[v]=getf(f[v]);
}
int main()
{
int a,b,Case=;
while(cin>>a>>b)
{
if(a==-&&b==-)
break;
if(a==&&b==)
{
printf("Case %d is a tree.\n",++Case);
continue;
}
for(int i=;i<=maxx;i++)
f[i]=i;
map<int,int>mp;
mp[a]=mp[b]=;
f[b]=a;
int flag=;
if(a==b)
flag=;
while(scanf("%d%d",&a,&b)&&a&&b)
{
mp[a]=mp[b]=;
int t1=getf(a);
int t2=getf(b);
if(t1!=t2&&a!=b)
f[t2]=t1;
else
flag=;
}
if(flag)
printf("Case %d is not a tree.\n",++Case);
else
{
map<int,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++)
{
if(f[it->first]==it->first)
flag++;
}
if(flag==)
printf("Case %d is a tree.\n",++Case);
else
printf("Case %d is not a tree.\n",++Case);
}
}
return ;
}
Is It A Tree? POJ - 1308(并查集判树)的更多相关文章
- 并查集判树 poj 1308
例题: poj 1308 题目大意比较简单,对任意两个点,有且仅有一条道路,也就是一棵树. 题解:一棵树中,肯定是不能有环的,而且只能由一个根节点.(没认真读题,只知道在那里判环....),所以这个题 ...
- POJ 1308/并查集
题目链接 /* 判断一棵树: * 1.There is exactly one node, called the root, to which no directed edges point. * 2 ...
- poj 3310(并查集判环,图的连通性,树上最长直径路径标记)
题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...
- 1021. Deepest Root (25) -并查集判树 -BFS求深度
题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...
- poj 1984 并查集
题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...
- UVA1455 - Kingdom(并查集 + 线段树)
UVA1455 - Kingdom(并查集 + 线段树) 题目链接 题目大意:一个平面内,给你n个整数点,两种类型的操作:road x y 把city x 和city y连接起来,line fnum ...
- HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- 【bzoj5133】[CodePlus2017年12月]白金元首与独舞 并查集+矩阵树定理
题目描述 给定一个 $n\times m$ 的方格图,每个格子有 ↑.↓.←.→,表示从该格子能够走到相邻的哪个格子.有一些格子是空着的,需要填上四者之一,需要满足:最终的方格图中,从任意一个位置出发 ...
- 并查集&线段树&树状数组&排序二叉树
超级无敌巨牛逼并查集(带权并查集)https://vjudge.net/problem/UVALive-4487 带删点的加权并查集 https://vjudge.net/problem/UVA-11 ...
随机推荐
- prometheus简单监控Linux,mysql,nginx
prometheus安装 下载安装 #官网下载 解压即可使用 https://prometheus.io/download/ #docker 方式安装 sudo docker run -n prome ...
- lodop打印设计
<template> <div class="dashboard-container"> <form id="form1"> ...
- 第一章:Python数据分析前的基础铺垫
本节概要 - 数据类型 - 数据结构 - 数据的常用操作方法 数据类型 基础铺垫 定义 我们搞数据时,首先要告诉Python我们的数据类型是什么 数值型:直接写一个数字即可 逻辑型:True,Fals ...
- k8s之yaml详解
k8s之yaml详解 apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 metadata: #资源的元数 ...
- csp-s模拟106
这场其实心态十分爆炸,首先一下午改上次破T3卡常一下午没过,心情十分暴躁.上来开题不顺利,T1想了一个小时没动.于是跳到T2,看T2的80pts貌似可拿就先打了.T3只会判10分,又想打个$2^{2N ...
- legend3---lavarel常用操作代码2
legend3---lavarel常用操作代码2 一.总结 一句话总结: 对于王思cong被执法人的感悟:失意时 莫心伤,得意时 莫膨胀 1.lavarel自动事务? DB::transaction方 ...
- 爬虫之代理和cookie的处理
代理操作 代理的目的 为解决ip被封的情况 什么是代理 代理服务器:fiddler 为什么使用代理可以改变请求的ip 本机的请求会先发送给代理服务器,代理服务器会接受本机发送过来的请求(当前请求对应的 ...
- app微信支付的集成步骤
1.引用地址 //微信支付 compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' 2.注册 private IWXAPI api ...
- drawable,andrid 圆形图片,圆角图片
各种drawable 小的例子, 开源中国地址:https://git.oschina.net/lyxs916/drawable-test.git bitm ...
- 【I·M·U_Ops】------Ⅱ------ IMU自动化运维平台之CMDB
说明本脚本仅作为学习使用,请勿用于任何商业用途.本文为原创,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. #A 我理解的 CMDB CMDB翻译过来,Configuratio ...