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 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
 题意:有向图判环;
思路:拓扑完没点;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define esp 0.00000000001
const int N=2e3+,M=1e6+,inf=1e9;
int n,m;
vector<int>edge[N];
int du[N];
int main()
{
while(~scanf("%d%d",&n,&m))
{
queue<int>q;
memset(du,,sizeof(du));
for(int i=;i<=n;i++)
edge[i].clear();
int ans=;
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
edge[u].push_back(v);
du[v]++;
}
for(int i=;i<=n;i++)
{
if(!du[i])q.push(i);
}
while(!q.empty())
{
int v=q.front();
q.pop();
ans++;
for(int i=;i<edge[v].size();i++)
{
du[edge[v][i]]--;
if(!du[edge[v][i]])
q.push(edge[v][i]);
}
}
if(ans==n)
printf("YES\n");
else
printf("NO\n");
}
return ;
}

hdu 5154 Harry and Magical Computer 拓扑排序的更多相关文章

  1. hdu 5154 Harry and Magical Computer

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5154 Harry and Magical Computer Description In reward ...

  2. BC Harry and Magical Computer (拓扑排序)

    Harry and Magical Computer  Accepts: 350  Submissions: 1348  Time Limit: 2000/1000 MS (Java/Others) ...

  3. HDU 5154 Harry and Magical Computer 有向图判环

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5154 题解: 有向图判环. 1.用dfs,正在访问的节点标记为-1,已经访问过的节点标记为1,没有访 ...

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

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

  5. HDU 5154 Harry and Magical Computer bfs

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

  6. 【HDOJ】5154 Harry and Magical Computer

    拓扑排序. /* 5154 */ #include <iostream> #include <cstdio> #include <cstring> #include ...

  7. HDU 6073 Matching In Multiplication(拓扑排序+思维)

    http://acm.hdu.edu.cn/showproblem.php?pid=6073 题意:有个二分图,左边和右边的顶点数相同,左边的顶点每个顶点度数为2.现在有个屌丝理解错了最佳完美匹配,它 ...

  8. HDU 5195 DZY Loves Topological Sorting 拓扑排序

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  9. HDU 3342 Legal or Not(拓扑排序判断成环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

随机推荐

  1. 【python cookbook】【字符串与文本】9.将Unicode文本统一表示为规范形式

    问题:确保所有的Unicode字符串都拥有相同的底层 解决方案:为解决同一个文本拥有多种不同的表示形式问题,应该先将文本统一表示为规范形式,这可以通过unicodedata模块来完成, unicode ...

  2. 【python cookbook】【数据结构与算法】7.让字典保持有序

    问题:创建一个字典,同时对字典做迭代或序列化操作时,也能控制其中元素的顺序: 解决方案:可以使用collections模块中的OrderedDict类来控制字典中元素的顺序.当对字典做迭代时,他会严格 ...

  3. STM32外部中断.

    void EXTIX_Init(void){     EXTI_InitTypeDef EXTI_InitStructure;    NVIC_InitTypeDef NVIC_InitStructu ...

  4. Ubuntu检测磁盘是否挂载

    Ubuntu默认不自动挂载磁盘. 只是学习Bash使用,需优化如使用 # file: mountAll.sh # include color support # a list of variables ...

  5. weblogic安装失败

    weblogic无法安装所选应用程序 Exception in AppMerge flows' progression Exception in AppMerge flows' progression ...

  6. java数组初始化

    java数组初始化 //静态初始化数组:方法一 String cats[] = new String[] { "Tom","Sam","Mimi&qu ...

  7. Spring集成memcached的详细介绍

    前提条件:工程需要引入jar包java_memcached-release_2.0.1.jar 第一步:添加memcached的配置文件. <bean class="org.sprin ...

  8. html+css复习之第1篇

    1. 保证在火狐浏览器字体<12px,苹果横屏的时候字体显示大小还是12px html { background: #fff; -webkit-text-size-adjust: 100%; - ...

  9. java 类加载顺序

    1.虚拟机在首次加载Java类时,会对静态初始化块.静态成员变量.静态方法进行一次初始化 2.只有在调用new方法时才会创建类的实例 3.类实例创建过程:按照父子继承关系进行初始化,首先执行父类的初始 ...

  10. HTML,javaScript,DOM详解

    HTML DOM 教程 DOM 教程 DOM 简介 HTML DOM 定义了访问和操作 HTML 文档的标准方法. DOM 将 HTML 文档表达为树结构. HTML DOM 树 HTML DOM 简 ...