我是靠谱客的博主 稳重大雁,最近开发中收集的这篇文章主要介绍Hdoj 5154 Harry and Magical Computer 【拓扑】Harry and Magical Computer,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Harry and Magical Computer

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


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. 1n100,1m10000
The next following m lines, each line contains two numbers a b, indicates a dependencies (a, b). 1a,bn
 

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

 注意到: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.这一句,就是说b一定要在a前面完成,有一种先后关系,很明显拓扑。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int M = 105;
struct node{
int to, next;
}e[M*M];
bool vis[M];
int in[M], head[M*M];
int n, m, tot;
void init(){
/*for(int i = 0; i < M; ++ i){
/map[i].clear();
in[i] = 0;
vis[i] = 0;
}*/
memset(in, 0, sizeof(in));
memset(head, -1, sizeof(head));
}
void add(int a, int b){
e[tot].to = a;
e[tot].next = head[b];
head[b] = tot++;
}
bool topo(){
memset(vis, 0, sizeof(vis));
int cou = 0, num;
queue<int >q;
for(int i = 1; i <= n; ++i)
if(!in[i]){
vis[i] = 1;q.push(i);
}
while(!q.empty()){
int temp = q.front(); q.pop(); ++cou;
for(int i = head[temp]; ~i; i = e[i].next){
int v = e[i].to;
if(!(--in[v])&&!vis[v]){
q.push(v);
vis[v] = 1;
}
}
}
if(cou == n) return 1;
else return 0;
}
int main(){
while(scanf("%d%d", &n, &m) == 2){
init();
int a, b;
tot = 0;
for(int i = 0; i < m; ++ i){
scanf("%d%d", &a, &b);
add(a, b); ++in[a];
}
//cout << "sad"<<endl;
bool flag = topo();
if(flag) cout << "YESn";
else cout << "NOn";
}
return 0;
}


最后

以上就是稳重大雁为你收集整理的Hdoj 5154 Harry and Magical Computer 【拓扑】Harry and Magical Computer的全部内容,希望文章能够帮你解决Hdoj 5154 Harry and Magical Computer 【拓扑】Harry and Magical Computer所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部