【dfs】Friend Chains

Friend Chains

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

 

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

Sample Output

2

题意:寻找最长子链。

开始时,总是想用最短路,寻找每两个点之间最短路的最大值,用了Dijkstra,一直在t,后来才想到,这个题完全是bfs,dfs

used[]是用来标记已经跑过的点,vector <int> v[] 属于容器

首先将所有的点都设置为没有经过,即 false,经过一个点 i 时,将其压入队列中,在循环中,取队顶元素并出队,将与队顶相联系的点,依次加一,跑过的点,将used[]=true,并将该点压入队列中。

map<string,int> mp;是一种可以改变下标的类型。mp ["string"]=1;

#include <bits/stdc++.h>
#define INF 0x3f3f3f

using namespace std;
int dis[1010][1010];
bool used [1010];
vector <int> v [1010];
queue <int> q;

void dfs(int i){
	memset(used,false,sizeof(used));
	dis[0][0] = 0;
	used[i] = true;    //±ê¼ÇÒѾ­ÅܹýµÄµã 
	q.push(i);
	while(!q.empty()){
		int t = q.front();
		q.pop();
		int m = v[t].size();
		for(int j=0;j<m;j++){
			int vv = v[t][j];
			if(used[vv])
				continue;
			dis[i][vv] = dis[i][t] + 1;
			q.push(vv);
			used[vv] = true;	 
		}
	}
}

map<string,int> mp;

int main(){
	string str1,str2,str;
	int n,m;
	while(~scanf("%d",&n)){
		if(n == 0)
			break;
		mp.clear();
		for(int i=0;i<n;i++){
			cin >> str;
			mp[str] = i;
		}
		for(int i=0;i<n;i++){
			dis[i][i] = 0;
			for(int j=i+1;j<n;j++)
				dis[i][j] = dis[j][i] = INF;
		}
		scanf("%d",&m);
		for(int i=0;i<n;i++){
			v[i].clear();
		}
		while(m--){
			cin >> str1 >> str2;
			int t1 = mp[str1];
			int t2 = mp[str2];
			v[t1].push_back(t2);
			v[t2].push_back(t1);
		}
		for(int i=0;i<n;i++)
			dfs(i);
		int ans = 0;
		for(int i=0;i<n;i++){
			for(int j=i+1;j<n;j++)
				ans = max(ans,dis[i][j]);
		}
		if(ans == INF)
			ans = -1;
		printf("%d\n",ans);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
马尔科夫链(Markov chains)是一种数学模型,它描述了在给定过去状态的情况下,未来状态的概率分布只与当前状态有关,与过去状态无关。马尔科夫链的本质在于其具有马尔科夫性质,即无记忆性。 在马尔科夫链中,系统存在一组状态,每个状态之间存在概率转移的关系。通过转移概率矩阵(transition matrix),我们可以描述状态之间的概率转移。该矩阵的行数和列数等于系统状态的数量,矩阵中的元素表示从一个状态转移到另一个状态的概率。 通过马尔科夫链,我们可以研究随机过程中状态的演化。在随机游走、物理学、生态学、经济学等领域,马尔科夫链都有着广泛应用。例如,在随机游走中,马尔科夫链可以帮助我们分析某个随机漫步者在不同位置之间的概率转移;在经济学中,马尔科夫链可以用于描述不同经济周期之间的转移。 此外,马尔科夫链还有一些重要的性质和概念,如吸收概率、平稳分布、遍历性等。吸收概率是指从某个状态开始,最终进入某个特定状态的概率。平稳分布表示在长时间内,系统状态的概率分布保持不变。而遍历性用于描述从任意状态开始,最终可以访问到所有其他状态的性质。 总之,马尔科夫链是一种重要的数学模型,用于研究随机过程中状态的演化以及系统的稳定性。它的应用领域广泛,并且在概率论、统计学和计算机科学等领域中都有重要的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值