【拓扑排序专题】HDU 5154 Harry and Magical Computer

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qian2213762498/article/details/82493896

http://acm.hdu.edu.cn/showproblem.php?pid=5154

Harry and Magical Computer

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

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

Recommend

heyang   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

思路

题目大意: 哈利用一个魔法电脑处理N个任务,但是有M个前后关系(a,b),意思是在b执行之前必须先执行a,

即a任务在b任务前,问你是否能满足要求处理完这N个任务。(也就是判断扔进队列的数目是否为n?)

思路:拓扑排序。裸的模板题,需要理解拓扑排序实质的流程。

因为拓扑排序本身用到了队列。它先将所有入度为0的点入队,并用num统计入度不为0的点。

遍历队列中的点所连的所有边,并减少该点连接边另一端的入度,只要另一端入度为0了,就将它加入队列中,

最后比较n和num是否相等就可以判断是否能处理完这N个任务。

AC Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int nmax=1010;
vector<int>G[nmax];
int inDegree[nmax];
int n,m;

bool toposort(){
	int num=0;
	queue<int>q;
	while(!q.empty()) q.pop();
	for(int i=1;i<=n;i++){//顶点编号:1~N
		if(inDegree[i]==0){
			q.push(i);//把入度为0的顶点扔进队列 
		} 
	}
	while(!q.empty()){
		int u=q.front();
		q.pop();
		num++;
		for(int i=0;i<G[u].size();i++){
			int v=G[u][i];
			inDegree[v]--;
			if(inDegree[v]==0){
				q.push(v);
			}
		}
	}
	if(num==n) //没有环路 
		return true;
	else		//有环路 
		return false;
	
}


int main(int argc, char** argv) {
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(inDegree,0,sizeof(inDegree));
		for(int i=1;i<=n;i++){
			G[i].clear();
		}
		int u,v;
		for(int i=0;i<m;i++){
			scanf("%d %d",&u,&v);
			G[u].push_back(v);
			inDegree[v]++;
		}
		if(toposort()) puts("YES");
		else puts("NO");
	} 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qian2213762498/article/details/82493896