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) ...
随机推荐
- Android HttpGet和HttpPost设置超时
HttpPost: private Runnable runnable = new Runnable() { @Override public void run() { String url = Ba ...
- ES6学习之字符串的扩展
字符的Unicode表示法 \uxxxx可以表示一个双字节的字符(xxxx表示字符编码,范围在0000---FFFF),超出此范围用两个双字节表示. console.log("\u0061& ...
- USACO-Greedy Gift Givers(贪婪的送礼者)-Section1.2<2>
[英文原题] Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange ...
- mysql-日志种类
MySQL有以下几种日志: 1,错误日志:记录启动.运行或停止时出现的问题,一般也会记录警告信息. 2,一般查询日志:记录建立的客户端连接和执行的语句. 3,慢查询日志:记录所有执行时间超过lo ...
- MySQL在linux下安装
mysql在linux下的安装 安装环境:系统是 centos6.5 1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloa ...
- 1.从GUI到MVC
GUI(graphic user interface 用户图形界面).GUI编程的目的是提供交互性,并根据用户的操作实时的更新界面.用户的操作是不可预知的鼠标和键盘事件,我们如何保持同步和更新?在上层 ...
- 使用 Chrome Timeline 来优化页面性能
使用 Chrome Timeline 来优化页面性能 有时候,我们就是会不由自主地写出一些低效的代码,严重影响页面运行的效率.或者我们接手的项目中,前人写出来的代码千奇百怪,比如为了一个 Canvas ...
- Spring入门第五课
集合属性 在Spring中可以通过一组内置的xml标签(如:<list>,<set>,<map>)来配置集合属性. 配置java.util.List类型的属性,需要 ...
- 关于mysql查询最近一条记录
关于mysql查询最近一条记录 最近项目中遇到需要查询记录当前时间最近的一条记录的问题,开始感觉无从下手,后来逐步发现了三种解决方案. 下策——查询出结果后将时间排序后取第一条 select * fr ...
- VR中运动控制器的传送系统
创建一个VRPawn 新建一个BluePrint,父类选择Pawn,我们命名为VRPawn,打开它. 添加一个Scene命名为CameraRoot 在CameraRoot节点下添加一个Camera 在 ...