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"。。。
![](/assets/blank.gif)
![](/assets/blank.gif)
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<queue> 8 #include<map> 9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::find; 13 using std::sort; 14 using std::pair; 15 using std::vector; 16 using std::queue; 17 using std::multimap; 18 #define pb(e) push_back(e) 19 #define sz(c) (int)(c).size() 20 #define mp(a, b) make_pair(a, b) 21 #define all(c) (c).begin(), (c).end() 22 #define iter(c) decltype((c).begin()) 23 #define cls(arr,val) memset(arr,val,sizeof(arr)) 24 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 25 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 27 const int N = 110; 28 int n, m, G[N][N]; 29 bool vis[N], flag[N]; 30 typedef unsigned long long ull; 31 void bfs() { 32 bool f = false; 33 queue<int> que; 34 rep(i, n) { 35 if (!flag[i]) { 36 vis[i] = true; 37 que.push(i); 38 } 39 } 40 while (!que.empty()) { 41 int p = que.front(); que.pop(); 42 rep(i, n) { 43 if (G[p][i]) { 44 if (vis[i]) continue; 45 que.push(i); 46 vis[i] = true; 47 } 48 } 49 } 50 rep(i, n) { 51 if (!vis[i]) { f = true; break; } 52 } 53 puts(f ? "NO" : "YES"); 54 } 55 int main() { 56 #ifdef LOCAL 57 freopen("in.txt", "r", stdin); 58 freopen("out.txt", "w+", stdout); 59 #endif 60 int a, b; 61 while (~scanf("%d %d", &n, &m)) { 62 cls(vis, 0), cls(flag, 0), cls(G, 0); 63 rep(i, m) { 64 scanf("%d %d", &a, &b); 65 G[b - 1][a - 1] = 1; 66 flag[a - 1] = 1; 67 } 68 bfs(); 69 } 70 return 0; 71 }
View Code
转载于:https://www.cnblogs.com/GadyPu/p/4603743.html
hdu 5154 Harry and Magical Computer相关推荐
- 【HDU】5304 Eastest Magical Day Seep Group's Summer【环dp+生成树计数】
传送门:[HDU]5304 Eastest Magical Day Seep Group's Summer my code: my~~code: #include <bits/stdc++.h ...
- BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...
- jekins 指定分支_jenkins的pipeline拉取指定分支的代码
脚本示例 pipeline { agent any options { durabilityHint 'PERFORMANCE_OPTIMIZED' timeout(time:5, unit: 'MI ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- hdu 2196 computer
hdu 2196 题意 给出一棵树,求出树上每一个点在树上走一条简单路径所能走的最长距离. 解法 说起来,这是我今天1A的第一题 我们设 \(up[i]\) 表示从这个点向上走到某个点又向下走的最长距 ...
- 【HDU - 1968】【UVA - 12096】The SetStack Computer (模拟,集合求交集并集操作,STL实现)
题干: Background from Wikipedia: 揝et theory is a branch of mathematics created principally by the Germ ...
- HDU多校联合赛(1007 Magical Forest)模拟题
题目: Problem Description There is a forest can be seen as N * M grid. In this forest, there is some m ...
- Hdu 2196 - Computer
即求树上每点的最长路,先求出树的直径上的一点(dfs.bfs均可,寻找第一点我用的bfs),然后再从这点搜出树直径的另一点.可以证明每点的最长路是到这两点的距离之一(因为树是连通的,因为是树的直径,如 ...
- HDU - 2196 Computer(树形dp)
题目链接:点击查看 题目大意:给定n个点以及n-1条边,保证可以组成一棵树,问每个点所能到达的最远距离 题目分析:首先这是一颗无向图所组成的树,经过分析,我们可以得到任何一个点,对于它所能到达的最远距 ...
最新文章
- Banner图的播放
- 微软开源NAS算法Petridish,提高神经网络迁移能力
- Linux进阶:DNS详解
- Java设计模式(八):外观设计模式
- 2014-07-28 使用Axure RP进行手机端BBS的原型设计
- 程序猿的崛起,一篇文章看懂编程语言
- 从电子电路到嵌入式系统(开篇)
- NetBeans 时事通讯(刊号 # 127 - Dec 01, 2010)
- node.js 谷歌翻译api
- 基于JSP的旅游网站系统
- NI RS422/RS485接线方式
- html tr行内样式左对齐,探索CSS单行文字居中,多行文字居左的实现方式
- 【蓝桥杯】第十三届蓝桥杯省赛 AK 攻略 —— C++ B组全真题超详细剖析
- 搭配Online:南方航2020年1月正式退出天合联盟
- Linux下修改环境变量
- 前端工程师的摸鱼日常(7)
- 971: 统计利用先序遍历创建的二叉树的深度
- UE4 Material 101学习笔记——30-37 植物叶片(透光/mask/面片隐藏/法线调整/AO/渐隐/世界空间色彩/随风舞动)
- autojs-获取api接口JSON值
- ViewPager 添加广告页面小圆点指示器效果
热门文章
- hadoop集群服务重启后出错
- Mysql 中转换表的引擎
- (分治)分治法 及 题目
- read实现交互输入自动化(笔记)
- 今天终于安装了Snippet Compiler!!!
- csgo手机上看demo_仪表上的车速和手机导航不一样,哪个更准?碰到测速了该看哪个?...
- 浮点错误的意思-PAT 、OJ
- 1035. 插入与归并(25)-浙大PAT乙级真题
- 1022. D进制的A+B (20)-PAT乙级真题
- python colorbar位置大小调整_python - matplotlib相邻子图:添加colorbar更改子图的大小 - 堆栈内存溢出...