poj 1308 Is It A Tree?

SAP 深入理解库存 当 某个期间发生货物移动的时候,系统在更新mard数据的之前(这个表是实时更新的),会检查此笔业务过账期间和mard里对应记录的期间是否一致,也就是 看这是不是本期间第一笔移动。其二,我们不能保证这定期的作业的确定性,硬件维护、系统性能都可能使这预定的“时刻”出现偏差,如果某一次没能抓取该时刻的库存,对整个报表来说,是数据的缺失;中,当某一期间(Period)的第一项操作更新了物料数量或价值,则SAP系统会向表MBEWH写入一条记录,以第一项操作之前的余额、价格控制为标准。这对于业务数据量大的。 阅读详情

Is It A Tree?
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 22790 Accepted: 7814

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.

题意:判断由这些组成的树是不是一棵树;

解题:并查集的应用;

注意:

1.空树也是树;

2.不能有环;

3.不能是森林;

参考代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int father[30000+1],rank[30000+1];
bool flag[30000+1];
void init(int n){
	for (int i=0;i<=n;i++){
		father[i]=i;
		rank[i]=1;
		flag[i]=false;
	}
}
int find(int x){
	if (x!=father[x])
		father[x] = find(father[x]);	//路径压缩
	return father[x];
}
void Union(int x,int y){
	int i=find(x);
	int j=find(y);
	if (i==j)
		return;
	if (rank[i]<rank[j])
		father[i]=j;
	else{
		father[j]=i;
		if (rank[i]==rank[j])	//两棵树同高
			rank[i]++;
	}
}
bool same(int i,int j){
	if (find(i)==find(j))
		return true;
	return false;
}
int main(){
	int a,b,t=1;
	while (cin>>a>>b){
		if (a==b&&a==-1)
			break;
		if (a==0&&b==0){
			cout<<"Case "<<t++<<" is a tree."<<endl;
			continue;
		}
		flag[a]=flag[b]=true;
		init(1000);
		bool tree=true;
		if (a==b)
			tree=false;
		else
			Union(a,b);
		int first=a;
		while (cin>>a>>b){
			if (a==0&&b==0)
				break;
			if (find(a)==find(b))
				tree=false;
			flag[a]=flag[b]=true;
			Union(a,b);
		}
		for (int i=1;i<=1000;i++){
			if (flag[i]==true&&find(i)!=find(first)){
				tree=false;
				break;
			}
		}
		if (tree==false)
			cout<<"Case "<<t++<<" is not a tree."<<endl;
		else
			cout<<"Case "<<t++<<" is a tree."<<endl;
	}
	return 0;
}




从打印攻击到视频重放:手把手用OULU-NPU训练你的第一个活体检测模型 本文详细介绍了如何使用OULU-NPU数据集训练活体检测模型,以应对打印攻击和视频重放等安全威胁。通过PyTorch框架,从环境准备、数据加载、预处理到模型架构设计,手把手教你构建一个高效的双流网络,结合CNN和LSTM技术,提升模型在多种攻击场景下的检测准确率。 阅读详情

相关推荐

使用Termux在Android设备上编译运行SpecCPU2006

SPEC2006搭配Termux,竟然还能这么玩?!

菜菜的菜园子 2959

poj1308

Is It A Tree? Time Limit:1000MS Memory Limit:10000K Total Submissions:10943 Accepted:3762 Description A tree is a well-known data structure that is either e...

weixin_34109408的博客 302

桑基图实战指南:Excel/Python/R三工具高效绘制方法

桑基图是一种以线条宽度编码流量大小、以节点位置表达流程阶段、以连接关系呈现数据流向的专项可视化技术,其核心原理在于‘宽即量、位即序、连即逻辑’。它并非装饰性图表,而是面向业务诊断的分析前置工具,尤其适用于预算分配、能源损耗、用户转化等存在明确单向流动与显著数值差异的场景。相比传统柱状图或漏斗图,桑基图能天然暴露系统瓶颈与隐性损耗,大幅提升决策效率。本文聚焦工程落地,覆盖Excel插件快速交付、Python Plotly交互开发、R networkD3生态集成三大主流路径,兼顾数据准备铁律、索引映射规范与中文

cuml0912的博客 487

机试练习05:poj1308——Is It A Tree?

一、题解方法 这道题还是应用的并查集的方法。 考虑不是一棵树的情况: 形成环,无根,即每个节点的入度为0。 有多个根,即形成森林。 节点有多个父节点,即节点的入度>1。 用并查集求解: 判断当前有向边的两端点是否在同一个集合内,若在,则一定存在环,则不存在这样的一棵树;若不在,进行其他判断。 对于多个父节点的判断,可以统计每个节点的入度,若入度>1...

anbing9968的博客 209

STM32F103C8T6驱动DM542步进电机:从CubeMX配置到按键控制的全流程避坑指南

本文详细介绍了如何使用STM32F103C8T6通过CubeMX配置驱动DM542步进电机的全流程,包括PWM频率计算、GPIO模式选择及按键控制实现。重点解决了常见硬件连接错误、PWM参数配置及加减速算法优化,帮助开发者快速搭建稳定的电机控制系统。

weixin_42741930的博客 307

【项目管理】第8章 项目整合管理 --知识点整理

制定项目章程: 第一步,旨在明确项目的目标、范围、关键里程碑等关键信息,为项目的后续工作奠定基础。制订项目管理计划: 在项目章程的基础上,进一步详细规划项目的执行过程,包括时间、成本、质量、风险等方面的管理计划。指导与管理项目执行: 按照项目管理计划执行项目,同时监控项目的进展,确保项目按照计划进行。监控项目工作: 对项目的执行情况进行持续监控,及时发现并解决问题,确保项目目标的实现。

weixin_42081167的博客 1593

判断是不是一棵树(并查集)

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 directe

qq_56866469的博客 608

Labview及NI-USRP驱动安装教程

软件下载 安装使用的LabVIEW2018及NI-USRP驱动程序已上传,下载地址: 天翼云盘链接 (访问码:oc33) 百度网盘链接 (提取码:88mi) (注:天翼云盘下载不限速) LabVIEW2018安装步骤 右键软件压缩包选择解压至LabVIEW2018 选中文件,鼠标右击选择以管理员身份打开2018LV-WinChn.exe文件。 点击确定。 点击Unzip解压。 点击确定。 点击下一步。 填写任意信息后,点击下一步。 点击下一步。 点击浏览更改安装路径建议安装到除C盘以

dadapongi6的博客 2万+

图论——并查集(三):判断是否为树

Sample 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,

“知识最大的敌人不是无知,而是错觉” 1037

避坑指南:Vue2中使用TinyMCE5常见问题解决方案(含PowerPaste插件异常处理)

本文针对Vue2项目中集成TinyMCE 5富文本编辑器时遇到的高频疑难问题,提供了一套深度实战解决方案。内容涵盖环境搭建、资源路径配置、图片与视频上传处理、以及替代PowerPaste插件的自定义粘贴清理方案,旨在帮助开发者有效规避常见陷阱,构建稳定可靠的内容编辑功能。

weixin_29012677的博客 154

手把手教你用Matlab为PMSM电流环设计2P2Z补偿器(附与PI对比)

本文详细介绍了如何使用Matlab的controlSystemDesigner工具为永磁同步电机(PMSM)电流环设计2P2Z补偿器,并与传统PI控制器进行性能对比。通过频域分析和时域仿真,展示了2P2Z补偿器在提升带宽、减少超调等方面的优势,为高性能伺服驱动和精密运动控制提供了实用解决方案。

weixin_42551227的博客 376

NOI 310:Is It A Tree?

这题就是判断给出的数据是不是一棵树 每棵树输入 0 0 分界 当输入-1 -1时结束。我是直接判断 但这题并查集好像会好点 然后漏掉了空树这个点 考虑不充分。

qq_67473550的博客 238

【乐企】有关乐企能力测试接口对接-基础版(详细)

乐企能力测试接口对接详细代码以及步骤

九七的博客 7658

poj 1308 Is it a tree? 题解

题意简述 给定一些边,边中包括的点编号不一定连续。请判断是否组成一颗树。 数据 输入: 十分奇怪。多组数据,每组数据包含一些点对(a,b)(a,b)(a,b),具体范围不明,反正开1e61e61e6能过。如果a==0a==0a==0且b==0b==0b==0表示一组数据结束,如果a==−1a==-1a==−1且b==−1b==-1b==−1表示所有数据结束。 输出 对于第iii组数据,输出"Cas...

LightningUZ的博客 342

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

POJ1308 Is It A Tree?

POJ1308 Is It A Tree? 1. 并查集,有点忘了,看维基百科回顾起来了,三个操作,路径压缩、合并、查找父节点。代码很简单,具体参考http://zh.wikipedia.org/wiki/%E5%B9%B6%E6%9F%A5%E9%9B%86 ...

dayong9372的博客 236

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

POJ 1308 Is It A Tree?

题意:给出一些节点,判断这些节点构成的是不是一棵树 思路:

搏风雨的专栏 707

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

POJ 1308 Is It A Tree?再谈 并查集3

是一个判断是否为树的题目 下面是树的定义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. //每一个节点有且仅有一个父节点//这就是判断不要fv!

latstars的博客 408

POJ 1308 Is It A Tree?

A - Is It A Tree?POJ - 1308 题意:输入一组有向边,判断是否能形成一棵树 考察点:连通图和树的定义 森林:多个树的集合 本题要点: (1)判环,若已存在两点在同一个集合中,此时连接两点会形成环 (2)判联通 很坑!!! 如果有多个集合说明不联通 特判:空树也是树(即没有...

simao 173

poj1308 Is It A Tree?

http://poj.org/problem?id=1308 题意:geiy

小超子的专栏 431

POJ——1308 HDU——Is It A Tree?(并查集模板题)

原题链接:http://poj.org/problem?id=1308 测试样例: 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. 题意:给你一些点,判断是不

pursuit的博客 548

POJ 1308-Is It A Tree 并查集

题目来源: http://acm.pku.edu.cn/JudgeOnline/problem?id=1308解题报告:首先判断这些点是否属于同一个集合: 如果两个点之间有边相连,则它们属于同一个集合接着,判断是否存在一个点入度为0,即根再判断,除了根以外的点,是否入度都为1,如果满足上述条件,则为树.#include using namespace std; int cnt=0; typedef struct _node { int key; int rank; int de

kindlucy的专栏 940

POJ 1308 Is It A Tree?(并查集)

POJ 1308 Is It A Tree?(并查集) vj链接 Solution 和 HDU 1272 一毛一样。。 代码 #include <algorithm> #include <cmath> #include <iostream> #include <map> #include <queue> #include <set> #include <stdio.h> #include <string.h> #

weixin_53177273的博客 323

POJ1308-Is It A Tree?

http://poj.org/problem?id=1308 #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #define maxn 1006 using namespace std; struct Edge { int ...

weixin_30457065的博客 118

POJ 1308 Is it a tree(并查集)

POJ1308 并查集的学习 #include <iostream>#include <memory.h>using namespace std;int father[5000];bool has_point[5000];int case_number;void Ini_Set(int y){ father[y]=y; has_point[y]=tr...

weixin_30708329的博客 92

poj1308 (并查集)Is It A Tree?

题目链接:poj1308 题目的要求是让判断给出的点能否连接成一颗树,一棵树的话就只有一个树根,同时一个子节点不能同时拥有两个父亲,解法是用并查集,用father数组来记录每个节点的父节点,v数组来记录是否输入了节点。 #include #include #include #include #include #include using namespace std; int f

no pains,no gains 1052

poj 1308 Is It A Tree? 简单并查集

闲的没事,做一道POJ,已经AC胡哥题册142个题了,好期待200题的大奖啊。扯远了。。 这个题太简单了,并查集的简单应用,输入有挺多坑的。 1 : 空树是一棵树。 2: :自己不能指向自己。 3 : 不能有重复边。 4 : 两个树合并的时候必须至少有一个点是祖先。 5 : 即使输入结束前已经判断完毕,也得输入结束。 //===============================

241
上一篇: poj 2524 Ubiquitous Religions
下一篇: poj 1611 The Suspects
JinbaoSite0144
博客等级 码龄12年 231粉丝 157原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值