Harry and Magical Computer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 499    Accepted Submission(s): 233

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
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100001
const int inf=0x7fffffff; //无限大
int map[][];
int vis[];
int flag[];
int main()
{
int n,m;
while(cin>>n>>m)
{
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
memset(flag,,sizeof(flag));
int a,b;
for(int i=;i<m;i++)
{
cin>>a>>b;
map[b-][a-]=;
flag[a-]=;
}
queue<int> q;
for(int i=;i<n;i++)
{
if(flag[i]==)
{
q.push(i);
vis[i]=;
}
}
int now;
int next;
while(!q.empty())
{
now=q.front();
for(int i=;i<n;i++)
{
if(map[now][i]==)
{
if(vis[i]==)
continue;
q.push(i);
vis[i]=;
}
}
q.pop();
}
int flag1=;
for(int i=;i<n;i++)
{
if(vis[i]==)
{
flag1=;
break;
}
}
if(flag1==)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
}

HDU 5154 Harry and Magical Computer bfs的更多相关文章

  1. hdu 5154 Harry and Magical Computer

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

  2. hdu 5154 Harry and Magical Computer 拓扑排序

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

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

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

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

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

  5. 【HDOJ】5154 Harry and Magical Computer

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

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

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

  7. hdu 5154 拓扑排序

    例题:hdu 5154 链接  http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思是第一行先给出n和m表示有n件事,m个关系,接下来输入m行,每行有 ...

  8. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  9. BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...

随机推荐

  1. 查看linux服务器内存信息

    查看服务器内存信息 dmidecode|grep -P -A5 "Memory\s+Device"|grep Size [root@localhost home]# dmideco ...

  2. js实现ctrl+v粘贴图片或是截图

    浏览器环境:谷歌浏览器 1.ctrl+v粘贴图片都是监听paste时间实现的,复制的数据都存在clipboardData下面,虽然打印显示数据长度为0,但是还是可以获取数据的 2.打印clipboar ...

  3. [HBase]mem store flusher 流程

  4. liblinear和libsvm区别

    来源于知乎: 1. LibLinear是线性核,LibSVM可以扩展到非线性核(当也能用线性核,但同样在线性核条件下会比LibLinear慢很多).2. 多分类:LibLinear是one vs al ...

  5. python3 安装

    Centos7 安装python3 #安装sqlite-devel yum -y install sqlite-devel #安装依赖 yum -y install make zlib zlib-de ...

  6. <编程之美>经典面试题:求二叉树节点的最大距离(我的解法,最容易理解的版本?)

    题目介绍: 如果把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义"距离"为两个节点之间的个数. 写一个程序求一棵二叉树中相距最远的两个节点之间的距离. 如下图所示, ...

  7. js交互

    Js和native交互的方法与问题 实现JS和Native交互有两种方式: 第一种:shouldOverrideUrlLoading(WebView view, String url) 通过给WebV ...

  8. Hive(五)数据类型与库表操作以及中文乱码

    一.数据类型 1.基本数据类型 Hive 支持关系型数据中大多数基本数据类型 类型 描述 示例 boolean true/false TRUE tinyint 1字节的有符号整数 -128~127 1 ...

  9. Linux下光盘镜像生成和刻录

    mkiosfs命令如在/root/下有文件file1 file2 file3maiosfs -o img.ios file1 file2 file3该命令将file1 file2 file3放入到im ...

  10. USACO 6.2 Calf Flac

    Calf Flac It is said that if you give an infinite number of cows an infinite number of heavy-duty la ...