BC Harry and Magical Computer (拓扑排序)
Harry and Magical Computer
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.
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 one line for each test case. If the computer can finish all the process print "YES" (Without quotes). Else print "NO" (Without quotes).
3 2
3 1
2 1
3 3
3 2
2 1
1 3
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 (拓扑排序)的更多相关文章
- hdu 5154 Harry and Magical Computer 拓扑排序
Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU5154拓扑排序
Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- Java排序算法——拓扑排序
package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...
- CF Fox And Names (拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- POJ1270 Following Orders (拓扑排序)
Following Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4254 Accepted: 1709 ...
- (简单) HDU 5154 Harry and Magical Computer,图论。
Description In reward of being yearly outstanding magic student, Harry gets a magical computer. When ...
- 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 ...
- 日常训练 dfs 之 拓扑排序
今天被拓扑排序给折磨了一天,主要就是我的一个代码有点小bug,真难找... 先来看看我今天写的题目吧! C. Fox And Names Fox Ciel is going to publish a ...
- POJ 2585:Window Pains(拓扑排序)
Window Pains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2524 Accepted: 1284 Desc ...
随机推荐
- android AsyncHttpClient 开源框架的使用
AsyncHttpClient 1.在很多时候android都需要进行网络的操作,而android自带的HttpClient可以实现,但要进行很多网络连接的时候(如:下载很多图片),就需要线程池来进行 ...
- C:进制
进制.C语言内存分配 1.对于进制 10进制 (0 - 9)16进制 (0——9 A B C D E F)硬件中的高低电平(0 和 1表示)所以计算机用 二进制 机器语言就是由 0 和 1 组成的一 ...
- Jupyter增加内核
本例的Jupyter安装在Python3下,以增加Python2内核为例. 首先确认在Python3下已安装了内核: ipython kernel install --user #or python3 ...
- Oracle:递归查询(树形结构数据)
今天要做一个查询功能:查询某用户所属部门,且包含该部门的所有上级部门信息.偶然找到了一个方法,特意来做个笔记.分享给和我一样的菜鸟,哈哈 查询子节点 1 select * 2 from d_arc_d ...
- DeleteDC() 与 ReleaseDC() 的区别 [转]
DeleteDC 该函数删除指定的设备上下文环境(DC). 原型: BOOL DeleteDC(HDC hdc): 参数: hdc:设备上下文环境的句柄. 返回值: 成功,返回非零值:失败,返回零.调 ...
- $(document).ready()与 window.onload执行时机
$(document).ready()方法和window.onload方法有相似的功能,但是在执行时机方面是有区别的.window.onload方法是子啊网页中的所有元素(包括元素的所有关联的文件)完 ...
- VCL -- Understanding the Message-Handling System
Understanding the Message-Handling System http://docwiki.embarcadero.com/RADStudio/XE7/en/Understand ...
- Windows Message Codes
https://www.autoitscript.com/autoit3/docs/appendix/WinMsgCodes.htm WM_ACTIVATE 0x0006 WM_ACTIVATEAPP ...
- Myeclipse的快捷键大全
选择你要注释的那一行或多行代码,按Ctrl+/即可,取消注释也是选中之后按Ctrl+/即可. 如果你想使用的快捷键的注释是的话,那么你的快捷键是ctrl+shift+/ 我以前都是手动注释的,直接打/ ...
- ORCALE用户授权与创建同义词
创建同义词: DROP public synonym marketmonitor_day; DROP public synonym marketmonitor_month; DROP public s ...