HDU 5154 Harry and Magical Computer(找环)
2015-08-31 21:25
281 查看
Harry and Magical Computer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1743 Accepted Submission(s): 684
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
Source
BestCoder Round #25
问题描述
作为年度优秀魔法学员的奖赏,哈利得到了一台具有魔力的计算机。这台计算机一旦开始处理某个任务,就会一直处理到这个任务结束为止(所以你可以认为它是单线程的)。有一天,这台计算机得到了n个任务要处理,分别标号1到n。这n个任务之间又有一些依赖关系,假如存在依赖关系(a, b),那么要处理a任务,必须先将b任务完成。现在哈利得到了所有的这些依赖关系,一共m个。他想知道,这台计算机能否完成所有的任务。
输入描述
多组输入数据 每组数据第一行由n m组成,分别代表任务数和依赖关系数。1 \leq n \leq 100, 1 \leq m \leq 100001≤n≤100,1≤m≤10000 接下来m行,每行两个数a b,表示一组依赖关系。1 \leq a, b \leq n1≤a,b≤n
输出描述
每组输出一行,"YES"(没有引号)表示能完成所有任务。 "NO"(没有引号)表示不能。
输入样例
3 2 3 1 2 1 3 3 3 2 2 1 1 3
输出样例
YES NO
解题思路:如果存在任务不能完成,那么说明该任务既出现在某任务的前面又出现在某任务的后面,换句话说就是某些任务与该任务一起组成了一个环。用弗洛伊德算法,判断最后的邻接矩阵的对角线是否有1存在,如果有,说明存在环,否则没有。
代码如下:
#include <cstdio> #include <iostream> #include <cstdlib> #include <cstring> #include <cmath> #include <string> #include <algorithm> #include <vector> #include <deque> #include <list> #include <set> #include <map> #include <stack> #include <queue> #include <numeric> #include <iomanip> #include <bitset> #include <sstream> #include <fstream> #include <limits.h> #include <ctime> #define debug "output for debug\n" #define pi (acos(-1.0)) #define eps (1e-6) #define inf (1<<28) #define sqr(x) (x) * (x) #define mod 1000000007 using namespace std; typedef long long ll; typedef unsigned long long ULL; int a[105][105]; int main() { int i,j,k,n,m,x,y,t; while(~scanf("%d%d",&n,&m)) { memset(a,0,sizeof(a)); for(i=1;i<=m;i++) { scanf("%d%d",&x,&y); a[y][x]=1; } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { for(k=1;k<=n;k++) { if(a[i][j]<min(a[i][k],a[k][j])) a[i][j]=min(a[i][k],a[k][j]); } } } int flag=0; for(i=1;i<=n;i++) { if(a[i][i]==1) { flag=1; break; } } if(flag) printf("NO\n"); else printf("YES\n"); } return 0; }
相关文章推荐
- CentOS下安装软件的方法
- Jsp分页实例---真分页
- Linux学习二:Makefile基础
- TopCoder SRM 666 Div2 Problem 999 - WalkOverATreeDiv2 (树形DP)
- OSI七层模型
- 数据库设计(2)生命周期
- linux select 模型详解
- scala 学习笔记(07) 一等公民的函数
- php大力力 [040节] 买了一天域名,整了一天后台,新网后台不懂啊
- 初进hp感想和目标
- HDU DFS
- 枚举类型的使用
- where、having、group by、order by的顺序
- Hibernate自学详细笔记
- 百度面试题 最长公共子串-动态规划法
- Linux学习一:安装/配置vi,熟悉gcc/vi
- 我排第几个
- android传递数据方式2--通过剪切板传数据
- 间隔300ms第一次一个管亮流动一次,第二次两个管亮流动。依次到8个管亮,然后重复此过程
- poj 2226 Muddy Fields(最小点覆盖+巧妙构图)