Chapter05-Erdos Numbers(POJ 1391)

来源:互联网 发布:mac myeclipse 破解 编辑:程序博客网 时间:2024/06/02 09:52

Erdos Numbers

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 1142

Accepted: 384

Description

The Hungarian Paul Erdos (1913-1996, pronounced as"Ar-dish") was not only one of the strangest mathematicians of the20th century, he was also among the most famous ones. He kept on publishingwidely circulated papers up to a very high age, and every mathematician havingthe 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 werecontent if they managed to publish a paper with somebody who had published apaper with Erdos. This gave rise to the so-called Erdos numbers. An author whohas jointly published with Erdos had Erdos number 1. An author who had notpublished with Erdos but with somebody with Erdos number 1 obtained Erdosnumber 2, and so on. Today, nearly everybody wants to know what Erdos number heor she has. Your task is to write a program that computes Erdos numbers for agiven set of scientists. 

Input

The input file contains a sequence of scenarios, eachscenario consisting of a paper database and a list of names. A scenario beginswith the line "p n", where p and n are natural numbers with1<=p<=32000;1<=n<=3000. Following this line are p lines containingdescriptions of papers (this is the paper database). A paper is described by aline of the following form: 

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

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

Example: 

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

After the p papers follow n lines each containing exactly one name in the sameformat 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 filecontains more than 250 characters. In each scenario there will be at most 10000 different authors. 

Output

For every scenariofirst print the number of the scenario in the format shown in the sampleoutput. Then print for every author name in the list of names their Erdosnumber based on the papers in the paper database of the scenario. The authorsshould be output in the order given in the input file. Authors that do not haveany relation to Erdos via the papers have Erdos number infinity. Adhere to theformat 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.

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

Hint

Huge input,scanfis recommended.

Source

Mid-CentralEuropean Regional Contest 2000

 

 

 

题目大意:

      有个叫Erodos, P.的科学家发表过很多论文,只要能与其共同发表论文那么可以说这位科学家的Erodos number为1,只要能与其合作过的科学家发表论文,那么其Erodos number为2;依次类推,如果没有任何关系的科学家可以认为为infinity;

      第一行输入p,n;p表示p行论文的描述,1=<p<=32000,n表示需要知道的作者的名称 1<=n<=3000;

 

分析

      如何用一个数据结构很好的存储这些相对比较乱的数据,是个问题,也就是说我们应该如何建立邻接表;

      显然邻接表应该存储作者间的相互合作的关系,如果作者i和作者j合作过,那么我们应该填写[i][j]=1;所为的论文其实就是为了建立他们之间的联系;

      因为作者的总数一开始不能完全确定,所以不妨用最大的值10000定义邻接矩阵;另外还有一件重要的事,那就是如果将作者名称与邻接矩阵的下标建立一一对应关系,所有需要用map来存储;

      构造好了邻接矩阵,剩下的事来一个bfs就可以了;

 

      做完这道题,最大的感触就是不会再爱了。。。感觉明明没有错,但是却总是RE,调试了半天还是有问题,后来在discuss里有人说测试数据是有问题的哈。。。因为存在着一组这样的数Shhdttgeelom, W.Y. : Partition No. 11这个数:是单独的,前后都有一个空格。。。瞬间崩溃了,改了之后终于对了。。。。不过效率一般

Java 代码如下:

import java.io.BufferedInputStream;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.LinkedList;import java.util.Scanner;public class Main {private HashMap<String, Integer> authorIndexMap;private ArrayList<Integer>[] authorRelationList;private int[] number;private static int authorindex;public Main() {authorIndexMap = new HashMap<String, Integer>(10000);authorRelationList = new ArrayList[10000];}public static void main(String[] args) {// TODO Auto-generated method stubScanner cin = new Scanner(new BufferedInputStream(System.in));int p = cin.nextInt();int n = cin.nextInt();String strFirstName;String strLastName;String strFullName;Main ma;int count = 0; // 用来统计输入的次数。。。。。。while (!(p == 0 && n == 0)) {count++;ma = new Main();authorindex = 0;char flagchar;ArrayList<String> al;for (int i = 0; i < p; i++) {al = new ArrayList<String>();while (true) {strFirstName = cin.next();strLastName = cin.next();if (strFirstName.charAt(strFirstName.length() - 1) == ':') {cin.nextLine();ma.createRelation(al);break;}flagchar = strLastName.charAt(strLastName.length() - 1);strLastName = strLastName.substring(0,strLastName.length() - 1);strFullName = strFirstName + " " + strLastName;al.add(strFullName);if (flagchar == ':') {cin.nextLine();ma.createRelation(al);break;}}}if (!ma.authorIndexMap.containsKey("Erdos, P.")) {ma.authorIndexMap.put("Erdos, P.", authorindex);authorindex++;}// 好的,建立好邻接矩阵了,那么可以开始算出所有人的number了;ma.setEdorsNumbers();System.out.println("Database #" + count);int tempindex;for (int i = 0; i < n; i++) {strFullName = cin.nextLine();if (!ma.authorIndexMap.containsKey(strFullName)) {System.out.println(strFullName + ": " + "infinity");continue;}tempindex = ma.number[ma.authorIndexMap.get(strFullName)];if (tempindex == -1) {System.out.println(strFullName + ": " + "infinity");} else {System.out.println(strFullName + ": " + tempindex);}}System.out.println();p = cin.nextInt();n = cin.nextInt();}}private void setEdorsNumbers() {// TODO Auto-generated method stubnumber = new int[authorindex];Arrays.fill(number, -1);LinkedList<Integer> al = new LinkedList<Integer>();int tempindex;tempindex = authorIndexMap.get("Erdos, P.");al.add(tempindex);number[tempindex] = 0;if (authorRelationList[tempindex] == null) {return;}int vert;while (al.size() > 0) {vert = al.remove();for (int i = 0; i < authorRelationList[vert].size(); i++) {tempindex = authorRelationList[vert].get(i);if (number[tempindex] == -1) {number[tempindex] = number[vert] + 1;al.add(tempindex);}}}}private void createRelation(ArrayList<String> al) {// TODO Auto-generated method stubfor (int i = 0; i < al.size(); i++) {if (authorIndexMap.containsKey(al.get(i))) {continue;}authorIndexMap.put(al.get(i), authorindex);authorindex++;}int indexi;int indexj;for (int i = 0; i < al.size()-1; i++) {for (int j =i+1; j < al.size(); j++) {if (i == j) {continue;}indexi = authorIndexMap.get(al.get(i));indexj = authorIndexMap.get(al.get(j));if (authorRelationList[indexi] == null) {authorRelationList[indexi] = new ArrayList<Integer>();}if (authorRelationList[indexj] == null) {authorRelationList[indexj] = new ArrayList<Integer>();}authorRelationList[indexi].add(indexj);authorRelationList[indexj].add(indexi);}}}}





0 0
原创粉丝点击