您的位置:首页 > 其它

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;
}




 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: