A. is it a tree

  题目连接  http://acm.hust.edu.cn/vjudge/contest/121192#problem/A

  题目要求判断是不是树,根据树的定义(连通的无回路的不含环的无向图)可以得出以下结论:

  1.不能有入度大于 一的点

  2.l=n-1;(边数=点数-1)

  3.空图也是树。

 (当有代码重复多次出现时,可以考虑自定义一个函数。)

#include<stdio.h>
#include<string.h>
#define Maxn 105
int D[Maxn],deg[Maxn];
int flage,a,b,tt=1,cnt,n;
void inital()
{
    memset(D,-1,sizeof(D));
    memset(deg,0,sizeof(deg));
    flage=1;
    n=0;
    //tt=1;
    cnt=0;
}
int main()
{
    inital();
    while(1)
    {
        scanf("%d %d",&a,&b);
        if(a<0||b<0) break;
        else if(a==0&&b==0)
        {
            if(cnt==0) {printf("Case %d is a tree.
",tt);tt++;}
            else if(flage&&n==cnt-1) {printf("Case %d is a tree.
",tt);tt++;}
            else {printf("Case %d is not a tree.
",tt);tt++;}
            inital();
        }
        else
        {
            if(flage)
            {
            if(D[a]==-1) {D[a]=a;cnt++;}
            if(D[b]==-1) {D[b]=b;cnt++;}
            n++;
            deg[b]++;
            if(deg[b]>1) flage=0;


            }
        }

    }
}
原文地址:https://www.cnblogs.com/Twsc/p/5698818.html