HDU

来源:互联网 发布:长沙编程招聘信息 编辑:程序博客网 时间:2024/06/05 02:06

Friend Chains

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


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
3XXXYYYZZZ2XXX YYYYYY ZZZ0
 

Sample Output
2
 


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4460

题意:有n个人,m条他们之间的关系,求所有两人之间的最近距离中的最远距离,距离就是两人之间隔了多少个人(由多少个人联系起来)+ 1。

把对应的名字用数字代替了,然后求出1到其余各点的最短距离,找出其中最大的,再求2道其余各点最短距离,找出其中最大的,然后再3,4,......把n个点都遍历一遍,在n个找出的最大的值中再找出其中最大的值,其为答案。
求最短路可以用bfs。

#include<iostream>#include<string.h>#include<stdio.h>#include<queue>#include<map>using namespace std;int n,m,mp[1010][10],cnt[1010],vis[1010],connect,dist[1010];map<string,int>a;string s1,s2;struct node{    int num;    int step;    node(int a,int b)    {        num = a;        step = b;    }    };void bfs(int pos){    vis[pos] = 1;    queue<node>q;    q.push(node(pos,0));    while(!q.empty())    {        node st = q.front();        q.pop();        for(int i=0;i<cnt[st.num];i++)        {            if(vis[mp[st.num][i]])                continue;            connect++;            vis[mp[st.num][i]] = 1;            dist[st.num] = st.step+1;            q.push(node(mp[st.num][i],st.step+1));        }    }}int main(){    while(scanf("%d",&n),n)    {        a.clear();        memset(cnt,0,sizeof cnt);        memset(mp,0,sizeof mp);        for(int i=0;i<n;i++)        {            cin>>s1;            a[s1] = i;        }        scanf("%d",&m);        while(m--)        {            cin>>s1>>s2;            int aa = a[s1],bb = a[s2];            mp[aa][cnt[aa]++] = bb;            mp[bb][cnt[bb]++] = aa;        }        int mx = 0;        for(int i=0;i<n-1;i++)        {            memset(vis,0,sizeof vis);            memset(dist,0,sizeof dist);            connect = 0;            bfs(i);            if(connect!=n-1)            {                mx = -1;                break;            }            for(int i=0;i<n;i++)                mx = max(dist[i],mx);        }        printf("%d\n",mx);    }    return 0;}


原创粉丝点击