欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU3342 Legal or Not(拓扑排序)

程序员文章站 2024-03-19 12:17:52
...

HDU3342 Legal or Not(拓扑排序)

Description
ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many “holy cows” like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost “master”, and Lost will have a nice “prentice”. By and by, there are many pairs of “master and prentice”. But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?
We all know a master can have many prentices and a prentice may have a lot of masters too, it’s legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian’s master and, at the same time, 3xian is HH’s master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not.
Please note that the “master and prentice” relation is transitive. It means that if A is B’s master ans B is C’s master, then A is C’s master.
Input
The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y’s master and y is x’s prentice. The input is terminated by N = 0.
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,…, N-1). We use their numbers instead of their names.
Output
For each test case, print in one line the judgement of the messy relationship.
If it is legal, output “YES”, otherwise “NO”.
Sample Input
3 2
0 1
1 2
2 2
0 1
1 0
0 0
Sample Output
YES
NO

题意

给定一个图,判断是否能连成环,若可以则输出NO,否则输出YES。我用了bfs去进行拓扑。

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<functional> 
#include<map>
using namespace std;
typedef long long ll;
const int N=1e5+10,NN=1e3+10,INF=0x3f3f3f3f;
const ll MOD=1e9+7;
int num_edge,n,m,sum;
int head[N],indegree[N];
struct Edge{
	int next;
	int to;
}edge[N];
void add_edge(int from,int to){
	num_edge++;
	edge[num_edge].next=head[from];
	edge[num_edge].to=to;
	head[from]=num_edge;
} 
void topo(){
	priority_queue<int,vector<int>,greater<int> >q;
	for(int i=0;i<n;i++){
		if(!indegree[i]){
			q.push(i);
			++sum;
		}
	}
	while(!q.empty()){
		int u=q.top();
		q.pop();
		for(int i=head[u];i!=-1;i=edge[i].next){
			--indegree[edge[i].to];
			if(!indegree[edge[i].to]){
				q.push(edge[i].to);
				++sum;
			}
		}
	}
	if(sum==n) printf("YES\n");
	else printf("NO\n");
}
void init(){
	num_edge=0;
	sum=0;
	memset(head,-1,sizeof head);
	memset(indegree,0,sizeof indegree);
}
int main(){
	while(scanf("%d%d",&n,&m)&&n){
		init();
		for(int i=0;i<m;i++){
			int u,v;
			scanf("%d%d",&u,&v);
			indegree[v]++;
			add_edge(u,v);
		}
		topo();
	}
}
相关标签: 拓扑排序 C++