hdu4460之BFS

Friend Chains

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2882    Accepted Submission(s): 970


Problem Description
For a group of people, there is an idea that everyone is equals to or less than 6 steps away from any other person in the group, by way of introduction. So that a chain of "a friend of a friend" can be made to connect any 2 persons and it contains no more than 7 persons.
For example, if XXX is YYY’s friend and YYY is ZZZ’s friend, but XXX is not ZZZ's friend, then there is a friend chain of length 2 between XXX and ZZZ. The length of a friend chain is one less than the number of persons in the chain.
Note that if XXX is YYY’s friend, then YYY is XXX’s friend. Give the group of people and the friend relationship between them. You want to know the minimum value k, which for any two persons in the group, there is a friend chain connecting them and the chain's length is no more than k .
 

Input
There are multiple cases. 
For each case, there is an integer N (2<= N <= 1000) which represents the number of people in the group. 
Each of the next N lines contains a string which represents the name of one people. The string consists of alphabet letters and the length of it is no more than 10. 
Then there is a number M (0<= M <= 10000) which represents the number of friend relationships in the group. 
Each of the next M lines contains two names which are separated by a space ,and they are friends. 
Input ends with N = 0.
 

Output
For each case, print the minimum value k in one line. 
If the value of k is infinite, then print -1 instead.
 

Sample Input
   
   
   
   
3 XXX YYY ZZZ 2 XXX YYY YYY ZZZ 0
 

Sample Output
   
   
   
   
2
 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=1000+10;
int n,m,size,index,sum;
int head[MAX],dist[MAX];
int num[MAX];
map<string,int>mp;

struct Edge{
	int v,next;
	Edge(){}
	Edge(int V,int NEXT):v(V),next(NEXT){}
}edge[MAX*20];

void Init(int num){
	memset(head,-1,sizeof head);
	size=sum=0;
}

void InsertEdge(int u,int v){
	edge[size]=Edge(v,head[u]);
	head[u]=size++;
}

void BFS(int u){
	queue<int>q;
	q.push(u);
	for(int i=0;i<=n;++i)dist[i]=INF;
	dist[u]=1;
	while(!q.empty()){
		u=q.front();
		q.pop();
		for(int i=head[u];i != -1;i=edge[i].next){
			int v=edge[i].v,d=dist[u]+1;
			if(d>=dist[v])continue;
			dist[v]=d;
			q.push(v);
			if(dist[v]>sum)sum=dist[v],index=v;
			if(dist[v] == sum && num[v]<num[index])index=v;
		}
	}
}

int main(){
	char name[12],name2[12];
	int u,v;
	while(scanf("%d",&n),n){
		memset(num,0,sizeof num);
		mp.clear();
		Init(n);
		int k=0;
		for(int i=0;i<n;++i){
			scanf("%s",name);
			mp[name]=++k;
		}
		cin>>m;
		for(int i=0;i<m;++i){
			scanf("%s%s",name,name2);
			u=mp[name];
			v=mp[name2];
			InsertEdge(u,v);
			InsertEdge(v,u);
			++num[u];
			++num[v];
		}
		index=1;
		//for(int i=1;i<=n;++i)
		BFS(1);
		BFS(index);
		for(int i=1;i<=n;++i)if(dist[i] == INF)sum=0;
		cout<<sum-1<<endl;
	}
	return 0;
}



你可能感兴趣的:(hdu4460之BFS)