Harry and Magical Computer

 Accepts: 350
 Submissions: 1348
 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 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 拓扑排序模板题。注意在处理IN[i] ++的时候要先判断是否已经存在这条边。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cctype>
#include <cmath>
#include <queue>
#include <map>
#include <cstdlib>
using namespace std; const int SIZE = ;
int IN[SIZE];
bool G[SIZE][SIZE];
int N,M; bool toposort(void);
int main(void)
{
int from,to; while(scanf("%d%d",&N,&M) != EOF)
{
fill(IN,IN + SIZE,);
fill(&G[][],&G[SIZE - ][SIZE - ],false);
for(int i = ;i < M;i ++)
{
scanf("%d%d",&from,&to);
if(!G[from][to])
IN[to] ++;
G[from][to] = ;
}
if(toposort())
puts("YES");
else
puts("NO");
} return ;
} bool toposort(void)
{
int count = ;
queue<int> que; for(int i = ;i <= N;i ++)
if(!IN[i])
que.push(i); while(!que.empty())
{
int cur = que.front();
que.pop();
count ++; for(int i = ;i <= N;i ++)
if(G[cur][i])
{
IN[i] --;
if(!IN[i])
que.push(i);
}
}
if(count < N)
return false;
return true;
}

BC Harry and Magical Computer (拓扑排序)的更多相关文章

  1. hdu 5154 Harry and Magical Computer 拓扑排序

    Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  2. HDU5154拓扑排序

    Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  3. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  4. CF Fox And Names (拓扑排序)

    Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. POJ1270 Following Orders (拓扑排序)

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4254   Accepted: 1709 ...

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

    Description In reward of being yearly outstanding magic student, Harry gets a magical computer. When ...

  7. Paint the Grid Again (隐藏建图+优先队列+拓扑排序)

    Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or ...

  8. 日常训练 dfs 之 拓扑排序

    今天被拓扑排序给折磨了一天,主要就是我的一个代码有点小bug,真难找... 先来看看我今天写的题目吧! C. Fox And Names Fox Ciel is going to publish a ...

  9. POJ 2585:Window Pains(拓扑排序)

    Window Pains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2524   Accepted: 1284 Desc ...

随机推荐

  1. 一个可创建读取日志的管理类(可固定创建2M大小的日志文件)

    这里,将日志管理基类命名为LogManagerBase(抽象类),具体的不同类型的日志可以通过继承完成.该基类可将日志以每个2M的方式存储起来,并可以读取当前正在使用的日志的所有内容. 要实现该基类, ...

  2. 用Log4Net记录NHibernate中执行的SQL语句及执行时间

    首页,在web.config中加入以下配置: <configuration> <configSections> <section name="log4net&q ...

  3. C++头文件中预编译宏的目的

    C++头文件中预编译宏的目的 eg: #ifndef _FACTORY_H_#define _FACTORY_H_......#endif //~_FACTORY_H_ 防止头文件被重复包含,导致变量 ...

  4. 怎样基于android4.4.2的源代码和android-4.3.1_r1的驱动编译I9250的ROM

    怎样基于android4.4.2的源代码和android-4.3.1_r1的驱动编译I9250的ROM 作者:雨水  2014-05-04 联系方式:dennis.hu.cd at gmail.com ...

  5. Beautyacticle

    Original: https://github.com/rizhilee/Beautyacticle Backup: https://github.com/eltld/Beautyacticle

  6. WEB的相关知识总结

    JS-->OOP/Module, DOM, JSON, AJAX------------------><script>, script.js的内容 HTML/JS/CSS HT ...

  7. [MySQL登录错误] ERROR1045 (28000): Access denied for user 'omonroy'@'20.112.251.19' (using password:YES)

    收到美国那边同事carl的call说用户登录不上去了,不过2个礼拜前他还用的好好的,他给我发email了,他有急事需要处理麻烦我记尽快协助,他在email有截取错误信息: root@xxxxx:/ho ...

  8. 都是类型惹的祸——小心unsigned

    正如我们所知道的,编程语句都有很多的基本数据类型,如char,inf,float等等,而在C和C++中还有一个特殊的类型就是无符号数,它由unsigned修饰,如unsigned int等.大家有没想 ...

  9. zoj-3626 Treasure Hunt I (树形dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接: zoj-3626 题意 给一棵n个节点的树, 节点编号1~n, 每个节点有权值val[i],经过这个节点就可 ...

  10. swift 获取UI上某点点颜色

    extension UIView { func colorOfPoint (point: CGPoint) -> UIColor { var pixel = UnsafePointer<C ...