開始正常刷題度日了。第一天就悲催了。
產生冠軍 |
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 22 Accepted Submission(s): 17 |
Problem Description
有一羣人,打乒乓球比賽,兩兩捉對撕殺,每兩個人之間最多打一場比賽。
球賽的規則如下: 如果A打敗了B,B又打敗了C,而A與C之間沒有進行過比賽,那麼就認定,A一定能打敗C。 如果A打敗了B,B又打敗了C,而且,C又打敗了A,那麼A、B、C三者都不可能成爲冠軍。 根據這個規則,無需循環較量,或許就能確定冠軍。你的任務就是面對一羣比賽選手,在經過了若干場撕殺之後,確定是否已經實際上產生了冠軍。 |
Input
輸入含有一些選手羣,每羣選手都以一個整數n(n<1000)開頭,後跟n對選手的比賽結果,比賽結果以一對選手名字(中間隔一空格)表示,前者戰勝後者。如果n爲0,則表示輸入結束。
|
Output
對於每個選手羣,若你判斷出產生了冠軍,則在一行中輸出“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 |
Author
qianneng
|
亮點是,把題解黏上去也錯了- -。。。
標答:
#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;
}
思想是,當 輸入 A 戰勝 B 時, 讓 B 指向 A, 表示B曾被打敗過.最後指向空的就表示沒有人戰勝過他, 如果這樣的人僅
存在一個,那麼明顯,最後的冠軍就是他了.
但是我想的是,把輸過的都置-1,沒輸過的置1,最後數數幾個1就好。
結果wa了,想的太簡單了麼?
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
int n;
while(cin>>n,n)
{
string a,b;
map<string,int>m;
while(n--)
{
cin>>a>>b;
if(m.find(a)==m.end())
m[a]=1;
m[b]=-1;
}
int cou=0;
map<string,int>::iterator it;
for(it=m.begin();it!=m.end();it++)
if(it->second==1)
cou++;
if(cou==1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
唉……還是好累,爲什麼呢……