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. UML精粹学习 - 订单类结构图

    Order Class Diagram of Martin Fowler's UML Distilled

  2. Codeforces Gym 100463B Music Mess Hash 逻辑题

    Music Mess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments ...

  3. Codeforces Beta Round #13 E. Holes 分块暴力

    E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...

  4. Testin云測公布首份国内应用质量报告:半数APP平均启动时间不合格

    Testin云測公布首份国内应用质量报告:半数APP平均启动时间不合格 2014/10/23 · Testin · 实验室报告 日前,Testin云測旗下质量管家Master通过随机取样1605款国内 ...

  5. 创建透明的UIToolbar

    TranslucentToolbar.h文件 #import <UIKit/UIKit.h> @interface TranslucentToolbar : UIToolbar @end ...

  6. ACM-经典DP之Monkey and Banana——hdu1069

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  7. linux消息队列操作

    对消息队列的操作无非有以下三种类型: 1. 打开或创建消息队列消息队列的内核持续性要求每一个消息队列都在系统范围内相应唯一的键值,所以,要获得一个消息队列的描写叙述字,仅仅需提供该消息队列的键值就可以 ...

  8. Android adb.exe程序启动不起来,如何处理

    经常遇到 Please ensure that adb is correctly located at 'D:\java\sdk\platform-tools\adb.exe' and can be ...

  9. $.getJSON()方法的 callback说明

    $.getJSON()方法跨域 去取得服务器的json对象的时候,url的后缀最后带一个"callback=?"的参数作为成功的回调函数:如: var url = "${ ...

  10. Doing Homework 状态压缩DP

    Doing Homework 题目抽象:给出n个task的name,deadline,need.  每个任务的罚时penalty=finish-deadline;   task不可以同时做.问按怎样的 ...