ZOJ 1129 Erdos Numbers(bfs)

Erdos Numbers

Time Limit: 10 Seconds       Memory Limit: 32768 KB

The Hungarian Paul Erdos (1913�C1996, pronounced as ��Ar-dish��) was not only one of the strangest mathematicians of the 20th century, he was also among the most famous ones. 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 Erdos is well respected.

Not everybody got a chance to co-author a paper with Erdos, so many people were content if they managed to publish a paper with somebody who had published a paper with Erdos. This gave rise to the so-called Erdos numbers. An author who has jointly published with Erdos had Erdos number 1. An author who had not published with Erdos but with somebody with Erdos number 1 obtained Erdos number 2, and so on. Today, nearly everybody wants to know what Erdos number he or she has. Your task is to write a program that computes Erdos numbers for a given set of scientists.


Input

The input file contains a sequence of scenarios, each scenario consisting of a paper database and a list of names. A scenario begins with the line ��p n��, where p and n are natural numbers with 1<=p<=32000;1<=n<=3000. Following this line are p lines containing descriptions of papers (this is the paper database). A paper is described by a line of the following form:

LastName1, FirstName1, LastName2, Firstname2, . . . : TitleOfThePaper

The names and the title may contain any ASCII characters between 32 and 126 except commas and colons. There will always be exactly one space character following each comma. The first name may be abbreviated, but the same name will always be written in the same way. In particular, Erdos�� name is always written as ��Erdos, P.��. (Umlauts like ����o��,����a��,. . . are simply written as ��o��,��a��, . . . .)

Example:

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

After the p papers follow n lines each containing exactly one name in the same format as in the paper database.

The line ��0 0�� terminates the input.

No name will consist of more than 40 characters. No line in the input file contains more than 250 characters. In each scenario there will be at most 10 000 different authors.


Output

For every scenario first print the number of the scenario in the format shown in the sample output. Then print for every author name in the list of names their Erdos number based on the papers in the paper database of the scenario. The authors should be output in the order given in the input file. Authors that do not have any relation to Erdos via the papers have Erdos number infinity. Adhere to the format shown in the sample output.

Print a blank line after each case.


Sample Input

2 2
Smith, M.N., Martin, G., Erdos, P.: Newtonian forms of prime factors matrices.
Gardner, M., Martin, G.: Commuting Names
Smith, M.N.
Gardner, M.
0 0


Sample Output

Database #1
Smith, M.N.: 1
Gardner, M.: 2 


Source: Mid-Central European Regional Contest 2000


思路:先找出每篇论文的作者,将作者的关系构成邻接矩阵。接着bfs,Paul Erdos首先进入搜索队列,与其直接合作的作者进队列,并且Erdos Number值是1,然后搜索与Erdos Number值为1的作者直接合作的作者,直至搜索全部作者。


AC代码:

#include <bits/stdc++.h> 
using namespace std;

struct {
	int who;
	int eNum;
}Q[10000];
vector<int> data[10000];
map<string,int> name;
map<string,int>::iterator pos;
int author[10000];
int paper[300]; 

void bfs(string Erdos)
{
	memset(author,-1,sizeof(author));
	memset(Q,0,sizeof(Q));
	
	int head = 0;
	int tail = 1;
	Q[head].who = name[Erdos];
	Q[head].eNum = 0;
	author[name[Erdos]] = 0;
	while(head!=tail)
	{
		int nowAuthor = Q[head].who;
		int many = data[nowAuthor].size();
		for(int i = 0; i<many; i++)
			if(author[data[nowAuthor][i]]==-1)
			{
				author[data[nowAuthor][i]] = Q[head].eNum+1;
				Q[tail].who = data[nowAuthor][i];
				Q[tail++].eNum = Q[head].eNum+1;
			}	
		head++;
	}
}

int main()
{
	int p,n;
	bool title;
	int numAuthor, numPaper;
	int iCase = 0;
	char str[300];
	string fullName;
	while(scanf("%d%d",&p,&n) && (p||n)){
		for(int i = 0; i<10000; i++)
			data[i].clear();
		name.clear();
		numAuthor = 0;
		while(p--)
		{
			title = false;
			numPaper = 0;
			while(!title)
			{
				scanf("%s",str); 
				int len = strlen(str);
				str[len-1]=' ';
				fullName = str;
				scanf("%s",str);
				len = strlen(str);
				
				if(str[len-1]==':') title = true;
				//标题的前面可能没有冒号
				if(str[len-1]=='.') title = true;//最后字符不是逗号,是点 
				
				str[len-1]='\0';
				fullName += str;
				
				pos = name.find(fullName);
				//第一次读取的作者,予以编号 
				if(pos==name.end()) name[fullName] = numAuthor++;
				paper[numPaper++] = name[fullName];//保存本篇论文的作者编号 
				if(title) gets(str);//gets()能读取空格 
			}
			//本篇论文中的作者编号,将论文合作者相互加入 
			for(int i = 0; i<numPaper; i++)
				for(int j = 0; j<numPaper; j++)
					if(i!=j) data[paper[i]].push_back(paper[j]);
		}
		
		string Erdos = "Erdos P.";
		pos = name.find(Erdos);
		if(pos==name.end()) name[Erdos]=numAuthor++;//判断作者Erdos P.是否出现过,未出现加入 
		
		bfs(Erdos);
		
		printf("Database #%d\n",++iCase);
		while(n--)
		{
			scanf("%s",str);
			printf("%s ",str);
			int len = strlen(str);
			str[len-1]=' ';
			fullName = str;
			scanf("%s",str);
			printf("%s: ",str);
			fullName += str;
			pos = name.find(fullName);
			if(pos==name.end()) printf("infinity\n");
			else if(author[name[fullName]]==-1) printf("infinity\n");
			else printf("%d\n",author[name[fullName]]);
		}
		printf("\n");
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值