标签:
#include <stdio.h>
#include <map>
#include <string>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
#define MAX 1500
bool path[MAX][MAX];
bool num[MAX];
map<string, int> list;
map<int , string>list1;
map<string, int>::iterator iter;
map<int , string>::iterator iter1;
int flag;
struct data
{
string str;
int count;
data()
{
count = 0;
}
}peo[MAX];
int flag1;
void input(int n)
{
list.clear();
list1.clear();
flag = 0;
//flag1 = 0;
string str1, str2;
for(int i=0; i<n; i++)
{
cin >> str1 >> str2;
int go, to;
if( (iter = list.find(str1) )!= list.end() )
go = iter->second;
else
{
list[str1] = flag++;
list1[flag-1] = str1;
go = flag-1;
}
if( (iter = list.find(str2) )!= list.end() )
to = iter->second;
else
{
list[str2] = flag++;
list1[flag-1] = str2;
to = flag-1;
}
path[go][to] = path[to][go] = true;
}
}
void bfs(string str)
{
iter = list.find(str);
int start = iter->second;
num[start] = true;
queue<int>Q;
Q.push(start);
int tp = Q.front();
Q.pop();
for(int i=0; i<flag; i++)
{
if(path[tp][i])
{
if(!num[i])
{
Q.push(i);
num[i] = true;
}
}
}
while(!Q.empty() )
{
int tp = Q.front();
Q.pop();
for(int i=0; i<flag; i++)
{
if(path[tp][i])
{
if(path[i][start] == false && i!=start)
{
iter1 = list1.find(i);
bool sign = false;
for(int j=0; j<flag1; j++)
{
if(peo[j].str == iter1->second)
{
peo[j].count++;
sign = true;
break;
}
}
if(!sign)
{
peo[flag1].str = iter1->second;
peo[flag1].count=1;
flag1++;
}
}
}
}
}
}
bool cmp(data a, data b)
{
if(a.count != b.count) return a.count > b.count ? 1:0;
else
return a.str < b.str ? 1:0;
}
int main()
{
//freopen("read.txt", "r", stdin);
int T;
scanf("%d", &T);
int co = 1;
while(T--)
{
printf("Case %d:\n", co++);
int n, m;
memset(path, false, sizeof(path) );
scanf("%d%d", &n, &m);
input(n);
string str;
for(int i=0; i<m; i++)
{
flag1 = 0;
memset(num, false, sizeof(num) );
cin >> str;
bfs(str);
if(flag1 != 0)
{
sort(peo, peo+flag1, cmp);
int max = peo[0].count;
int tp = 0;
while(peo[tp].count == max)
{
cout << peo[tp].str << " ";
tp++;
if(tp >= flag1) break;
}
printf("\n");
}
else
printf("-\n");
}
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/sober-reflection/p/5bce098418def146106562ce0f9795a7.html