1014 Circles of Friends (35 分)

A circle of friends is a network of friend relationships. If A is a friend of B, then B is considered a friend of A no matter B admits or not, and they are said to belong to the same circle. Here we assume that friendship is transitive, that is, if A is a friend of B, and B is a friend of C, then A is a friend of C and the three of them all belong to the same circle.

On the other hand, A is not so close to C as B is. We define the distance D(X, Y) between two friends X and Y as the minimum number of friends between them. For example, D(A, B) = 0, and D(C, A) = 1. The diameter of a friends circle is the maximum distance between any pair of friends in the circle.

Now given some people’s relationships, you are supposed to find the number of friends circles and the circle with the largest diameter.

Input Specification:
Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤1000), which is the total number of people involved, and hence they are numbered from 1 to N. Then N lines follow, each in the format:

k p
​1
​​ … p
​k
​​

where k (0≤k<min(10,N)) is the number of friends and p
​1
​​ to p
​k
​​ (if k>0) are the friends’ indices. The i-th line corresponds to the i-th person. All the numbers in a line are separated by spaces. It is guaranteed that no one is given as a friend of oneself.

Output Specification:
For each case, print in a line the number of friends circles, and the largest diameter, separated by exactly one space.

Sample Input:
17
2 15 12
1 17
2 16 9
1 8
4 10 13 15 14
0
2 11 14
1 4
2 2 3
2 13 11
2 15 7
2 1 14
2 5 15
0
0
1 3
1 2
Sample Output:
4 3

题意分析,给定一张图,输出图中连通分量的个数,输出所有连通块中任意两个顶点的最大距离的最大值

题意还是很清楚的,建图然后bfs,从入度最小(边最少)的顶点开始搜索,这样搜到最后就是这个连通块内的最大距离,比较坑的地方是,两个人的距离是从0开始的,还有别被这个时间限制吓到了,限制1000ms,实际跑出来每个都没超过10ms

说明一点:单独只在一个无向图内求任意两点的最大距离,仅仅需要两次bfs,具体做法是,任选一点进行bfs,同时记录距离最远的点,第二次从这个最远的点进行bfs,记录下最远距离就是整个图任意两点的最远距离,但是对于这个题而言,有可能会有很多联通块,处理起来可能不是很方便,所以就直接暴力搜索所有入度最小的点了。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n, m, f;
	cin >> n;
	vector<vector<int>>edge(n + 1);
	for (int i = 1; i <= n; i++){
		cin >> m;
		for (int j = 0; j < m; j++){
			cin >> f;
			edge[f].push_back(i);
			edge[i].push_back(f);
		}
	}
	vector<int>visited(n + 1), dist(n + 1);
	int ret = 0;
	while (1){
		int v = -1, min_ = INT_MAX;
		for (int i = 1; i <= n; i++)
			if (edge[i].size() < min_ && !visited[i]){
				min_ = edge[i].size();
				v = i;
			}
		if (v == -1)
			break;
		ret++;
		queue<int>que;
		que.push(v);
		visited[v] = true;
		int k = 0;
		while (que.size()){
			int num = que.size();
			while (num--){
				auto top = que.front();
				que.pop();
				for (auto& it:edge[top])
					if ( !visited[it]){
						visited[it] = true;
						que.push(it);
					}
			}
			k++;
		}
		dist[v] = k-1;
	}
	int a = *max_element(dist.begin(), dist.end());
	cout << ret << " "<<(a==0?0:a-1);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值