hdu 5154 Harry and Magical Computer(BestCoder Round #25)

Harry and Magical Computer

                                                      Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 472    Accepted Submission(s): 222

Problem Description

In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal with a process, it will work until the ending of the processes. One day the computer got n processes to deal with. We number the processes from
1 to n. However there are some dependencies between some processes. When there exists a dependencies (a, b), it means process b must be finished before process a. By knowing all the m dependencies, Harry wants to know if the computer can finish all the n processes.

Input

There are several test cases, you should process to the end of file.

For each test case, there are two numbers n m on the first line, indicates the number processes and the number of dependencies. 1≤n≤100,1≤m≤10000

The next following m lines, each line contains two numbers a b, indicates a dependencies (a, b). 1≤a,b≤n

Output

Output one line for each test case.

If the computer can finish all the process print "YES" (Without quotes).

Else print "NO" (Without quotes).

Sample Input

3 2
3 1
2 1
3 3
3 2
2 1
1 3

Sample Output

YES
NO

Source

BestCoder Round #25

       判断拓扑排序是否能够完成,拓扑排序不能完成的条件是没有环,只要判断图里是否有环是否就可以了,用flody和深搜应该都可以吧,我用的flody做的。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int map[150][150];
int n,m;
void flody()
{
    for(int k=1;k<=n;k++)
       for(int i=1;i<=n;i++)
           for(int j=1;j<=n;j++)
               if(map[i][k]&&map[k][j])
                   map[i][j]=1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(map,0,sizeof(map));
        int sign=0;
        int a,b;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            if(a==b)
            {
                sign=1;
                continue;
            }
            map[a][b]=1;
        }
        if(sign)
        printf("NO\n");
        else
        {
            flody();
            for(int i=1;i<=n;i++)
            if(map[i][i])
            sign=1;
            if(sign)
            printf("NO\n");
            else
            printf("YES\n");
        }

    }
    return 0;
}
时间: 2024-12-09 22:41:13

hdu 5154 Harry and Magical Computer(BestCoder Round #25)的相关文章

hdu 5154 Harry and Magical Computer 拓扑排序

Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal wi

HDU 5154 Harry and Magical Computer bfs

Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 499    Accepted Submission(s): 233 Problem Description In reward of being yearly outstanding magic student, Harry gets

(简单) HDU 5154 Harry and Magical Computer,图论。

Description In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal with a process, it will work until the ending of the processes. One day the computer got n processes to deal with. We num

HDU 5154 Harry and Magical Computer 有向图判环

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5154 题解: 有向图判环. 1.用dfs,正在访问的节点标记为-1,已经访问过的节点标记为1,没有访问过的节点标记为0,如果访问到-1的节点说明说有环. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 t

hdu 5154 Harry and Magical Computer

http://acm.hdu.edu.cn/showproblem.php?pid=5154 思路:有向图判断有没有环,可以用floyd.. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int inf=1<<20; 6 7 int g[200][200]; 8 int main() 9 { 10 int n,m;

拓扑序列 之 hdu 5154 Harry and Magical Computer

/* AOV网(Activity On Vertex Network): 用图来表示工程:其中,用顶点表示活动:弧表示活动之间的制约关系. 工程是否顺利进行?---->AOV网是否存在有向回路 ******************************************* 用产生(包含所有) 顶点序列的方法,顶点序列满足: 在图中,若顶点vi到顶点vj存在路径, 则在序列中,顶点vi领先于顶点vj. 满足上述条件的顶点序列称为拓扑序列, 产生这一序列的过程称为拓扑排序. ********

HDOJ 5154 Harry and Magical Computer floyd判环

floyd判环 Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1005    Accepted Submission(s): 404 Problem Description In reward of being yearly outstanding magic student, H

hdu 5171 GTY&#39;s birthday gift (BestCoder Round #29)

GTY's birthday gift                                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 209    Accepted Submission(s): 71 Problem Description F

HDU 4932 Miaomiao&#39;s Geometry(BestCoder Round #4)

Problem Description: There are N point on X-axis . Miaomiao would like to cover them ALL by using segments with same length. There are 2 limits: 1.A point is convered if there is a segments T , the point is the left end or the right end of T.2.The le