仙林鼎山游乐园
时间限制(普通/Java) :
1000 MS/ 3000 MS 运行内存限制 : 65536 KByte
总提交 : 390 测试通过 : 112
总提交 : 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;
}
}
}