HDU 1272 小希的迷宮 + 1325 Is It A Tree? , 並查集

來源:互聯網
上載者:User

題目連結:

小希的迷宮:http://acm.hdu.edu.cn/showproblem.php?pid=1272

Is It A Tree:http://acm.hdu.edu.cn/showproblem.php?pid=1325

題目類型: 並查集

題目:

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. 

InputThe 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.  


OutputFor 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.



題目大意:

HDU的小希的迷宮是Is It A Tree?這一題的漢化版。題目翻譯可看小希的迷宮。

大概意思是給定一些結點的關係, 然後要判斷這些結點是否組成一棵樹。 


分析與總結:

判斷是否是一棵樹的幾個要點:

(1)判斷是否有迴路,有迴路的就不是樹。這個可用並查集來做。一旦給定的兩個結點a,b是屬於同一個集合的,那麼便可判定有迴路。


(2)看是否只有一個連通分支, 如果有多個,那麼就成了森林。 這個可以用並查集來判斷。最終並查集只有一個根結點,


(3)判斷頂點數是否等於邊數加1,如果不等,則說明不是樹。


(4)判斷節點的入度是否<=1,如果大於1,則說明不是樹;


(5)判斷兩個節點的父節點是否相等,如果相等,則不能構成樹;


並查集的學習: 並查集--學習詳解 
  http://blog.csdn.net/shuangde800/article/details/7329843

Is It A Tree代碼:

#include<iostream>#include<cstdio>#include<cstring>#define N 120005using namespace std;int father[N], rank[N],in[N],out[N];bool vis[N], isCircle;void initSet(){    for(int i=1; i<N; ++i){        father[i]=i, rank[i]=0;        in[i] = 0, out[i] = 0;    }}int find(int x){    int i, j = x;    while(j!=father[j]) j=father[j];    while(x!=j){        i = father[x];        father[x] = j;        x = i;    }    return j;}void Union(int x, int y){    int a = find(x);    int b = find(y);    if(a == b){        isCircle = true;        return;    }    if(rank[a] > rank[b])        father[b] = a;    else{        if(rank[a] == rank[b])            rank[b]++;        father[a] = b;    } }int main(){#ifdef LOCAL    freopen("input.txt","r",stdin);#endif    int a,b,cas=1;    bool flag=true;        while(~scanf("%d %d",&a,&b) && a>=0 && b>=0){        if(!a && !b){            printf("Case %d is a tree.\n",cas++);            continue;        }        int edge = 1;        memset(vis, 0, sizeof(vis));        flag = true;        isCircle = false;        initSet();        int Max = a>b?a:b;        int Min = a<b?a:b;        if(a==b) flag=false;        vis[a]=vis[b]=true;        ++in[b];        ++out[a];        Union(a, b);        while(~scanf("%d %d",&a,&b) && a && b){            vis[a] = vis[b] = true;            if(a==b) flag=false;            if(Min > a) Min = a;            if(Min > b) Min = b;            if(Max < a) Max = a;            if(Max < b) Max = b;            ++in[b];            if(in[b]>1) flag=false;            ++edge;            Union(a,b);        }        int numConnect=0, node=0;        for(int i=Min; i<=Max; ++i) if(vis[i] ){            ++node;            if(father[i]==i)                ++numConnect;        }                if(node!=edge+1) flag = false;        if(flag && !isCircle && numConnect==1) printf("Case %d is a tree.\n", cas++);        else printf("Case %d is not a tree.\n",cas++);    }    return 0;}


——      生命的意義,在於賦予它意義。 

                   原創  http://blog.csdn.net/shuangde800  , By
  D_Double






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.