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. Runtime.getRuntime().exec 类 防止阻塞

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; impor ...

  2. poj1077

    题意:给出一个八数码问题,求解法,不可解则输出unsolvable. 分析:可以用ida*算法,估价函数可以使用每个数码到其最终位置的最短距离之和.对于不可解的判断,我这里用迭代深度大于100时判定为 ...

  3. 32 Profiling Go Programs 分析go语言项目

    Profiling Go Programs  分析go语言项目 24 June 2011 At Scala Days 2011, Robert Hundt presented a paper titl ...

  4. Flask:操作SQLite3(0.1)

    Windows 10家庭中文版,Python 3.6.4,Flask 1.0.2 本文介绍了第一次在Flask框架中操作SQLite3数据库的测试,参考了官网的文档Using SQLite 3 wit ...

  5. 【轨迹动画css】不规则轨迹动画css教程,弹球,客服广告悬浮层都可以用

    小demo如下,可更具自己需求修改: css @keyframes animX{ 0% {left: 0px;} 100% {left: 500px;} } @keyframes animY{ 0% ...

  6. java基础78 Servlet的生命周期

    1.Servlet的生命周期 简单的解析就是: 创建servlet实例(调用构造器)---->调用init()方法---->调用service()方法----->调用destroy( ...

  7. Qt通过ODBC来操作Excel

    示例代码: #include<QtCore/QCoreApplication> #include<QtSql> #include<QObject> #include ...

  8. Kafka ACL使用实战(单机版)

    一.简介 自0.9.0.0.版本引入Security之后,Kafka一直在完善security的功能.当前Kafka security主要包含3大功能:认证(authentication).信道加密( ...

  9. N的阶乘末尾有多少个0

    N的阶乘(N!)中的末尾有多少个0? N的阶乘可以分解为: 2的X次方,3的Y次方,4的5次Z方,.....的成绩.由于10 = 2 * 5,所以M只能和X和Z有关,每一对2和5相乘就可以得到一个10 ...

  10. HBase结合MapReduce批量导入(HDFS中的数据导入到HBase)

    HBase结合MapReduce批量导入 package hbase; import java.text.SimpleDateFormat; import java.util.Date; import ...