时间限制:1000ms
单点时限:1000ms
内存限制:512MB
描述
Jin Yong was the most famous and popular Chinese wuxia (The one who fight bad people by his Wukong i.e. Wushu and Kongfu) novelist who lived in Hong Kong. Between 1955 and 1972, he wrote 14 novels which earned him a reputation as one of the greatest and most popular Chinese writers. Over 100 million copies of his works have been sold worldwide,not including a countless number of pirated copies. Jin Yong’s works seem to have magic. Once you begin to read a novel of his, you just can’t stop until you finish it.
Last month, Jin Yong passed away at the age of 94. Many Jin Yong’s fans in PKU held a meeting to memorize him. Jin Yong’s fans always like to discuss or argue or even quarrel about whose Wukong are better among the wuxia characters of his novel. During the meeting, this happened again:
Every fans said some words like “Qiao Feng’s Wukong is better than Guo Jing’s”. Obviously, those words may contradict each other and then cause quarrels. As a boring and girlfriendless male programmer of EECS school, you always want to make some things. So you are eager to point out the contradictions as soon as possible. That means, you want to find out the first one whose words contradict the words said by others before him.
Please note that if A is better than B, and B is better than C, then of course A must be better than C.
输入
There are no more than 15 test cases.
For each test case:
The first line is an integer n( 1 <= n <=20), meaning that there are n sentences.
The following n lines are those n sentences which is in the format below:
s1 s2
This means someone said that s1’s Wukong was better than s2’s. Both s1 and s2 are names of Jin Yong’s characters which consists of only English letters. It’s guaranteed that s1 and s2 are different, and their length is no more than 30. Names are case sensitive.
输出
For each test case, print the first sentence which cause a contradiction. If there are no contradiction, print 0 instead.
提示
DON’T try to figure out who are those names in the sample and waste your time.
样例输入
2 BrokenReputation ExtinctNun HelloLaught EnvelopeNotFlat 6 LandOverWind LonelyLight FireMonk CutTheForest CutTheForest LookCrazy MakeFoxRush LetMeGo HeroAunt UniqueLand LookCrazy FireMonk
样例输出
0 LookCrazy FireMonk
解题思路:N很小,直接拓扑排序判环即可。
#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<bitset>
#include<set>
#define INF (0x3f3f3f3f)
using namespace std;
typedef long long int ll;
const int MAXN=10010;
map<string,int> mp;
int cnt=0;
vector<int> G[MAXN];
int degree[MAXN];
int deg[MAXN];
bool topo(){
queue<int> que;
for(int i=1;i<=cnt;i++){
deg[i]=degree[i];
}
int num=0;
for(int i=1;i<=cnt;i++){
if(deg[i]==0){
que.push(i);
num++;
}
}
while(!que.empty()){
int tp=que.front();
que.pop();
for(int i=0;i<G[tp].size();i++){
deg[G[tp][i]]--;
if(deg[G[tp][i]]==0){
que.push(G[tp][i]);
num++;
}
}
}
if(num==cnt)
return false;
return true;
}
int main(){
int N;
while(~scanf("%d",&N)){
cnt=0;
for(int i=1;i<=2*N;i++){
G[i].clear();
degree[i]=0;
}
mp.clear();
bool flag=0;
for(int i=0;i<N;i++){
string a,b;
cin>>a>>b;
if(mp[a]==0)
mp[a]=++cnt;
if(mp[b]==0)
mp[b]=++cnt;
G[mp[a]].push_back(mp[b]);
degree[mp[b]]++;
if(flag==0&&topo()){
cout<<a<<" "<<b<<endl;
flag=1;
}
}
if(flag==0)
cout<<0<<endl;
}
return 0;
}