O - Is It A Tree?

英语题目翻译——OJ_ 310:Is It A Tree? 题目:310:Is It A Tree?(OpenJudge - 310:Is It A Tree?) 翻译: 树是一种众所周知的数据结构,它要么是空的(null, void, nothing),要么是由满足以下属性的节点之间的有向边连接的一个或多个节点的集合。 只有一个结点,称为根,没有有向边指向它。 除了根,其他每个节点都有一条边指向它。 从根到每个节点都有一个唯一的有向边序列。 例如,考虑下面的插图,其中节点用圆表示,边用带箭头的线表示。前两个是树,但最后一个不是。 在这个问题中,. 阅读详情

Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]

Description

A tree is a well-known data structure that is either empty (, 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.
 
  
 
  
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;

#define maxn 105
bool flag[maxn];
int set[maxn];

void  make_set()
{
    for(int i=1;i<=100;i++)
    {
        flag[i]=false;set[i]=i;
    }
}
int find_set(int x)
{
    while(x!=set[x])
        x=set[x];
    return x;
}
void union_set(int x,int y)
{
    x=find_set(x);
    y=find_set(y);
    if(x==y)return;
    set[y]=x;
}
int main()
{
    int i,x,y,V=1;
    while(scanf("%d%d",&x,&y)!=-1)
    {
        if(x==-1&&y==-1)break;
        if(x==0&&y==0)
        {
            printf("Case %d is a tree.\n",V++);
			continue; 
        }
        make_set();
        int root=x;
        bool tree=true;
        flag[x]=flag[y]=true;
        if(x==y)tree=false;
        else
			 union_set(x,y);
        while(scanf("%d%d",&x,&y))
        {
            if(x==0&&y==0)break;
            flag[x]=flag[y]=true;
            if(find_set(x)==find_set(y))
                tree=false;
            union_set(x,y);
        }
        for(i=1;i<=100;i++)
            if(flag[i]&&find_set(i)!=find_set(root))
                tree=false;
        if(tree)
            printf("Case %d is a tree.\n",V++);
        else 
			printf("Case %d is not a tree.\n",V++);
    }
    return 0;
}


SAP PO开发-端到端配置 1.登录PO系统,选择system landscape,将外围系统的系统信息(Software Component以下简称SC)注册到PO系统中。2.点击products注册外围系统信息。3.选择New 新建 按钮:创建一个新的系统和版本。4.输入产品名称、供应商、版本号等信息。然后点击:Next。5.设置instance 名称,然后点击Next。6.最后设置软件组件的名称,然后点击Finish。 阅读详情

相关推荐

瑞芯微rv1126 rtsp+mpp+rga取流

瑞芯微rv1109 rtsp+mpp+rga取流

ZuoSeDiao的博客 6339

Is It A Tree?(它是一棵树吗?)

https://cn.vjudge.net/problem/POJ-1308 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 sat...

嵩韵儿 2174

为什么类型论是函数式编程的理想基础?

函数式编程(如Haskell、Scala、OCaml)以“函数为核心”“无副作用”“不可变数据”等特性著称,但它的“安全高效”并非凭空而来——背后的关键支撑是类型论。本文将聚焦“类型论如何支撑函数式编程”,从基础概念到具体案例,解释两者的本质联系。本文将按“故事引入→核心概念解释→关系拆解→原理+代码案例→实际应用→总结”的逻辑展开,用“乐高积木”“菜谱”等比喻贯穿全文,确保抽象理论可视化。类型论:程序的“接口规则手册”,定义类型和函数的约束;函数式编程。

AI天才研究院 694

Is It A Tree?

链接 题目描述:判断给出的边集能否构成一棵树。 解题思路: 不仅需要判断所有点是否属于一个集合,还要判断各个点是否符合树的定义。 而判断各点是否符合树的定义可以转换为判断它的入度是否符合要求,根结点的入度为0,而其余点的入度为1。 只要各个结点满足入度要求,只有一个根结点,以及各结点都属于同一集合,就可以构成一棵树。 代码 #include <iostream> #include <cstdio> #include <map> using namesp

weixin_43878652的博客 328

智能座舱音频系统集成与调试:从芯片选型到音质优化实战

在智能座舱域控制器架构中,音频系统已从简单的功放加扬声器演进为SoC、数字音频总线、外部DSP与多通道功放协同的复杂子系统。TDM总线的时隙分配、PDM麦克风阵列的信号完整性,以及AEC回声消除算法如何与硬件协同,直接决定多音区体验和语音交互的可靠性。工程实践中,音频调试的关键在于先理顺供电、接地、时钟与数据路由,再针对功放削波、采样率失配、TDM slot错位等典型问题进行系统性排查。从芯片选型到系统级指标验收,只有将音频链路视为整车电子电气架构的一个协同整体,才能真正实现从“响”到“好听”的跨越。本文结

weixin_30162121的博客 364

ADRV9026初始化避坑指南:手把手调试PostMcsInit常见故障

本文详细解析了ADRV9026多片同步初始化过程中的PostMcsInit常见故障,包括时钟校准失败、JESD链路建立问题和ARM核通信异常等。通过实际项目经验,提供了硬件检查清单、软件配置要点和分层诊断方法,帮助工程师快速定位和解决问题,提升系统稳定性。

hadoop5ranger的博客 620

Is It A Tree?(并查集)

题目:   给出一对对的数字a,b,表示从a到b有一条边。判断这是不是一棵树。 多case,每个case以0 0 结尾输入以-1 -1结尾 分析:①每个节点(除了根结点)只有一个入度;②只有一个根结点。 森林:多个根。 入度:指向同一个结点的边数。 将点逐个加入集合中,然后判断就行了。 #include #include using namespace std; int

ToBeYours的博客 485

AVS3代码阅读HPM4.0

参考AVS3代码阅读(HPM6.0)(一.整体架构以及CU划分部分代码) 版本:HPM4.0 核心文件:app_encoder.c   要做的是帧间预测,所以前面的函数先暂时不管了,挖个坑以后再填。直接从enc_pic看起。 enc_pic()    ...

CRR的博客 3187

Codeforces Round #811 (Div. 3)补题(A-G)

A.Everyone Loves to Sleep B.Remove Prefix C.Remove Prefix D.Color with Occurrences E.Add Modulo 10 F.Build a Tree and That Is It G.Path Prefixes

佯设的博客 663

【电源专题】案例:对比不同电池在不同电流下的电池放电曲线

无

一点硬件 2840

Is It A Tree?——是一棵树吗?(并集查)

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 fo...

一个和代码打交道の小孩 3735

运行openclaw gateway install时出现Gateway install failed: Error: schtasks create failed的解决方案

本文主要介绍了运行openclaw gateway install时出现Gateway install failed: Error: schtasks create failed的解决方案,希望能对学习和使用openclaw的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 11万+

Is It A Tree?(并查集)

Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 Description A tree is a well-known data structure that is either empty (null, void, ...

绝尘 362

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

1135 Is It A Red-Black Tree (30 point(s))

There is a kind of balanced binary search tree namedred-black treein the data structure. It has the following 5 properties: (1) Every node is either red or black. (2) The root is black. (3) Every...

张蔡辉 222

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

【杭电oj】1325 - Is It A Tree?(树,并查集)

Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19420    Accepted Submission(s): 4349 Problem Description A tree is a

wyg1997 759

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

KY34 Is It A Tree?

【代码】KY34 Is It A Tree?

敲代码的小风 118

Is it a tree 并查集

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 i...

博客标题不能为空我也没办法 427

并查集 hdu-1325 Is It A Tree?

Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32554Accepted Submission(s): 7447 Problem Description A tree is a we...

weixin_30699443的博客 104
上一篇: N - 食物链
下一篇: P - Shortest Prefixes
翔蝈蝈
博客等级 码龄16年 25粉丝 69原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值