HDU4460 LA6378 Friend Chains【BFS】

Friend Chains

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

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

Source
2012 Asia Hangzhou Regional Contest

Problem link : HDU4460 LA6378 Friend Chains
problems outlined :( slightly)
Analysis :
    this question can be used to solve BFS, SPFA algorithm can also be used to solve.
    .
Program Description :
    code appears in UVALive6378 WA.
Reference links :( slightly)
Inscription :( omitted)

AC C ++ language program is as follows:

/* HDU4460 LA6378 Friend Chains */

#include <bits/stdc++.h>

using namespace std;

const int N = 1000 + 1;
vector<int> g[N];
int vis[N], vis2[N], d[N];
int n, m;

int bfs(int root)
{
    memset(vis, 0, sizeof(vis));
    memset(d, -1, sizeof(d));

    vis[root] = vis2[root] = 1;
    d[root] = 0;
    int maxd = 0;
    int ans = root;

    queue<int> q;
    q.push(root);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(int i = 0; i < (int)g[u].size(); i++) {
            int v = g[u][i];
            if(vis[v]) continue;
            vis[v] = vis2[v] = 1;
            d[v] = d[u] + 1;
            if(maxd < d[v]){
                maxd = d[v];
                ans = v;
            }
            q.push(v);
        }
    }
    return ans;
}

int solve(int n) {
    int u = bfs(n);
    int v = bfs(u);
    return d[v];
}

int main()
{
    while(scanf("%d", &n) == 1 && n) {
        map<string, int> mp;
        string name, name1, name2;
        for(int i = 1; i <= n; i++) {
            cin >> name;
            mp[name] = i;
            g[i].clear();
        }

        scanf("%d", &m);
        for(int i = 0; i < m; i++) {
            cin >> name1 >> name2;
            int u = mp[name1];
            int v = mp[name2];
            g[u].push_back(v);
            g[v].push_back(u);
        }

        if(m + 1 < n) {  printf("-1\n"); continue; }
        memset(vis2, 0, sizeof(vis2));
        int ans = 0;
        for(int i = 1; i <= n; i++)
            if(!vis2[i]) ans = max(ans, solve(i));
        for(int i = 1; i <= n; i++)
            if(d[i] == -1) {  ans = -1;  break; }

        printf("%d\n", ans);
    }
    return 0;
}
Released 2126 original articles · won praise 2306 · Views 2.54 million +

Guess you like

Origin blog.csdn.net/tigerisland45/article/details/104488119