#include <vector> #include <string> #include <sstream> #include <stdio.h> /* printf */ #include <stdlib.h> /* abs */ #include <cmath> #include <map> #include <queue> #include <functional> #include <sstream> #include <vector> #include<algorithm> #include<iostream> using namespace std; map<string,int>ErdosNumbers; vector<string> extract_names(string &line) { vector<string>list; string::size_type begin(0); string::size_type end=line.find(".,",begin); while(end!=string::npos) { list.push_back(line.substr(begin,end-begin+1)); begin=end+3; end=line.find(".,",begin); } if(begin<line.size()-1) list.push_back(line.substr(begin)); return list; } int main() { //freopen("c:\\temp\\in.txt","r",stdin); int scenario; scanf("%d",&scenario); for(int sc=1;sc<=scenario;sc++) { int p,n; scanf("%d %d",&p,&n); cin.ignore(); vector<string>authors; map<string,vector<string> >CoAuthors; for(int i=0;i<p;i++) { string data,names; getline(cin,data); stringstream ss(data); getline(ss,names,':'); authors=extract_names(names); for(int i=0;i<authors.size();i++) ErdosNumbers[authors[i]]=-1; for(int i=0;i<authors.size();i++) { for(int j=0;j<authors.size();j++) { if(authors[i]!=authors[j]) { if(find(CoAuthors[authors[i]].begin(),CoAuthors[authors[i]].end(),authors[j]) == CoAuthors[authors[i]].end()) CoAuthors[authors[i]].push_back(authors[j]); } } } } vector<string>find_name; string name; for(int i=0;i<n;i++) { getline(cin,name); find_name.push_back(name); } ErdosNumbers["Erdos, P."]=0; for(int i=0;i<CoAuthors["Erdos, P."].size();i++) ErdosNumbers[CoAuthors["Erdos, P."][i]]=1; vector<string>pre_l; pre_l=CoAuthors["Erdos, P."]; while(!pre_l.empty()) { vector<string>new_l; for(int i=0;i<pre_l.size();i++) { for(int j=0;j<CoAuthors[pre_l[i]].size();j++) { if(ErdosNumbers[CoAuthors[pre_l[i]][j]]==-1) { ErdosNumbers[CoAuthors[pre_l[i]][j]]=1+ErdosNumbers[pre_l[i]]; new_l.push_back(CoAuthors[pre_l[i]][j]); } } } pre_l=new_l; } // for(map<string,int>::iterator it=ErdosNumbers.begin();it!=ErdosNumbers.end();it++) // { // cout<<it->first<<" "<<it->second<<endl; // } printf("Scenario %d\n",sc); for(int i=0;i<n;i++) { cout<<find_name[i]<<" "; map<string,int>::iterator it=ErdosNumbers.find(find_name[i]); if(it==ErdosNumbers.end() || it->second==-1) printf("infinity\n"); else cout<<it->second<<endl; } } return 0; }
রবিবার, ৫ জানুয়ারী, ২০১৪
solution code of uva 10044
Labels:
uva solution
এতে সদস্যতা:
মন্তব্যগুলি পোস্ট করুন (Atom)
0 comments:
একটি মন্তব্য পোস্ট করুন