Is It A Tree?(hdu1325)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1325
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29221 Accepted Submission(s): 6719
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.
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 2 is a tree.
Case 3 is not a tree.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const int maxk=+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
int node[maxn];
bool vis[maxn];//标记该点是否被访问过
int edge,flag,po;//存边数,点数
void init()
{
memset(vis,false,sizeof(vis));
for(int i=;i<maxn;i++)//题目没有说明范围,开一个足够大的数
node[i]=i;
}
int Find(int a)
{
return a==node[a]?a:node[a]=Find(node[a]);
}
void Union(int a,int b)
{
a=Find(a);
b=Find(b);
if(a==b)
{
flag=;
return ;//这里为何直接退出呢,因为如果a,b已经属于一个集合了,再加一条边就构成环了
}
if(!vis[a])
{
vis[a]=true;//这个点没有访问过的话,点数加一
po++;
}
if(!vis[b])
{
vis[b]=true;
po++;
}
edge++;//边数加一
node[b]=a;
}
int main()
{
ios::sync_with_stdio(false);
int n,m,ca=;
while()
{
scanf("%d%d",&n,&m);
if(n==&&m==)
{
printf("Case %d is a tree.\n",ca++);
continue;
}
if(n<&&m<) break;//注意题目说小于0就退出,我一直以为是两者都等于-1才退出,卡了两个多小时
init();
edge=flag=po=;
Union(n,m);
// while(cin>>n>>m)
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==) break;
if(n==m||node[m]!=m) flag=;//两者相等的话也不行,并且只能有一个入度
Union(n,m);
}
if(flag)
{
printf("Case %d is not a tree.\n",ca++);
continue;
}
// for(int i=1;i<=maxn;i++)
// {
// if(vis[i]) po++;
// }
if(edge==po-)//树的点数等于边数加一
printf("Case %d is a tree.\n",ca++);
else
printf("Case %d is not a tree.\n",ca++);
}
return ;
}
Is It A Tree?(hdu1325)的更多相关文章
- Is It A Tree?[HDU1325][PKU1308]
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 【HDU1325】Is It A Tree?(并查集基础题)
有以下坑点: 1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值. 2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树. (无环路 ...
- hdu1325 Is It A Tree?并检查集合
pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...
- HDU1325 Is It A Tree? 【并查集】
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- hdu1325 Is It A Tree? 基础并查集
#include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...
- POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28838 Accepted: 9843 -& ...
- hdu1325 Is It A Tree?(二叉树的推断)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
随机推荐
- 查看linux上所有用户
1.查看所有用户名 cat /etc/passwd |cut -f 1 -d #是1不是L的小写 2.显示用户信息 whoami 查看当前登录用户名. id username 查看用户的uid,gid ...
- SpringBoot系列(1)
简介:用来简化新Spring应用的初始搭建以及开发过程:该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 特点:1. 创建独立的Spring应用程序2. 嵌入的Tomcat, ...
- 开源一个windows消息队列查看器
windows消息简单易用,在异步消息发送场景的使用还是比较适合.为了方便查看队列中的消息和删除一些异常消息,开发了一个简单的小工具拿出来分享下. 源码地址:https://gitee.com/eab ...
- require用法及源码解析
一.require()的基本用法 require语句内部逻辑: 当 Node 遇到 require(X) 时,按下面的顺序处理. (1)如果 X 是内置模块(比如 require('http')) ...
- ServerSocket01
ServerSocket表示服务端套接字:我们首先来看下其中的一个构造器: public ServerSocket(int port,int backlog) throws IOException 其 ...
- Learning Python 007 基本语句
Python 基本语句 if - 条件判断 没有什么好说的,直接上代码: age = 3 if age >= 18: print('adult') elif age >= 6: print ...
- Sublime text3 创建html模板
最近接手了公司官网跟新的任务,需要编写HTML页面.页面中存在大量重复内容(导航条.页脚.侧边栏等),每次复制粘贴也不是个事,网上搜了相关的HTML模板创建问题,还找到了.楼主使用的是Sublime ...
- jdk 安装及环境变量配置
一.jdk安装及基础配置,转自文章来源:http://www.cnblogs.com/smyhvae/p/3788534.html 1.jdk下载及安装 下载网站:http://www.oracle. ...
- C#使用Json.NET解析Json
本文转载自 http://xiaosheng.me/2016/10/01/article25/ 最近在 C# 项目中需要使用到 Json 格式的数据,我简单上网搜索了一下,基本上有两种操作 Json ...
- CocoaPods常用操作命令
查看镜像: gem sources -l 删除镜像 gem sources --remove https://rubygems.org/ 添加镜像 gem sources -a https://gem ...