poj1308 Is It A Tree

题目链接:http://poj.org/problem?id=1308
题意:给你一个有向图,问你这是不是一棵树
解析:输入有点坑,他可能没有一条边,其他的你只需要判断一下边数是不是等于点数-1,每个结点的前驱结点是不是只有一个

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e3+100;
int fa[maxn],vis[maxn];
int main(void)
{
    int x,y,case_t = 1;
    while(~scanf("%d %d",&x,&y))
    {
        if(x==-1 || y==-1)
            break;
        int flag = 1;
        memset(vis,0,sizeof(vis));
        memset(fa,0,sizeof(fa));
        int c1 = 0,c2 = 0;
        if(x!=0)
        {
            do
            {
                if(!fa[y])
                    fa[y] = x;
                else
                    flag = 0;
                if(!vis[x])
                {
                    vis[x] = 1;
                    c1++;
                }
                if(!vis[y])
                {
                    vis[y] = 1;
                    c1++;
                }
                c2++;
            }while(~scanf("%d %d",&x,&y)&&x&&y);
            if(c1-1 != c2)
                flag = 0;
        }
        printf("Case %d ",case_t++);
        if(flag)
            puts("is a tree.");
        else
            puts("is not a tree.");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值