B - B(Is It A Tree?)
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
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.
方法一:直接使用树的定义来做。
树的性质
1)树的节点数比边数多1(空树除外)
2) 树中除根节点的每个节点只有唯一的父节点。
条件1可以排除有环的图,条件2可以排除多个根的图。
特别注意的是,空树不满足条件1,需单独判断!
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int pre[100010];
int main()
{
int s=0;
while(1)
{
s++;
int x,y,s1=0,k=0;
memset(pre,0,sizeof(pre));
while(~scanf("%d%d",&x,&y)&&(x+y))
{
if(x==-1&&y==-1) return 0;
pre[x]=1;
pre[y]=1;
k++;
}
for(int i=0;i<100010;i++)
{
if(pre[i])
s1++;
}
if(s1==0||s1==k+1)//树的节点数比边数多1(空树除外)
printf("Case %d is a tree.\n",s);
else
printf("Case %d is not a tree.\n",s);
}
}
方法二:并查集
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 1000; int p[maxn]; //存父亲节点
int r[maxn]; //存入度
bool used[maxn]; //判断下标是否使用
int Max, Min; //判断树中使用的下标的最值 void set() //初始化
{
for(int x = 0; x < maxn; x++)
{
p[x] = x; //自己为自己的父亲节点
r[x] = 0; //各点独立 入度为 0
used[x] = false; //各点均未使用
}
Min = maxn;
Max = -maxn;
} int find(int x) //找根节点
{
return x == p[x] ? x : p[x] = find(p[x]);
} void Union(int x, int y) //合并两点所在的树
{
int fx = find(x);
int fy = find(y);
p[fy] = fx;
}
int main()
{
int a, b;
int C = 0; //记录是第几组数据
while(scanf("%d%d", &a, &b) != EOF)
{
if(a == -1 && b == -1) break;
set(); //初始化 p[b] = a; r[b]++; //处理第一对数据
Min = min(a, b);
Max = max(a, b);
used[a] = true; //标记使用
used[b] = true; int x, y;
bool flag = true;
while(scanf("%d%d", &x, &y) != EOF)
{
if(x == 0 && y == 0)
{
int root = 0, index; //记录根节点个数 和下标
for(int i = Min; i <= Max; i++)
{
if(used[i] && i == p[i]) //判断有几个根
{
root++; index = i;
}
} if(root != 1) flag = false; //一棵树只能有一个根 if(flag)
{
for(int i = Min; i <= Max; i++) //判断入度情况,除了根节点,其它节点入度均为1
{
if(i != index && used[i] && r[i] != 1) flag = false;
}
} if(flag) printf("Case %d is a tree.\n", ++C);
else printf("Case %d is not a tree.\n", ++C);
//printf("%d\n", root); //开始有些小错误 输出中间变量,找错误
break; //注意:易忘记
} if(find(x) != find(y)) Union(x, y); //如果不在同一棵树中,则合并 r[y]++; //入度+1
used[x] = true; //标记使用
used[y] = true;
Min = min(Min, x); Min = min(Min, y); //更新最值
Max = max(Max, x); Max = max(Max, y);
}
}
return 0;
}
B - B(Is It A Tree?)的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Tree树节点选中及取消和指定节点的隐藏
指定节点变色 指定节点隐藏 单击节点 未选中则选中该节点 已选中则取消该节点 前台: 1.HTML <ul id="listDept" name="listDept ...
随机推荐
- 项目实战--解决运行sql文件错误
说明: 新项目启动,通过公司运维同学给的数据库脚本在Navicat中建项目的数据库,运行脚本时报错 Error Code: 1227. Access denied; you need (at leas ...
- Hive表的基本操作
目录 1. 创建表 2. 拷贝表 3. 查看表结构 4. 删除表 5. 修改表 5.1 表重命名 5.2 增.修.删分区 5.3 修改列信息 5.4 增加列 5.5 删除列 5.6 修改表的属性 1. ...
- 有哪些适合个人开发的微信小程序
微信小程序提供了一个简单.高效的应用开发框架和丰富的组件及API,帮助开发者在微信中开发具有原生 APP 体验的服务. 微信小程序支持采用云开发模式,无需后台服务,十分的方便快捷,适合个人开发一些工具 ...
- 基于腾讯云存储COS的ClickHouse数据冷热分层方案
一.ClickHouse简介 ClickHouse是一个用于联机分析(OLAP)的列式数据库管理系统(DBMS),支持PB级数据量的交互式分析,ClickHouse最初是为YandexMetrica ...
- Macbook 安装Windows的完美教程
[原文](http://www.melodydance.top/mac-win.html) 1. 背景 Windows相对于Mac市场占有率更高,对很多人来说Windows使用起来更方便,以至于很多人 ...
- LeetCode 二分查找模板 II
模板 #2: int binarySearch(vector<int>& nums, int target){ if(nums.size() == 0) return -1; in ...
- 解决Tengine健康检查引起的TIME_WAIT堆积问题
简介: 解决Tengine健康检查引起的TIME_WAIT堆积问题 一. 问题背景 "服务上云后,我们的TCP端口基本上都处于TIME_WAIT的状态"."这个问题在线下 ...
- CTFHub - Web(六)
命令注入: 1.进入页面,测试127.0.0.1, 关键代码: <?php $res = FALSE; if (isset($_GET['ip']) && $_GET['ip'] ...
- ctfshow—web—web6
打开靶机 发现登录窗,首先想到SQL注入 抓包,进行SQL注入测试 测试发现空格符被过滤了 使用/**/代替空格符进行绕过,绕过后登录成功 检测回显位 开始查询数据库名 开始查询数据库内数据表名称 查 ...
- AWS IoT Greengrass是什么?V1和V2版本及其差异
AWS IoT Greengrass Greengrass主要是用于边缘计算或者机器学习有关,对于详细了解请阅读结尾处的官方文档,文档内容也较为丰富. 目录 AWS IoT Greengrass ...