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. OSAL

    1. CC254x的软件基于OSAL架构 2. OSAL并非一个真的操作系统, 而只是让软件设立一个事件的执行方法(setup excution of events) 3. 每一个子系统都在OSAL的 ...

  2. 在MyEclipse中搭建Spring MVC开发环境

    环境版本 IDE:MyEclipse 8.5 Spring:spring 3.2.8 JDK:1.6 1.打开MyEclipse-->File-->New-->Web Project ...

  3. canvas 动画

    1.随机产生形状,做360度运转,带有一个开始开始按钮一个停止按钮 var canvas=$('.mycanvas'); canvas.attr("width",500);//$( ...

  4. php的内存分配还是很智能的

    <?php echo memory_get_usage().PHP_EOL;$a = 1;$b = $a;echo memory_get_usage().PHP_EOL; <?php ec ...

  5. js词法作用域规则

    function foo() {console.log( a ); // 2不是3} function bar() {var a = 3;foo();} var a = 2;bar(); js中的作用 ...

  6. java 面试每日一题4

    题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?  1.程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. publ ...

  7. HDU 1060 Leftmost Digit

    基本思路:(参考大神和加自己的思考) 考虑到此题需要输入这么大的数a,并且还的求aa,求出来会更大,更多位.当时考虑用大数方法求(数组实现),结果实现不行.看网上大神采用对数法,巧妙避开处理这么大的数 ...

  8. hasOwnproperty详细总结

    hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象.不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员. isPrototypeO ...

  9. HDU(1175),连连看,BFS

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...

  10. MSSQL删除字段时出现 服务器: 消息 5074,级别 16,状态 1,行 1 的解决办法

    有的朋友在做用户维护字段的界面时,肯定发现一个问题,当用脚本:ALTER TABLE 表名 DROP COLUMN 字段名进行删除字段的操作时,会出现“服务器: 消息 5074,级别 16,状态 1, ...