poj 2491

用map比较容易 ,但是要注意没一个case都需要清空一下map 嗯这里贡献了一次Wa
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
    int test;
    int n;
    cin>>test;
    string str[400];
    string str1,str2;
    int s;
    int cas=0;
    while(test--)
    {
         map<string,string>map1;
         map1.clear();
         map<string,int>map2;
         map2.clear();
         cin>>n;
         s = n-1;
         while(s--)
         {
                   cin>>str1>>str2;
                   map1[str1] = str2;
                   map2[str1]++;
                   map2[str2] = map2[str2]+2;
         }
         string index;
         for(map<string,int>::iterator it = map2.begin();it!=map2.end();it++)
         {
             if(it->second==1)
             index = it->first;
         }
         cout<<"Scenario #"<<++cas<<":"<<endl;
         while(map1[index].size())
         {
               cout<<index<<endl;
               index = map1[index];
         }
         cout<<index<<endl<<endl;       
    }
    
    system("pause");
    return 0;
}
posted @ 2011-06-16 22:35  dapanshe  阅读(152)  评论(0编辑  收藏  举报