首页 > ACM题库 > HDU-杭电 > Hdu 1369 Erdos Numbers[解题报告] C++
2013
12-09

Hdu 1369 Erdos Numbers[解题报告] C++

Erdos Numbers

问题描述 :

The Hungarian Paul Erdos (1913�1996, 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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

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

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

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

#include <iostream>
#include <map>
#include <memory.h>
using namespace std;

const int maxn = 10000;
map<string,int> name;
string Erdos = "Erdos, P.";
int relation[maxn][maxn];
int mark[maxn];
int pr=0;
int people=0;

void build(string paper)
{
    string tmp;
    int org=0;
    int rec[maxn]={0};
    int prec=0;
    for(int i=0;i<paper.size();i++)
    {
        if(paper[i]==':')break;
        if(paper[i]=='.'&&paper[i+1]==',' || paper[i]=='.'&&paper[i+1]==':')
        {
            tmp = paper.substr(org,i-org+1);
            org = i+3;
            if(name.find(tmp)==name.end())
            {
                name.insert(make_pair(tmp,pr++));
                rec[prec++]=pr-1;
                people++;
            }
            else
            {
                int num = name.find(tmp)->second;
                rec[prec++]=num;
            }
        }
    }
    for(int i=0;i<prec;i++)
    {
        for(int j=0;j<prec;j++)
        {
            if(rec[i]==rec[j])continue;
            else
                relation[rec[i]][rec[j]]=relation[rec[j]][rec[i]]=1;
        }
    }

    return ;
}

void bfs()
{
    int quee[maxn]={0};
    int fr=0,ed=1;
    quee[fr] = name.find(Erdos)->second;
    mark[quee[fr]]=0;
    int vis[maxn]={0};
    while(fr<ed)
    {
        for(int i=0;i<people;i++)
        {
            if(quee[fr]==i)continue;
            if(relation[quee[fr]][i]==1&&vis[i]==0)
            {
                mark[i]=mark[quee[fr]]+1;
                quee[ed++] = i;
                vis[i]=1;
            }
        }
        fr++;
    }
}
int main()
{
    int tst,book,query;
    cin>>tst;
    int test=1;
    while(tst--)
    {
        cout<<"Scenario "<<test++<<endl;
        cin>>book>>query;
        cin.ignore();
        for(int i=0;i<book;i++)
        {
            string paper;
            getline(cin,paper);
            build(paper);
        }
        bfs();
        for(int i=0;i<query;i++)
        {
            string que;
            getline(cin,que);
            if(name.find(que)==name.end())
            {
                cout<<que<<" infinity"<<endl;
                continue;
            }
            int qq = name.find(que)->second;
            if(mark[qq]==0)
                cout<<que<<" infinity"<<endl;
            else
                cout<<que<<" "<<mark[qq]<<endl;
        }
        name.clear();
        for(int i=0;i<people;i++)
        {
            for(int j=0;j<people;j++)
            {
                relation[i][j]=0;
            }
            mark[i]=0;
        }
        pr=0;
        people=0;
    }
}

 


  1. 老实说,这种方法就是穷举,复杂度是2^n,之所以能够AC是应为题目的测试数据有问题,要么数据量很小,要么能够得到k == t,否则即使n = 30,也要很久才能得出结果,本人亲测

  2. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!