开始正常刷题度日了。第一天就悲催了。
产生冠军 |
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;
}
唉……还是好累,为什么呢……