南邮 OJ 1119 仙林鼎山游乐园

仙林鼎山游乐园

时间限制(普通/Java) :  1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
总提交 : 390            测试通过 : 112 

比赛描述

端午节到来,sed投资建设的仙林鼎山游乐园开业了。整个园区拥有许多游乐场,有多个入口和多个出口,游乐场之间铺设小路相连。端午节来游乐园的人实在太多,sed发布规定:游乐场之间的小路为单行道。但游乐场太多,工作人员常常将指示方向的标志牌放错方向,导致游客在游乐园又回到游览过的地方。请sed解决这个问题,按照现有的标志牌指示的方向,判断游客在一次行程中是否有可能回到游览过的地方。



输入

第一行是一个正整数:测试用例数目,最多为100。之后,每个测试用例包括:

l       第1行给出两个整数n、e,2≤n≤50,1≤e≤1225,n表示游乐场的数目,游乐场分别用,e表示所有游乐场之间路径数。

l       e行,每1行给出两个整数,分别表示两个游乐场的序号,给出了它们之间的单行道。

输出

对于每个测试用例:

l       输出YES或NO,表示按照现有的标志牌指示的方向,游客在一次行程中是否有可能回到游览过的地方。

样例输入

2
2 1
0 1
3 3
0 1
1 2
2 0

样例输出

NO
YES

题目来源

“IBM南邮杯”团队赛2009 【深紫】



//阶梯思想:拓扑排序。即每次去掉入度为0的节点。
//所有入度为0的节点都去除之后,仍然有顶点的话,说明图中有环路
#include<iostream>
#include<list>
#include<vector>
#include<queue>
#define MAX_N 50
using namespace std;

class directedGraphVertex{
public:
	int n;
	int inDegree;
	list<int> adjacent;
};

int main(){
	int N,n,e,i,j,k,vertexCount;
	queue<int> que;
	list<int>::iterator it;
	directedGraphVertex aVertex,graphVertexes[MAX_N];
	for(i=0;i<MAX_N;++i){
		graphVertexes[i].n = i;
	}
	cin>>N;
	while(N--){
		cin>>n>>e;
		vertexCount = 0;
		while(!que.empty()){
			que.pop();
		}
		for(i=0;i<n;++i){
			graphVertexes[i].adjacent.clear();
			graphVertexes[i].inDegree = 0;
		}
		for(k=0;k<e;++k){
			cin>>i>>j;
			graphVertexes[i].adjacent.push_back(j);
			++graphVertexes[j].inDegree;
		}
		for(i=0;i<n;++i){
			if(0==graphVertexes[i].inDegree){
				que.push(i);
			}
		}
		while(!que.empty()){
			i = que.front();
			que.pop();
			++vertexCount;
//			cout<<i<<endl;
			for(it = graphVertexes[i].adjacent.begin();it!=graphVertexes[i].adjacent.end();++it){
				if(0==--graphVertexes[*it].inDegree){
					que.push(*it);
				}
			}
		}
		if(vertexCount<n){
			cout<<"YES"<<endl;
		}else{
			cout<<"NO"<<endl;
		}
	}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值