1003. Erdős Number

 
Time Limit: 1sec    Memory Limit:256MB
Description

The  Erdős  number describes the "collaborative distance" between a person and mathematician Paul Erdős.

To be assigned an Erdős number, an author must co-write a research paper with an author with a finite Erdős number. Paul Erdős has an Erdős number of zero. Anybody else's Erdős number is k + 1 where k is the lowest Erdős number of any coauthor. Erdős wrote around 1,500 mathematical articles in his lifetime, mostly co-written. He had 511 direct collaborators; these are the people with Erdős number 1. The people who have collaborated with them (but not with Erdős himself) have an Erdős number of 2, those who have collaborated with people who have an Erdős number of 2 (but not with Erdős or anyone with an Erdős number of 1) have an Erdős number of 3, and so forth. (from wikipedia.org).
 
 
Input

Assume that Paul Erdős is numbered 0, other actors are numbered by positive integers between 1 to 100.

The first line is the number of test cases. Each test case start with the number m of actors in a line, then m pairs of authors are followed.

Output

For each test case, each author's  Erdős number is printed out, if it is finite,  in a separate line in the form "author number: Erdősnumber" in the order of increasing actor number, and finally  a dotted line "---" is printed out in a separate line.

Sample Input
 Copy sample input to clipboard
2
1
0 1
3
0 4
0 2
2 3

 
Sample Output
1:1
---
2:1
3:2
4:1
---
题目的意思是输出到0这点的距离,利用广度优先搜索,结果是我在代码上的处理有些问题,标记顺序的问题,真是不应该这样写。
直接上代码吧
// Problem#: 8592
// Submission#: 2196678
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<queue>
#include<memory.h>
using namespace std;
queue<int>qu;
int id[105];
bool visit[105];
bool adj[105][105];
int countt;
void bfs()
{
  while(!qu.empty())
  {
    int tmp=qu.front();
   
    //visit[tmp]=true;这里不能标记,一直是WA,因为考虑到一个顺序的问题
    for(int i=0;i<105;i++)
    {
      if(adj[tmp][i]&&!visit[i])
      {
        visit[i]=true;//进入队列就标记为访问过
        id[i]=id[tmp]+1;
        qu.push(i);
      }
    }
   qu.pop();
  }  
}
int main()
{
  int casen;
  cin>>casen;

  while(casen--)
  {
    while(!qu.empty())
      qu.pop();
    memset(adj,false,sizeof(adj));
    memset(visit,false,sizeof(visit));
    int m;
    cin>>m;
    int temp=m;
    while(m--)
    {
       int a,b;
       cin>>a>>b;
      adj[a][b]=true;
      adj[b][a]=true;
    }
    memset(id,-1,sizeof(id));
    id[0]=0;
    visit[0]=1;
    qu.push(0);
    bfs();
   for (int i = 1; i < 105; ++i)
   {
     /* code */
       if(id[i]!=-1)
      cout<<i<<":"<<id[i]<<endl;
   }
    cout<<"---"<<endl;
    }
}                                 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值