zoukankan      html  css  js  c++  java
  • hdu 4460 Friend Chains

    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 .

    InputThere 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.
    OutputFor 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   


    这题头铁交了发flyod,发现虽然1000的三次方可能会水过,但是多组样例铁定是tle了。
    然后用bfs写的时候,忘了是任意两点,结果又凉了好久,最后修修改改终于过了。
    附ac代码:
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cmath>
     4 #include <string>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <queue>
     8 #include <map>
     9 #include <vector>
    10 using namespace std;
    11 const int maxn = 1111;
    12 typedef long long ll;
    13 const int inf = 0x3f3f3f3f;
    14 int n,m;
    15 vector<int>vec[maxn];
    16 string st[maxn];
    17 int vis[maxn];
    18 void bfs(int j)
    19 {
    20     memset(vis,0,sizeof(vis));
    21     queue<int>q;
    22     q.push(j);vis[j]=1;
    23     while(!q.empty())
    24     {
    25         int u=q.front();q.pop();
    26         for(int i=0;i<vec[u].size();++i)
    27         {
    28           //  printf("%dvis%d
    ",vis[i],i);
    29             if(!vis[vec[u][i]])
    30             {
    31                q.push(vec[u][i]);
    32                vis[vec[u][i]]=vis[u]+1;
    33           //     printf("%d %di
    ",vis[i],i);
    34             }
    35         }
    36     }
    37     return ;
    38 }
    39 int main() {
    40     ios::sync_with_stdio(false);
    41     map<string,int>p;
    42     while(cin>>n,n)
    43     {
    44         string a,b;
    45         for(int i=0;i<n;++i)
    46         {
    47             cin>>a;
    48             p[a]=i;
    49         }
    50         cin>>m;
    51         for(int i=0;i<m;++i)
    52         {
    53             cin>>a>>b;
    54        //     printf("%d %dp
    ",p[a],p[b]);
    55             vec[p[a]].push_back(p[b]);
    56             vec[p[b]].push_back(p[a]);
    57         }
    58         int ans=0;
    59         for(int i=0;i<n;++i)
    60         {
    61             bfs(i);
    62             for(int j=0;j<n;++j)
    63             {
    64                 ans=max(ans,vis[j]);
    65             }
    66         }
    67 
    68 
    69         int j=0;
    70         for(j=0;j<n;++j)
    71         {
    72             ans=max(ans,vis[j]);
    73             if(vis[j]==0)
    74             {
    75        //        printf("%dj
    ",j);
    76                 break;
    77             }
    78         }
    79         if(j==n)    printf("%d
    ",ans-1);
    80         else printf("-1
    ");
    81         p.clear();
    82         for(int i=0;i<n;++i)
    83         vec[i].clear();
    84     }
    85     return 0;
    86 }
    View Code
  • 相关阅读:
    HDU- 1075 What Are You Talking About (Trie or map)
    HDU-1251 统计难题 (Trie)
    Trie树模板(插入和查询)
    HDU 5120 Intersection
    HDU 5025 Saving Tang Monk(状压bfs)
    Light OJ 1094 (树的直径)
    zzuli 1877 蛤玮打扫教室
    HDU 5642 King's Order(数位dp)
    POJ 2689 Prime Distance
    hdu 3853 LOOPS(概率dp)
  • 原文地址:https://www.cnblogs.com/zmin/p/8331571.html
Copyright © 2011-2022 走看看