2012杭州赛区(浙江理工大学)H - Friend Chains


H - Friend Chains
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
题意:给你n个人和他们的关系,问任意找一个人,他的最长的朋友链是多少,朋友链的人数不包括他自己。

思路:这道题其实解法很多,深搜广搜最短路都可以,我是选择了最短路来做这道题的,当时队友是应该是用了广搜做了,我是赛后用最短路一遍过了。其实就是暴力枚举所有点,然后最短路,找出最大的那个链数就可以了。还有一点就是,如果那个人的朋友关系里面没有包含所有人的话,那么输出-1,也就是说每个人都必须互相之间有一定的关系才输出最长的链数。问题化简到最短路上就是从某人为起点最短路一遍,然后如果有一个人没有遍历到就输出-1。下面给代码。

#include<iostream>
#include<stack>
#include<cstring>
#include<map>
#include<string>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<utility>
using namespace std;
#define maxn 1005
typedef long long LL;
int head[maxn], maxnum, n;
struct node {
	int x, next;
}p[maxn * 20];
void dikjistra(int x) {
	queue<int>q;
	int vis[maxn];
	memset(vis, -1, sizeof(vis));
	vis[x] = 0;
	q.push(x);
	while (!q.empty()) {
		int now = q.front();
		q.pop();
		for (int i = head[now]; i != -1; i = p[i].next) {
			if (vis[p[i].x] == -1) {
				vis[p[i].x] = vis[now] + 1;
				q.push(p[i].x);
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (vis[i]==-1) {
			maxnum = 2000;
			break;
		}
		maxnum = max(vis[i], maxnum);
	}
}
int main() {
	while (scanf("%d", &n)&&n) {
		char s[15];
		memset(head, -1, sizeof(head));
		map<string, int>mm;
		for (int i = 0; i < n; i++) {
			scanf("%s", s);
			mm[s] = i;
		}
		int m;
		maxnum = 0;
		scanf("%d", &m);
		for (int i = 0; i < 2 * m;) {//用邻接表建双向图
			char s1[15], s2[15];
			scanf("%s%s", s1, s2);
			int st = mm[s1], tg = mm[s2];
			p[i].x = tg;
			p[i].next = head[st];
			head[st] = i++;
			p[i].x = st;
			p[i].next = head[tg];
			head[tg] = i++;
		}
		for (int i = 0; i < n; i++) {
			dikjistra(i);
		}
		if (maxnum == 2000)
			printf("-1\n");
		else
			printf("%d\n", maxnum);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值