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. PetaPoco 笔记

    PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项--单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的 ...

  2. Fragment进阶

    fragment之间的通信,fragment和Activity生命周期之间的关系 通过上一篇浅显的学习了一下,怎么在Activity中添加fragment.在介绍fragment之间的通信之前,我们来 ...

  3. C:内存分配、内存中五大区

     1.内存的划分  (从高到低依次是: 栈区 . 堆区 .全局静态区 . 常量区 . 代码区 )栈区是系统自动回收,堆区是我们手动回收  2. 栈区   在函数内部定义的局部变量和数组.都存放在栈区, ...

  4. JedisPool连接池实现难点

    [http://jiangwenfeng762.iteye.com/blog/1280700]   [可改进的问题] 问题是jedispool有没有办法监控状态,比如说当前连接有多少,当前idle连接 ...

  5. VTK序列图像的读取[转][改]

    医学图像处理的应用程序中,经常会碰到读取一个序列图像的操作.比如CT.MR等所成的图像都是一个切面一个切面地存储的,医学图像处理程序要处理这些数据,第一步当然是把这些数据从磁盘等外部存储介质中导入内存 ...

  6. (剑指Offer)面试题35:第一个只出现一次的字符

    题目: 在字符串中找出第一个只出现1次的字符,如输入“abaccdeff”,则输出b. 思路: 1.暴力遍历 从头开始扫描字符串中的每个字符,当访问某个字符时,取该字符与后面的每个字符相比较,如果没有 ...

  7. PHP发送邮件。

    第三方类库: ①.email.class.php. ②.phpmailer:https://github.com/PHPMailer/PHPMailer. PHPMailer发送邮件”SMTP 错误: ...

  8. MySQL 索引优化 btree hash rtree

    一.MySQL索引类型 mysql里目前只支持4种索引分别是:full-text,b-tree,hash,r-tree b-tree索引应该是mysql里最广泛的索引的了,除了archive基本所有的 ...

  9. Codeforces Round #340 (Div. 2) D. Polyline 水题

    D. Polyline 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co There are three p ...

  10. Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合

    C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...