【每日一题】8月27日题目精讲 Is It A Tree?

链接:https://ac.nowcoder.com/acm/problem/105905
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 10000K,其他语言20000K
64bit IO Format: %lld
题目描述
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.
输入描述:
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.
输出描述:
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).
示例1
输入
复制
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
输出
复制
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree

题解:

题意:判断给的是不是树
我们想想树的性质,每个点都有一个父亲节点,除了根节点
前者我们可以用并查集维护,后者可以用入度出度来判断,因为根节点只有出度,入度为0,而其他点的入度为1
为了避免是森林,我们还应该要求所有点必须联通
对了,空树也为树

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <math.h>
#include <string>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#define maxn  10010
const int MaxN = 0x3f3f3f3f;
const int MinN = 0xc0c0c00c;typedef long long ll;
ll mod = 998244353;
using namespace std;
int f[maxn];
int in[maxn];
bool visited[maxn];
int ifind(int x){if(x==f[x]) return x;return f[x]=ifind(f[x]);
}
void imerge(int x,int y){int dx=ifind(x);int dy=ifind(y);if(dx!=dy) f[dx]=dy;return ;
}int main()
{int n,m;int z=1;while(cin>>n>>m&&n!=-1){for(int i=0;i<maxn;i++) f[i]=i;memset(visited,false,sizeof(visited));memset(in,0,sizeof(in));bool flag=true;if(n==0&&m==0){printf("Case %d is a tree.\n",z++);continue;}while(n||m){visited[n]=true;visited[m]=true;in[m]++;imerge(n,m);scanf("%d%d",&n,&m);}int ans=0;for(int i=1;i<maxn;i++){if(visited[i]&&in[i]==0){ans++;}if(in[i]>=2) flag=false;}if(ans!=1) flag=false;if(flag==false) printf("Case %d is not a tree.\n",z++);else printf("Case %d is a tree.\n",z++);}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/319510.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

YbtOJ#631-次短路径【左偏树,最短路】

正题 题目链接:https://www.ybtoj.com.cn/contest/114/problem/1 题目大意 给出nnn个点mmm条边的一张无向图&#xff0c;对于每个点iii求不经过i∼1i\sim 1i∼1的最短路的第一条边的情况下iii到111的最短路 数据保证这条边唯一 n∈[1,105],m∈[1,2105],c∈[1,103]n\in[1,10^…

好代码是管出来的——使用Jenkins搭建CI服务器

Jenkins是一个开源的跨平台的CI工具&#xff0c;它可以部署在Windows、Linux等平台上&#xff0c;并且Jenkins提供了非常丰富的插件来帮助完成编译、测试、部署等工作。  本文将介绍在Windows平台上使用Jenkins完成.Net Core应用的持续集成环境搭建&#xff0c;其主要内容有&…

牛客练习赛 59

A.小乔和小灰灰 前几天刚刚学了序列自动机&#xff0c;这题直接也没咋想暴力的做法&#xff0c;直接上序列自动机匹配子序列即可。 #define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0) #pragma GCC optimize(2) #include<iostream> #include<algorithm&…

【图论】清理牛棚/Cleaning Shifts S(luogu 4644)

正题 luogu 4644 题目大意 给你一段时间&#xff0c;有n头奶牛&#xff0c;第i头可以花费sis_isi​的代价清理lil_ili​到rir_iri​&#xff0c;问你清理完所有时间段的最小代价 解题思路 所有点需求都为1的线性规划&#xff0c;直接代入最短路就可以了 code #include<q…

1153 Decode Registration Card of PAT (25分)

1153 Decode Registration Card of PAT (25分) 题意&#xff1a; 给出一组学生的准考证号和成绩 准考证号组成为&#xff1a;考试等级&#xff08;TAB&#xff09;&#xff0c;考场号&#xff08;从101到999&#xff09;&#xff0c;考试日期&#xff08;yymmdd&#xff09;&…

YbtOJ#893-带权的图【高斯消元,结论】

正题 题目链接:https://www.ybtoj.com.cn/problem/893 题目大意 给出一张nnn个点mmm条边的无向联通图&#xff0c;每条边正反向各有A,B,CA,B,CA,B,C三种边权。 保证满足 Ax,y−Ay,x,Bx,yBy,x,Cx,y−Cy,xA_{x,y}-A_{y,x}\ ,\ B_{x,y}B_{y,x}\ ,\ C_{x,y}-C_{y,x}Ax,y​−Ay,x​…

.Net Core 2.1 通用主机(Core 在控制台应用程序中的应用)

一、介绍官方文档中说&#xff0c;Microsoft.AspNetCore.App 元包&#xff08;ASP.NET Core 2.1 或更高版本&#xff09;包含通用主机的Microsoft.Extensions.Hosting包&#xff0c;当创建控制台项目以后并没有相应的包。在官方案例中所用的Nuget包有&#xff1a;1. Microsoft.…

【二分】【最小生成树】公路修建问题(P2323)

正题 P2323 题目大意 给你n个点和若干边&#xff0c;每条边有两种代价&#xff0c;问你选择n-1条边使得这n个点连通&#xff0c;且选择第一种代价的边不小于k&#xff0c;让你使代价最大值最小 解题思路 二分答案&#xff0c;然后最小生成树&#xff0c;连接可以连得边&…

PAT 1152 Google Recruitment (20 分)- 甲级

题意 给出一个L长的字符串&#xff0c;求出其中第一个k位长的素数 题解&#xff1a; 其实就是暴力求解&#xff0c;不过在这介绍一个函数 int numstoi(s); s是string型的&#xff0c;stoi可以将string转换成int型 string f_str std::to_string(f); 是将int型转化成string型…

Grakn Forces 2020——D题Pecco模型总结

今天是双节但是为什么我今天好自闭啊啊啊&#xff0c;自己在寝室自闭了一下午&#xff0c;好无聊。明天一定要精神满满啊&#xff01; A - Circle Coloring 签到题 #define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0) #pragma GCC optimize(2) #include<iost…

产品开发管理之流程和体系(总篇)

前言秋风瑟瑟&#xff0c;夏日的灼热犹在&#xff0c;就瞬间迎来刺骨寒风。凛冬将至&#xff0c;今天对我们来说&#xff0c;像贴面的利刃一样冰冷而真实。农民、建筑工人、司机、程序员、私企高管、私企老板、资本巨富&#xff0c;都被裹挟进了这个焦灼的时代&#xff0c;没有…

YbtOJ#593-木棍问题【费用流】

正题 题目链接:https://www.ybtoj.com.cn/contest/114/problem/3 题目大意 n∗mn*mn∗m的网格上有一些格子有木球&#xff0c;两个相邻木球直接可以有木棍。 两个LLL形的木棍会产生AAA的代价&#xff0c;两个III形的木棍会产生BBB的代价 对于每个kkk求出插入kkk根木棍时的最小…

【LCT】旅游(P1505)

正题 P1505 题目大意 给你一棵树让你进行以下操作 1.修改一条边的权值 2.把一条链的权值取反 3.查询一条链的权值和 4.查询一条链的边权最大值 5.查询一条链的边权最小值 解题思路 把边拆成点&#xff0c;然后就是lct模板了 code #include<cstdio> #include<…

1143 Lowest Common Ancestor 甲级

题意&#xff1a; 给出一棵二叉搜索树的前序遍历&#xff0c;问结点u和v的共同最低祖先是谁&#xff0c;利用先序遍历特点。 二叉搜索树满足&#xff1a; 节点的左子树只包含键小于节点键的节点。 节点的键只包含节点的右键大于或等于子树的节点的键。 左子树和右子树也必须是…

牛客练习赛 70

A - 重新排列 二分答案&#xff0c;然后循环一边即可。 #define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0) #pragma GCC optimize(2) #include<iostream> #include<algorithm> using namespace std; const int N100010; int cnt[30]; char s[N]; ch…

《.NET Core 2.0 应用程序高级调试》7折优惠

活动截止到2018年10月15日晚24:00&#xff0c;之后恢复8.6折价格。都说.NET Core 在Linux 上跑得快&#xff0c;可是如果.NET Core 程序在Linux 操作系统上出现了内存泄漏、死锁或者崩溃情况怎么办&#xff1f;怎样在Linux 和macOS操作系统上有效地使用工具对.NET Core 应用程序…

CF803G-Periodic RMQ Problem【离散化,线段树,ST表】

正题 题目链接:https://www.luogu.com.cn/problem/CF803G 题目大意 一个长度为nnn的序列aaa复制kkk份连接&#xff0c;要求支持 区间赋值区间查询最小值 n,q∈[1,105],k∈[1,104]n,q\in[1,10^5],k\in[1,10^4]n,q∈[1,105],k∈[1,104] 解题思路 先把所有查询的连续区间边界离…

【bfs】Replication G(P7151)

正题 P7151 题目大意 给你一个n*n的方阵&#xff0c;有若干机器人的起始点 机器人在每个时间会向一个方向走一步&#xff0c;经过D个时间后就会在相邻四个格子复制一个副本&#xff0c;副本会和原机器人走相同方向&#xff0c;同时也会复制 当任意一个机器人的操作碰到障碍…

1151 LCA in a Binary Tree 甲级

题意&#xff1a; 给定前序遍历和中序遍历&#xff0c;问u和v的lca &#xff08;先是中序&#xff0c;后是中序&#xff09; 题解&#xff1a; 方法一&#xff1a; 参考题解 将树映射到一颗BST上&#xff0c;在BST上找到答案然后再映射回原本的树 方法二&#xff1a; 参考题…

牛客练习赛 58——树链剖分

A - 牛能和宝石 签到题 #define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0) #pragma GCC optimize(2) #include<iostream> #include<algorithm> using namespace std; const int N100010; int a[N],b[N]; int n; int main() {IO;int T1;//cin>>…