4005: Legal or Not

ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not. Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.

输入格式

The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N<=100,2<= M <= 200). Then M lines follow, each contains a pair of (x, y) which means x is y's master and y is x's prentice. The input is terminated by N = 0.TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.

输出格式

For each test case, print in one line the judgement of the messy relationship.If it is legal, output "YES", otherwise "NO".

大致翻译:

题目描述

ACM-DIY是一个大型QQ群,众多优秀的ACM聚集在一起。它是如此和谐,就像一个大家庭。每天,许多“圣牛”如HH,HH,AC,ZT,LCC,BF,Qinz等在线聊天交流。当有人有疑问时,许多像迷失这样的热心奶牛会来帮忙。然后被帮助的人会称迷失为“主人”,迷失将有一个不错的“徒弟”。渐渐地,有好几对“师徒”。但随之而来的问题是:师傅太多,徒弟太多,我们怎么知道合法与否?我们都知道一个师傅可以有很多师徒,一个师徒也可能有很多师傅,这是合法的。然而,有些奶牛并不那么诚实,他们持有非法关系。瞬间拿HH和3xian来说,HH是3xian的师傅,同时3xian是HH的师傅,这是相当违法的!为避免这种情况,请帮助我们判断他们的关系是否合法。请注意,“师徒”关系是可传递的。这意味着如果A是B的师傅,B是C的师傅,那么A就是C的师傅。

输入格式

输入由多个测试用例组成。对于每种情况,第一行包含两个整数,N(要测试的成员)和 M(要测试的关系)(2 <= N<=100,2<= M <= 200)。然后是M行,每条线都包含一对(x,y),这意味着x是y的师父,y是x的学徒。输入由 N = 0终止 简单起见,我们给每个人一个数字 (0, 1, 2,..., N-1)。我们使用他们的号码而不是他们的名字。

输出格式

对于每个测试用例,在一行中打印对混乱关系的判断。如果合法,则输出“是”,否则输出“否”。

输入样例 复制

4 3
0 1
1 2
2 3
3 3
0 1
1 2
2 0
0 1

输出样例 复制

YES
NO
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 210;//注意数据范围

int n,m;
int h[N],e[N],ne[N],idx;
int q[N];
int d[N];

void add(int a,int b)
{
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

bool ToPu()
{
    int hh=0,tt=-1;
    for(int i=0;i<n;i++)
        if(!d[i]) q[++tt]=i;

    while(hh<=tt)
    {
        int t=q[hh ++];

        for(int i=h[t];i!=-1;i=ne[i])
        {
            int j=e[i];
            d[j]--;
            if(!d[j]) q[++tt]=j;
        }
    }

    return tt==n-1;
}

int main()
{
    while(cin >> n >> m)
    {
        if(!n) break;
        memset(h,-1,sizeof h);
        memset(d,0,sizeof d);
        memset(q,0,sizeof q);
        while(m--)
        {
            int a,b;
            cin >> a >> b;
            add(a,b);
            d[b]++;
        }
        if(ToPu()) puts("YES");
        else puts("NO");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值