1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <sstream>
using namespace std;
vector<string> split(string str,char delimiter){
vector<string> internal;
stringstream ss(str);
string tok;
while(getline(ss,tok,delimiter)){
internal.push_back(tok);
}
return internal;
}
int main(){
string cases,papers,names;
int p,n,c,cont=1;
getline(cin, cases);
c=atoi(cases.c_str());
while(c--){
getline(cin,papers,' ');
getline(cin,names);
p=atoi(papers.c_str());
n=atoi(names.c_str());
map<string, pair<bool,int> > mapName;
mapName["Erdos, P."]=make_pair(0,0);
for(int i=0;i<p;i++){
vector<string> temp,comp;
int menor=-1;
bool entrou=0;
string aux,lixo,nome,nomeMenor;
getline(cin, aux, ':');
getline(cin,lixo);
temp = split(aux,',');
for(int j=0;j<temp.size();j=j+2){
nome=(temp[j]+','+temp[j+1]);
if(j==0)
nome.erase(remove(nome.begin(), nome.end(), '\n'), nome.end());
if(nome[0]==' ')
nome.erase(nome.begin());
if(mapName.find(nome) == mapName.end()){
mapName[nome] = make_pair(0,-1);
}
comp.push_back(nome);
}
for(int j=0;j<comp.size();j++){
if(mapName[comp[j]].second!=-1){
if(entrou){
if(menor>mapName[comp[j]].second){
menor=mapName[comp[j]].second;
nomeMenor=comp[j];
}
}
else{
entrou=1;
menor=mapName[comp[j]].second;
nomeMenor=comp[j];
}
}
}
for(int j=0;j<comp.size();j++){
if(entrou){
if(comp[j]!=nomeMenor){
mapName[comp[j]].second=menor+1;
cout<<endl;
}
}
}
}
string nomeParaMostrar[n];
for(int i=0;i<n;i++){
getline(cin, nomeParaMostrar[i]);
mapName[nomeParaMostrar[i]].first=1;
}
map<string, pair<bool,int> >::iterator it;
cout<<"Scenario "<<cont<<endl;
for(int i=0;i<n;i++){
if(mapName[nomeParaMostrar[i]].second!=-1)
cout<<nomeParaMostrar[i]<<" "<<mapName[nomeParaMostrar[i]].second<<endl;
else
cout<<nomeParaMostrar[i]<<" infinity"<<endl;
}
cont++;
if(c>1)
cout<<endl;
}
return 0;
}
|