Legal or Not

        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
            Total Submission(s): 9125    Accepted Submission(s): 4231

Problem Description
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.

 
Input
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, M <= 100). 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.
 
Output
For each test case, print in one line the judgement of the messy relationship.
If it is legal, output "YES", otherwise "NO".
 
Sample Input
3 2
0 1
1 2
2 2
0 1
1 0
0 0
 
Sample Output
YES
NO
 
Author
QiuQiu@NJFU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2647 3333 3339 3341 3336 
 
题目大意:请帮我们判断他们的关系是否合法。
思路:拓扑排序判环、、、
代码:
#include<queue>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 1010
using namespace std;
queue<int>q;
int n,m,x,y,in[N],tot,sum,head[N];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
struct Edge
{
    int from,to,next;
}edge[N];
int add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
int main()
{
    )
    {
        n=read(),m=read();
        &&m==) break;
        sum=,tot=;
        memset(,sizeof(in));
        memset(edge,,sizeof(edge));
        memset(head,,sizeof(head));
        ;i<=m;i++)
        {
            x=read(),y=read();
            add(x+,y+),]++;
        }
        ;i<=n;i++)
         ) q.push(i);
        while(!q.empty())
        {
            x=q.front(),q.pop();sum++;
            for(int i=head[x];i;i=edge[i].next)
            {
                int t=edge[i].to;
                in[t]--;
                ) q.push(t);
            }
        }
        if(sum!=n) printf("NO\n");
        else printf("YES\n");
    }
    ;
}
 

HDU——3342 Legal or Not的更多相关文章

  1. HDU.3342 Legal or Not (拓扑排序 TopSort)

    HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...

  2. HDU 3342 Legal or Not(判断是否存在环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...

  3. hdu 3342 Legal or Not

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Description ACM-DIY is a large QQ g ...

  4. HDU 3342 Legal or Not(拓扑排序判断成环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

  5. hdu 3342 Legal or Not(拓扑排序)

    Legal or Not Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  6. HDU 3342 Legal or Not(有向图判环 拓扑排序)

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. HDU 3342 Legal or Not (最短路 拓扑排序?)

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. HDU 3342 -- Legal or Not【裸拓扑排序 &amp;&amp;水题 &amp;&amp; 邻接表实现】

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. HDU 3342 Legal or Not(判断环)

    Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so h ...

随机推荐

  1. AJPFX总结FileWriter类的write方法

    FileWriter从类OutputStreamWriter继承的 1.public void write(int c)          throws IOException写入单个字符.     ...

  2. Thinkphp删除缓存

    控制器代码   public function delcache(){ //当找到有Runtime的文件夹时,进入if if(is_dir(RUNTIME_PATH)){ delDir(RUNTIME ...

  3. [转]在ubuntu上安装chrome浏览器

    原文链接: https://www.linuxidc.com/Linux/2013-10/91857.htm --------------------------------------------- ...

  4. git创建分支及日常使用

    克隆代码 git clone https://github.com/master-dev.git 查看所有分支 git branch --all # 默认只有master分支,所以会看到如下两个分支 ...

  5. C# 移动开发 MasterDetailPage 侧滑

    先上结果图: 虽然是跨平台的安卓和ios都可以运行,由于目前只配置了安卓的,ios的先不理. 我们先新建一个项目,跨平台应用: 可移植类库: 可移植项目右键添加新建项 选 Forms MasterDe ...

  6. vba,excel,身份证,照片

    Sub 插入图片() '调整单元格大小,以适应图片大小 功能 插入身份证照片打印 - 正面在单元格d6       反面单元格d10 ActiveSheet.Pictures.Delete '清理过期 ...

  7. 如何选安卓android|linux系统开发板,简化学习难度,缩短开发进程

    平台一:iTOP-4412精英版 系统支持:Android 4.0.3系统  / Android 4.4系统 / Linux + Qt系统 / Ubuntu12.04系统 开发板特点:Cortex-A ...

  8. hibernate fetch属性

    fetch的属性值有:select(默认值).join.subselect 1)当fetch=”select”时,程序会先查询返回要查询的主体对象,然后根据lazy属性看是否懒加载. 2)当fetch ...

  9. 浅谈JS之text/javascript和application/javascript

    问题描述: JS在IE8以下浏览器运行异常 代码: <script>标签是这样子写的: <script type="application/javascript" ...

  10. ios远程推送和python版push server相关笔记

    今天研究了下ios的远程推送,网上的相关教程很多,做了一遍下来记录一下遇到的问题和注意事项(转载请注明) 1.证书及乱七八糟的配置 公钥:app id管理那儿的“Development Push SS ...