hdu1856&&poj1308(基础并查集)

来源:互联网 发布:淘宝助理导入csv失败 编辑:程序博客网 时间:2024/05/18 10:58
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26363    Accepted Submission(s): 6028


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 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 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.
并查集学习:http://blog.csdn.net/tribleave/article/details/72878239
题意:大慨就是给你一些有向边,让你判断他们能不能构成一棵树。这里树的定义不能有环,空树也是树,每个节点的入度不能大于1,还有就是只有一个根节点,不然就是森林了。
思路:直接并查集合并判断即可,具体见代码注释。
代码:
#include<bits/stdc++.h>using namespace std;const int maxn=1e5+5;int father[maxn],vis[maxn];int in[maxn];bool ans=true;/*int findset(int x){    if(x!=father[x]) father[x]=findset(father[x]);    return father[x];}*/
//递归版本路径压缩有时候可能会爆栈,造成RE。
int findset(int x){    int k, j, r;    r = x;    while(r != father[r])     //查找跟节点        r = father[r];      //找到跟节点,用r记录下    k = x;    while(k != r)             //非递归路径压缩操作    {        j = father[k];         //用j暂存parent[k]的父节点        father[k] = r;        //parent[x]指向跟节点        k = j;                    //k移到父节点    }    return r;         //返回根节点的值}void Union(int a,int b){    int fa=findset(a);    int fb=findset(b);    if(fa==fb) ans=false;//判断是否形成环    else father[fa]=fb;}void init(){    ans=true;    for(int i=0;i<maxn;i++)    {        in[i]=0;        vis[i]=0;        father[i]=i;//并查集初始化    }}int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        if(n<0&&m<0) break;        init();        static int t=1;        if(n==0&&m==0)        {            printf("Case %d is a tree.\n",t++);            continue;        }        vis[n]=vis[m]=1;        in[m]++;        Union(n,m);        while(scanf("%d%d",&n,&m)&&(n+m))        {            //if(n==0&&m==0) break;            vis[n]=vis[m]=1;            in[m]++;            Union(n,m);        }        int cnt=0;        for(int i=1;i<maxn;i++)        {            if(in[i]>1) ans=false;//某个点入度大于1也是不满足的            if(father[i]==i&&vis[i]) cnt++;//cnt用于记录根节点数目,大于1就是森林了            if(cnt>1||!ans) break;        }        if(!ans)        {            printf("Case %d is not a tree.\n",t++);            continue;        }        if(cnt>1) printf("Case %d is not a tree.\n",t++);        else printf("Case %d is a tree.\n",t++);    }    return 0;}

原创粉丝点击