HDOJ 题目4460 Friend Chains(树的最长链)

Friend Chains

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


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
 

Recommend
We have carefully selected several similar problems for you:   5551  5550  5549  5548  5547 
 找到每个节点到叶子节点的最长链就好,,INF开始开太大了,,wa的不能自理。。
ac代码
153896832015-11-05 11:33:32Accepted4460109MS2072K2105 BC++XY_

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
char str[15];
int k;
struct s
{
    int u,v,next;
}edge[20010<<1];
int head[1010],cnt,vis[1010],s,e,maxn,dis[1010],num[1010],n;
char str1[15],str2[15];
void add(int u,int v)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int res,flag;
void bfs(int u)
{
    int i;
    queue<int>q;
    memset(vis,0,sizeof(vis));
    vis[u]=1;
    for(i=0;i<=n;i++)
        dis[i]=INF;
    dis[u]=1;
    s=u;
    maxn=0;
    q.push(u);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(!vis[v])
            {
                vis[v]=1;
                int d=dis[u]+1;
               // if(d>=dis[v])
               //     continue;
                dis[v]=d;
                q.push(v);
                if(dis[v]>maxn)
                {
                    maxn=dis[v];
                    s=v;
                }
                if(dis[v]==maxn&&num[v]<num[s])
                    s=v;
            }
        }
    }
}
int main()
{
   // int n;
    while(scanf("%d",&n)!=EOF,n)
    {
        int i;
        k=0;
        map<string,int>mp;
        for(i=1;i<=n;i++)
        {
            scanf("%s",str);
            mp[str]=++k;
        }
        memset(head,-1,sizeof(head));
        cnt=0;
        int m;
        scanf("%d",&m);
        memset(num,0,sizeof(num));
        while(m--)
        {
            scanf("%s%s",str1,str2);
            int a=mp[str1];
            int b=mp[str2];
            add(a,b);
            add(b,a);
            num[a]++;
            num[b]++;
        }
        maxn=0;
        s=1;
        bfs(1);
        bfs(s);
        //printf("%d\n",INF);
        for(i=1;i<=n;i++)
            if(dis[i]==INF)
                maxn=0;
        printf("%d\n",maxn-1);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值