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. 关于mysql存储过程的definer的问题

    由于对mysql了解不够透彻,导致对definer问题查了好久才解决问题 记录自己的一些理解! 问题描述: 在数据库写,为一个表写了一个触发器,此触发器调用一个存储过程:由公司写的一个c程序自动往该表 ...

  2. ReentrantLock

    与synchronized相同并发性和内存语义. [新增特性]锁投票.定时锁等候.可中断锁等候.更少时间调度线程. [用法注意点]Lock必须在finally块中释放. Lock lock = new ...

  3. Hibernate操作和保存方式

    Session API [Java Hibernate 之 CRUD 操作]http://www.codeceo.com/article/java-hibernate-crud.html   [Ses ...

  4. redis的发布订阅模式

    概要 redis的每个server实例都维护着一个保存服务器状态的redisServer结构 struct redisServer {     /* Pubsub */     // 字典,键为频道, ...

  5. jquery网页字体变大小

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. webstorm 主题设置 皮肤设置

    推荐个编辑器主题下载的一个网站. Color Themes    网址:http://color-themes.com [点这里直接跳转] 但是,只支持几个编辑器. 各种颜色搭配的主题,随你选择!我个 ...

  7. Keil MDK AGDI Drivers, ULink, JLink, ST-Link, NuLink, JTAGjet

    AGDI Drivers AGDI is an Application Program Interface (API) third-party developers can use to create ...

  8. nginx-1.4.4 + tcp_proxy_module手动编译安装

    Nginx开源软件默认没有提供TCP协议的负载均衡,下面记录一下我的安装过程: 1. 下载nginx最新稳定版的源码 mkdir /software cd /software yum install ...

  9. C++ Interview - using new and delete to alloc and free memory

    1. dynamic create object and initialization int *pi = new int; // pi points to an uninitialized int ...

  10. hdu 3339 In Action 背包+flyod

    In Action Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=333 ...