Is It A Tree?

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 0
8 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 0
3 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.
別看這道題的英文復(fù)雜,實(shí)際上題意真的簡單,題意如下:
就樣例來說,每兩個(gè)數(shù)字代表一條邊的兩端,也就是說這兩個(gè)數(shù)字代表一條邊,0 0代表一組數(shù)據(jù)的結(jié)束,-1 -1代表程序結(jié)束。
這一道題的目的就是為了判斷一下這些數(shù)字到底能不能組成一棵樹。
我們要知道,要組成一顆樹呢,首先,要沒有環(huán)(你見過一顆樹有環(huán)的嗎,反正我是沒有),其次,這棵樹只能有一個(gè)根節(jié)點(diǎn)(有兩個(gè)及以上的根節(jié)點(diǎn),這就不是樹了,是森林了),最后,一個(gè)點(diǎn)的入度最多為1(如果入度大于1了,那這些數(shù)構(gòu)成的不是有森林就是有環(huán)了),根節(jié)點(diǎn)入度為0。
還有,我們要注意一個(gè)坑點(diǎn),空樹也是一棵樹,也就是說直接給你一個(gè)0 0,這就是一個(gè)樹。
你們可能注意到我的寫法里面用了一下宏定義,其實(shí)適當(dāng)?shù)倪\(yùn)用宏定義不僅使得代碼的意思更好理解,還可以優(yōu)化我們的代碼。
這道題呢我是用并查集做的,不用并查集其實(shí)也能做。
代碼如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#define cls(x) memset(x,0,sizeof(x))//代表清空一個(gè)數(shù)組
using namespace std;
const int MAXN=100010;
const int inf=1<<29;
bool flag=false;
int Min,Max;//這里的Min和Max代表得是點(diǎn)的大小范圍
int par[MAXN],in[MAXN];
bool exi[MAXN];//判斷一個(gè)點(diǎn)是否存在
int find(int x)
{
    if(par[x]==x)return x;
    return par[x]=find(par[x]);
}
void init()
{
    for(int i=1;i<=MAXN;i++)
     par[i]=i;
}
void unite(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x==y){
        flag=true;//判斷是否有環(huán)
        return ;
    }
    par[y]=x;
}
bool solve()
{
    int cnt=0;
    for(int i=Min;i<=Max;i++)
    if(exi[i])
    {
        if(in[i]==0)cnt++;//判斷根結(jié)點(diǎn)
        if(in[i]>1)return false;//判斷是否有入度大于1的點(diǎn)
    }
    if(cnt!=1)return false;
    return true;
}
int main()
{
    int a,b;
    Min=inf,Max=-1;
    int p=1;
    init();
    while(~scanf("%d%d",&a,&b))
    {
        if(a==-1&&b==-1)return 0;
        if(a==0 && b==0)
        {
         if(Min==inf&&Max==-1){
            printf("Case %d is a tree.\n",p++);
            continue;
         }
         bool ans=solve();
         if(ans && !flag)printf("Case %d is a tree.\n",p++);
         else printf("Case %d is not a tree.\n",p++);
         cls(in);
         cls(par);
         cls(exi);
         Min=inf;
         Max=-1;
         init();
         flag=false;
         continue;
        }
        in[b]++;
        if(Max<a)Max=a;//確定點(diǎn)范圍
        if(Max<b)Max=b;
        if(Min>a)Min=a;
        if(Min>b)Min=b;
        exi[a]=true;
        exi[b]=true; 
        unite(a,b);
    }
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 今天在BM南京營學(xué)習(xí)到了職業(yè)生涯規(guī)劃的一些理論,包括人職模型以及能力三核,其中人職模型比較復(fù)雜,而能力三核的理論稍...
    我就是哈哈哈閱讀 2,904評論 2 1
  • 誰愛這不息的變幻, 她的行徑? 催一陣急雨, 抹一天云霞, 月亮,星光,日影, 在在都是她的花樣, 更不容峰巒與江...
    丹青墨筆閱讀 313評論 0 1
  • 國慶節(jié),在這個(gè)舉國同慶的日子,多少年前的這一天,一位老人宣布中華人民共和國成立了!是多么的富有劃時(shí)代的意義!從此,...
    想飛的樹不如草閱讀 641評論 0 3
  • 我們的飲食可分為食品和飲品兩大類。飲品在人類的飲食結(jié)構(gòu)中發(fā)揮著食品不能替代的作用。從人們最先接觸的飲品——水開始,...
    久九商會(huì)閱讀 501評論 0 0
  • 謊言是美是丑,其實(shí)取決于我們看待問題的方式。同樣的謊言,也許是美的同時(shí),也是丑陋的。 有些所謂的善意謊言,也許最終...
    子耳子耳閱讀 1,196評論 0 0

友情鏈接更多精彩內(nèi)容