hdu 5154 Harry and Magical Computer
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5154
Harry and Magical Computer
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 \leq n \leq 100,1 \leq m \leq 10000$
The next following m lines, each line contains two numbers a b, indicates a dependencies $(a, b). 1 \leq a, b \leq 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
先建立一张有向图,再遍历,若有顶点未访问到输出"NO",否则输出"YES"。。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::pair;
using std::vector;
using std::queue;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
int n, m, G[N][N];
bool vis[N], flag[N];
typedef unsigned long long ull;
void bfs() {
bool f = false;
queue<int> que;
rep(i, n) {
if (!flag[i]) {
vis[i] = true;
que.push(i);
}
}
while (!que.empty()) {
int p = que.front(); que.pop();
rep(i, n) {
if (G[p][i]) {
if (vis[i]) continue;
que.push(i);
vis[i] = true;
}
}
}
rep(i, n) {
if (!vis[i]) { f = true; break; }
}
puts(f ? "NO" : "YES");
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int a, b;
while (~scanf("%d %d", &n, &m)) {
cls(vis, ), cls(flag, ), cls(G, );
rep(i, m) {
scanf("%d %d", &a, &b);
G[b - ][a - ] = ;
flag[a - ] = ;
}
bfs();
}
return ;
}
hdu 5154 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 ...
- (简单) HDU 5154 Harry and Magical Computer,图论。
Description In reward of being yearly outstanding magic student, Harry gets a magical computer. When ...
- HDU 5154 Harry and Magical Computer bfs
Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 5154 Harry and Magical Computer 有向图判环
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5154 题解: 有向图判环. 1.用dfs,正在访问的节点标记为-1,已经访问过的节点标记为1,没有访 ...
- 【HDOJ】5154 Harry and Magical Computer
拓扑排序. /* 5154 */ #include <iostream> #include <cstdio> #include <cstring> #include ...
- BC Harry and Magical Computer (拓扑排序)
Harry and Magical Computer Accepts: 350 Submissions: 1348 Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 5154 拓扑排序
例题:hdu 5154 链接 http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思是第一行先给出n和m表示有n件事,m个关系,接下来输入m行,每行有 ...
- BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...
- HDU5154拓扑排序
Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
随机推荐
- 将U盘分成 启动盘+文件存储区
我看了很多帖子,发现想要将U盘分区的朋友绝大部分是和我一样,想用U盘做成一个启动盘同时兼顾文件存储,分区的目的很简单,就是想将启动部分单独做成一个区,以免在日常的应用中使得启动文件染毒或者误操作造成损 ...
- onclicklistener到底怎么用?
转载地址:http://blog.csdn.net/dickren123/article/details/7216975 相信很多像我一样的新手学习Android开发会遇到这个问题,通过这几天的归类和 ...
- spinlock自旋锁de使用
Linux内核中最常见的锁是自旋锁.一个自旋锁就是一个互斥设备,它只能有两个值:"锁定"和"解锁".如果锁可用,则"锁定"位被设置,而代码继 ...
- C++记录debug信息的log类
取自:http://www.viksoe.dk/code/all_mfc.htm,里面有各种MFC常用的类 // LogFile.h: interface for the CLogFile class ...
- Sublime Text 2/3安装CTags实现函数跳转
安装ctags 下载 ctags程序,放到目录D:/ctags/下 安装ctags插件 1. 打开Sublime Text 2. Preferences->Package Control-> ...
- aspx网页相对布局
网页的布局 <body bgcolor="#b6b7bc"> <form id="form1" runat="server" ...
- 根据JSON对象动态加载表格--大数据量
EasyUI的DataGrid加载数据的时候,如果列数过多(300列以上),数据渲染及其缓慢. JSON对象格式: 1:rowno 2:title 3:colspan 4:rowspan 5:back ...
- EasyUI datagrid 复杂表头处理
1:表头固定(前台写) 参照官方:http://www.jeasyui.net/demo/334.html 效果图: 源代码如下: <!DOCTYPE html> <html> ...
- 【改进版】C++小程序中一个cout输出语句背后的堆栈知识
最开始写这篇文章的时候,凭着自己对汇编的一点理解就堆出了这些内容,经 egmkang的指点,才发觉自己是井底之蛙,花了半天的功夫,去学习顺序点等内容.针对上次写的程序,我决定添一些内容,把程序2后面的 ...
- 怎样在Android中ListView与ArrayAdapter配合使用
[代码]main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...