2016年8月15日 星期一

北二區101-4王者之路(C++版)


參考程式碼 :
#include <iostream>
#include <cstring>
#include <map>

using namespace std;
const int MaxN = 20; //最多 20 個
map<string , int> mp;  //姓名,編號 :編號為 1 ~ n
int mpcnt;
int los[MaxN+1]; 
string nam[MaxN+1];
int main(void)
{
   int t , n , r , i;
   string a,b;
   cin >> t;
   while( t-- )
   {
      mpcnt = 0;  mp.clear();
      cin >> n >> r;
      for(i=1; i<=n; ++i) los[i] = 0;
      for(i=0; i<r; ++i)
      {
 cin >> a >> b;
 if( mp.find(a) == mp.end() ) mp[a] = ++mpcnt; // 沒出現過,給編號,加入
 int ai = mp[a]; nam[ai] = a;
 if( mp.find(b) == mp.end() ) mp[b] = ++mpcnt; // 沒出現過,給編號,加入
 int bi = mp[b]; nam[bi] = b;
 los[bi] = 1;  //輸過一場就是 1
      }
   // 因為 王者 會贏其他所有的人,不會輸, 印出唯一沒輸過的
      for(i=1; i<=n; ++i) if( los[i] == 0 ) cout << nam[i] << endl;
   }
   return 0;
}
/* 範例輸入
3
3 3
tpa azf
tpa abc
azf abc
5 11
tpa azf
azf abc
tpa abc
abc xyz
tpa abcde
xyz abc
azf abcde
azf xyz
abc azf
tpa xyz
azf abcde
5 12
mmm bbb
bbb ccc
mmm ccc
ddd mmm
mmm eee
ddd ccc
ddd bbb
bbb mmm
ddd eee
bbb eee
mmm bbb
bbb ccc
--------範例輸出
tpa
tpa
ddd

*/

0 意見:

張貼留言