HDOJ 1325 Is It A Tree?(并查集)
2015-07-29 16:55
411 查看
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17568 Accepted Submission(s): 3944
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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).
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
大意:
和小希的迷宫不同的是,这个是有方向的,所以就不能再压缩路径了
ac代码:
<pre name="code" class="cpp">/* *********************************************** Author : AnICoo1 Created Time : 2016-08-01-19.12 Monday File Name : D:\MyCode\2016-8月\2016-8-1.cpp LANGUAGE : C++ Copyright 2016 clh All Rights Reserved ************************************************ */ #include<stdio.h> #include<math.h> #include<string.h> #include<stack> #include<set> #include<map> #include<queue> #include<vector> #include<iostream> #include<algorithm> #define MAXN 101000 #define LL long long #define ll __int64 #define INF 0xfffffff #define mem(x) memset(x,0,sizeof(x)) #define PI acos(-1) #define eps 1e-8 using namespace std; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;} double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;} //head int fa[MAXN],num[MAXN],flag,vis[MAXN]; int findroot(int x) { int r=x; while(r!=fa[r]) r=fa[r]; return r; } void Union(int a,int b) { int nx=findroot(a); int ny=findroot(b); vis[a]=1;vis[b]=1; if(nx==ny||ny!=b)//如果成环或者已被加入树中,说明成环 { flag=1;return; } fa[b]=a; } int main() { int n,m,a,b,cas=0; while(cin>>n>>m) { if(n<0&&m<0) break; if(n==0&&m==0) { printf("Case %d is a tree.\n",++cas); continue; } for(int i=1;i<=MAXN;i++) fa[i]=i,vis[i]=0; flag=0; Union(n,m); while(cin>>a>>b,a||b) Union(a,b); if(flag) { printf("Case %d is not a tree.\n",++cas); continue; } int ans=0; for(int i=1;i<=MAXN;i++) if(fa[i]==i&&vis[i]) ans++; if(ans==1) printf("Case %d is a tree.\n",++cas); else printf("Case %d is not a tree.\n",++cas); } return 0; }
相关文章推荐
- ORACLE 存储过程 循环表操作
- 【SDOI 2013】【BZOJ 3130】费用流
- PHP $_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 的区别
- 创建servlet类时出现的“private static final long serialVersionUID = 1L;”语句是什么意思啊?
- WPF 4 DataGrid 控件(进阶篇二)
- x01.TextProc: 两三分钟完成的一个小工具
- 几种计算机语言的不同 (知乎)
- 微软官方宣布 零售版的Windows 10 U盘不会进入中国市场
- Ajax xmlHttpRequest的status的值的含义
- perl守护进程
- javascript 打开页面window.location.href和window.open的区别
- EF增删改查的优化
- Gson转JSON字符串时候, 将时间转成Long型
- 开博纪念
- HDU考试排名2093
- Android中关于Adapter的使用(中)SimpleAdapter
- UVA - 12563 Jin Ge Jin Qu hao
- A free software for easily creating setups
- CodeIgniter模型
- 表设计上的思考