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 ...
随机推荐
- 慕课网-安卓工程师初养成-2-7 Java中变量的使用规则
来源:http://www.imooc.com/code/1242 不得不接受的变量小脾气: 1.Java 中的变量需要先声明后使用 2.变量使用时,可以声明变量的同时进行初始化,也可以先声明后赋值 ...
- jQuery中 wrap() wrapAll() 与 wrapInner()的区别
今晚看书的时候发现jQuery有三个包裹节点的方法,百度了一下jQuery wrap() / wrapAll() / wrapInner(),果然搜索结果 W3School的文档说明是排第一的. 可是 ...
- 恶心的Oracle的if else if...
出处:http://blog.sina.com.cn/s/blog_407d47e60100d8ig.html 前段时间写Oracle存储过程就遇到问题.原来写成这样if 1=2 then null ...
- bzoj1216 [HNOI2003]操作系统
1216: [HNOI2003]操作系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 751 Solved: 419[Submit][Status ...
- Bug修复问题
采用下面的代码,访问网页:http://www.weather.com.cn/data/cityinfo/101010100.html,想读取下图中红框中的内容,但是抛出了IOException,通过 ...
- grep使用
grep常用的使用方法 grep –rns “match_content”filename 查看匹配内容的行 find /path –name “*.h” –o –name “*.cpp” | xar ...
- Sql server 数据库 int 和guid 两者的比较
我们公司的数据库全部是使用GUID做主键的,很多人习惯使用int做主键.所以呢,这里总结一下,将两种数据类型做主键进行一个比较. 使用INT做主键的优点: 1.需要很小的数据存储空间,仅仅需要4 by ...
- ASP.NET Session丢失的情况
正常操作情况下会有ASP.NET Session丢失的情况出现.因为程序是在不停的被操作,排除Session超时的可能.另外,Session超时时间被设定成60分钟,不会这么快就超时的. 现在我就把原 ...
- 【IHttpHandler】使用IHttpHandler防盗链
防盗链的原理,从图片请求的URL地址上判断是否是我们自己网站上的域名,如果不是,恭喜,你的图片已经被盗链了! 那么我来介绍下图片放盗链的一个方法 首先,添加个httpHandlers请求,webcon ...
- WP8_当滚动到滚动条的70%时,自动加载数据效果实现
Touch.FrameReported += Touch_FrameReported; void Touch_FrameReported(object sender, TouchFrameEven ...