问题描述:
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 p1 ... pk
where k (0 <= k < min(10, N)) is the number of friends and p1 to pk (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 2Sample Output:
4 3
总觉得相似的题目好像在 advanced级 里写过,不过忘记是哪题了,以后找到了的话会补上。。。
思路就是在每一个网络图中,调用两次bfs算法;
第一次任意选起始点,调用bfs,找到距离最远的点,记录在vr[]中;第二次,对vr[]中的每个点为起点,调用bfs,可以证明:第二次找到的距离最远的点必然有全图里最大的距离。
AC代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
#include<bits/stdc++.h> using namespace std; vector<set<int> > v; vector<bool> vb; vector<int> vr; set<int>::iterator its; int spart,smax; void bfs(int n,bool flag) { vector<bool> vbb(vb.size(),true); int md=0; pair<int,int> p; queue<pair<int,int> > q; q.push(pair<int,int>(n,-1)); if(flag) vb[n]=false; else vbb[n]=false; for(;!q.empty();q.pop()) { p=q.front(); if(p.second>md) { md=p.second; if(flag) { vr.clear(); vr.push_back(p.first); } } else if(p.second==md) if(flag) vr.push_back(p.first); for(its=v[p.first].begin();its!=v[p.first].end();++its) { if(flag) { if(vb[*its]) { vb[*its]=false; q.push(pair<int,int>(*its,p.second+1)); } } else { if(vbb[*its]) { vbb[*its]=false; q.push(pair<int,int>(*its,p.second+1)); } } } } if(md>smax) smax=md; return; } int main() { // ios::sync_with_stdio(false); // freopen("data.txt","r",stdin); int n,k,x; spart=0,smax=0; scanf("%d",&n); v.resize(n); vb.resize(n,true); for(int i=0;i<n;i++) { scanf("%d",&k); for(;k--;) { scanf("%d",&x); x--; v[i].insert(x); v[x].insert(i); } } for(int t=0;t<v.size();t++) { if(vb[t]) { bfs(t,true); for(int i=0;i<vr.size();i++) bfs(vr[i],false); spart++; t=0; } } printf("%d %d",spart,smax); return 0; } |