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


Problem Description
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.

 
Input
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.
 
Output
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).
 
Sample Input
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
 
Sample Output
Case 1 is a tree. Case 2 is a tree. Case 3 is not a tree.
 
Source
 
题目大意:就是问你这个是否是一棵树。  树:只有一个根,除跟外每个节点都只有一个入度,也可以为空树
个人思路:自己本来也写了另外一个代码,然而超时了,过程中碰到了output limit error ,发现是数组开小了,还有memory limit error ,可能是杭电判题太严,好像非要用EOF.....。正确做法是并查集来做,将有边的都归为一个集合,当两者已经属于一个集合了,然后又有一条边时,就构成环了,,,再根据边数等于点数-1,得出答案
看代码
#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+7;
const int maxn=1e5+1;
const int maxk=100+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
int node[maxn];
bool vis[maxn];//标记该点是否被访问过
int edge,flag,po;//存边数,点数
void init()
{
    memset(vis,false,sizeof(vis));
    for(int i=0;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=1;
        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=1;
    while(1)
    {
       scanf("%d%d",&n,&m);
        if(n==0&&m==0)
        {
            printf("Case %d is a tree.\n",ca++);
            continue;
        }
        if(n<0&&m<0) break;//注意题目说小于0就退出,我一直以为是两者都等于-1才退出,卡了两个多小时
        init();
        edge=flag=po=0;
        Union(n,m);
      //  while(cin>>n>>m)
      while(scanf("%d%d",&n,&m)!=EOF)
        {
            if(n==0&&m==0) break;
            if(n==m||node[m]!=m) flag=1;//两者相等的话也不行,并且只能有一个入度
            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-1)//树的点数等于边数加一
            printf("Case %d is a tree.\n",ca++);
        else
            printf("Case %d is not a tree.\n",ca++);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/caijiaming/p/9392045.html