HDU4460 Friend Chains

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

题目:
给一些人,这些人要每两个人都能连起来,可能有一些连不起来,友谊链的长度为一个人到另一个人的距离。问 在所有的链的长度中最大的一条多长。每一条链要尽量短。
样例中 xxx与zzz没有连起来,xxx通过yyy与zzz相连,长度为2,就相当于xxx到zzz的最短距离,这样的话用bfs记录最短路就可以了,然后搜一遍找最大的那个。
要用vector存点

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
using namespace std;
char s[1005][10];
map<string, int> M;//将每个名字对应成数字
int vis[1005];//是否用过
vector <int > v[1005];
int dis[1005][1005];//这里存每个点到其他所有点的距离
int bfs(int u)
{
    int b;
    queue<int > q;
    q.push(u);
    vis[u] = 1;
    dis[u][u] = 0;//到自己的距离设为0
    while(!q.empty())
    {
        b = q.front(), q.pop();
        int len = v[b].size();
        for(int i = 0; i < len; i++)
        {
            int a = v[b][i];
            if(vis[a] == 0)
            {
                dis[u][a] = dis[u][b]+1;//这一层的友谊链长度等于上一层的加一
                vis[a] = 1;//标记为看过,防止出现环
                q.push(a);
            }
        }
    }
}
int main()
{
    int n, m, ans;
    while(scanf("%d", &n) != EOF)
    {
        if(n == 0)
            break;
        for(int i = 0; i < n; i++)
        {
            scanf("%s", s[i]);
            M[s[i]] = i;//映射
        }
        scanf("%d", &m);
        for(int i = 0; i < n; i++)
            v[i].clear();//不要忘了清空
        for(int i = 0; i < m; i++)
        {
            char s1[12], s2[12];
            scanf("%s%s", s1, s2);
            int t1 = M[s1];
            int t2 = M[s2];
            v[t1].push_back(t2);//用vector
            v[t2].push_back(t1);
        }
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                dis[i][j] = 100000;
        //将所有点设为一个很大的数,如果某个点和谁都不是朋友的话,友谊链,长度无限大,输出-1;
        for(int i = 0; i < n; i++)
        {
            memset(vis, 0, sizeof vis);
            bfs(i);
        }
        ans = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                ans = max(ans, dis[i][j]);
            }
        }
        if(ans == 100000)
            ans = -1;
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值