uva 10044 - Erdos Numbers

题目就是找出和 erdos这个人合著书的关系。 第一合作是1 。 然后和第一合作的作者有关系的是2.。。

 

第一次用stl写了写。。虽然写的蛮恶心的

题目:

Problem F: Erdös Numbers 

 

Background 

The Hungarian Paul Erdös (1913-1996, speak as ``Ar-dish'') not only was one of the strangest mathematicians of the 20th century, he was also one of the most famous. He kept on publishing widely circulated papers up to a very high age and every mathematician having the honor of being a co-author to Erdös is well respected.

Not everybody got the chance to co-author a paper with Erdös, so many people were content if they managed to publish a paper with somebody who had published a scientific paper with Erdös. This gave rise to the so-called Erdös numbers. An author who has jointly published with Erdös had Erdös number 1. An author who had not published with Erdös but with somebody with Erdös number 1 obtained Erdös number 2, and so on.

 

Problem 

Today, nearly everybody wants to know which Erdös number he or she has. Your task is to write a program which computes Erdös numbers for a given set of scientists.

 

Input 

The first line of the input contains the number of scenarios.

The input for each scenario consists of a paper database and a list of names. It begins with the line

P N

where P and N are natural numbers. Following this line are P lines containing descriptions of papers (this is the paper database). A paper appears on a line by itself and is specified in the following way:

Smith, M.N., Martin, G., Erdos, P.: Newtonian forms of prime factors matrices

Note that umlauts like `ö' are simply written as `o'. After the P papers follow N lines with names. Such a name line has the following format:

Martin, G.

 

Output 

For every scenario you are to print a line containing a string ``Scenario i" (where i is the number of the scenario) and the author names together with their Erdös number of all authors in the list of names. The authors should appear in the same order as they appear in the list of names. The Erdös number is based on the papers in the paper database of this scenario. Authors which do not have any relation to Erdös via the papers in the database have Erdös number ``infinity".

 

Sample Input 

1
4 3
Smith, M.N., Martin, G., Erdos, P.: Newtonian forms of prime factor matrices 
Erdos, P., Reisig, W.: Stuttering in petri nets
Smith, M.N., Chen, X.: First oder derivates in structured programming
Jablonski, T., Hsueh, Z.: Selfstabilizing data structures
Smith, M.N.
Hsueh, Z.
Chen, X.

 

Sample Output 

Scenario 1
Smith, M.N. 1
Hsueh, Z. infinity
Chen, X. 2

 

代码

  1 #include <iostream>
  2 #include <map>
  3 #include <memory.h>
  4 using namespace std;
  5 
  6 const int maxn = 10000;
  7 map<string,int> name;
  8 string Erdos = "Erdos, P.";
  9 int relation[maxn][maxn];
 10 int mark[maxn];
 11 int pr=0;
 12 int people=0;
 13 
 14 void build(string paper)
 15 {
 16     string tmp;
 17     int org=0;
 18     int rec[maxn]={0};
 19     int prec=0;
 20     for(int i=0;i<paper.size();i++)
 21     {
 22         if(paper[i]==':')break;
 23         if(paper[i]=='.'&&paper[i+1]==',' || paper[i]=='.'&&paper[i+1]==':')
 24         {
 25             tmp = paper.substr(org,i-org+1);
 26             org = i+3;
 27             if(name.find(tmp)==name.end())
 28             {
 29                 name.insert(make_pair(tmp,pr++));
 30                 rec[prec++]=pr-1;
 31                 people++;
 32             }
 33             else
 34             {
 35                 int num = name.find(tmp)->second;
 36                 rec[prec++]=num;
 37             }
 38         }
 39     }
 40     for(int i=0;i<prec;i++)
 41     {
 42         for(int j=0;j<prec;j++)
 43         {
 44             if(rec[i]==rec[j])continue;
 45             else
 46                 relation[rec[i]][rec[j]]=relation[rec[j]][rec[i]]=1;
 47         }
 48     }
 49 
 50     return ;
 51 }
 52 
 53 void bfs()
 54 {
 55     int quee[maxn]={0};
 56     int fr=0,ed=1;
 57     quee[fr] = name.find(Erdos)->second;
 58     mark[quee[fr]]=0;
 59     int vis[maxn]={0};
 60     while(fr<ed)
 61     {
 62         for(int i=0;i<people;i++)
 63         {
 64             if(quee[fr]==i)continue;
 65             if(relation[quee[fr]][i]==1&&vis[i]==0)
 66             {
 67                 mark[i]=mark[quee[fr]]+1;
 68                 quee[ed++] = i;
 69                 vis[i]=1;
 70             }
 71         }
 72         fr++;
 73     }
 74 }
 75 int main()
 76 {
 77     int tst,book,query;
 78     cin>>tst;
 79     int test=1;
 80     while(tst--)
 81     {
 82         cout<<"Scenario "<<test++<<endl;
 83         cin>>book>>query;
 84         cin.ignore();
 85         for(int i=0;i<book;i++)
 86         {
 87             string paper;
 88             getline(cin,paper);
 89             build(paper);
 90         }
 91         bfs();
 92         for(int i=0;i<query;i++)
 93         {
 94             string que;
 95             getline(cin,que);
 96             if(name.find(que)==name.end())
 97             {
 98                 cout<<que<<" infinity"<<endl;
 99                 continue;
100             }
101             int qq = name.find(que)->second;
102             if(mark[qq]==0)
103                 cout<<que<<" infinity"<<endl;
104             else
105                 cout<<que<<" "<<mark[qq]<<endl;
106         }
107         name.clear();
108         for(int i=0;i<people;i++)
109         {
110             for(int j=0;j<people;j++)
111             {
112                 relation[i][j]=0;
113             }
114             mark[i]=0;
115         }
116         pr=0;
117         people=0;
118     }
119 }

 

转载于:https://www.cnblogs.com/doubleshik/p/3439687.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值