//MiYu原創(chuàng), 轉(zhuǎn)帖請注明 : 轉(zhuǎn)載自 ______________白白の屋題目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=2094題目描述:
Problem Description
有一群人,打乒乓球比賽,兩兩捉對撕殺,每兩個人之間最多打一場比賽。
球賽的規(guī)則如下:
如果A打敗了B,B又打敗了C,而A與C之間沒有進(jìn)行過比賽,那么就認(rèn)定,A一定能打敗C。
如果A打敗了B,B又打敗了C,而且,C又打敗了A,那么A、B、C三者都不可能成為冠軍。
根據(jù)這個規(guī)則,無需循環(huán)較量,或許就能確定冠軍。你的任務(wù)就是面對一群比賽選手,在經(jīng)過了若干場撕殺之后,確定是否已經(jīng)實際上產(chǎn)生了冠軍。
Input
輸入含有一些選手群,每群選手都以一個整數(shù)n(n<1000)開頭,后跟n對選手的比賽結(jié)果,比賽結(jié)果以一對選手名字(中間隔一空格)表示,前者戰(zhàn)勝后者。如果n為0,則表示輸入結(jié)束。
Output
對于每個選手群,若你判斷出產(chǎn)生了冠軍,則在一行中輸出“Yes”,否則在一行中輸出“No”。
Sample Input
3
Alice Bob
Smith John
Alice Smith
5
a c
c d
d e
b e
a d
0
Sample Output
Yes
No
一道很明顯的數(shù)據(jù)結(jié)構(gòu)題, 用拓?fù)渑判蚪鉀Q.
當(dāng) 輸入 A 戰(zhàn)勝 B 時, 讓 B 指向 A, 表示B曾被打敗過.
最后指向空的就表示沒有人戰(zhàn)勝過他, 如果這樣的人僅
存在一個,那么明顯,最后的冠軍就是他了. 這里我用到了
C++ STL 的map set 用來實現(xiàn) B->A 的映射關(guān)系.
代碼如下 :
//MiYu原創(chuàng), 轉(zhuǎn)帖請注明 : 轉(zhuǎn)載自 ______________白白の屋
#include <iostream>
#include <set>
#include <map>
#include <string>
using namespace std;
map <string, string> mp;
set <string> st;
int main ( )
{
int N;
while ( cin >> N, N )
{
st.clear();
mp.clear();
string s1,s2;
for ( int i = 0; i != N; ++ i )
{
cin >> s1 >> s2;
st.insert ( s1 );
st.insert ( s2 );
mp[ s2 ] = s1;
}
set <string>::iterator beg = st.begin();
int nCount = 0;
for ( ; beg != st.end (); ++ beg )
{
if ( !mp[ *beg ].length () )
{
++ nCount;
}
}
puts ( nCount == 1 ? "Yes" : "No" );
}
return 0;
}