【PAT-T】1014. Circles of Friends (35)

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 2
Sample Output:
4 3
分析:图层序遍历;记录层数。
细节:1000ms执行时间

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
#include <functional>

#define rep(i,j,k) for(int i=j;i<=k;++i)
const int Max=1000;

using namespace std;
struct Ver
{
    vector<int> edge;
}ver[Max];

class graph
{
    public:
       int verCount,edgeCount;
       int visited[Max];
       graph();
       void insertEdge(int v, int w);
       int BFS(int v);
       void check();
};

graph::graph()
{
    rep(i,1,Max){
        visited[i]=0;
    }
    verCount=edgeCount=0;
}

void graph::insertEdge(int v, int w)
{
    ++edgeCount;
    ver[v].edge.push_back(w);
    ver[w].edge.push_back(v);
}

int graph::BFS(int v)
{
    int sum=0;
    queue<int> q;
    visited[v]=1;
    q.push(v);
    int flag1=v,flag2=0;
    while(!q.empty()){
        v=q.front();
        int w;
        int k=ver[v].edge.size();
        rep(i,0,k-1){
            w=ver[v].edge[i];
            if (visited[w]==0){
                visited[w]=1;
                q.push(w);
                flag2=w;
            }
        }
        if (flag1==v){
        	++sum;
        	flag1=flag2;
        	flag2=0;
		}
		q.pop();        
    }
    return sum;
}

void graph::check()
{
	rep(i,1,verCount){
		visited[i]=0;
	}
}

int main()
{
//	freopen("test.txt","r",stdin);
    graph g;
    int n;
    cin>>n;
    g.verCount=n;
    rep(i,1,n){
        int k;
        cin>>k;
        rep(j,1,k){
            int w;
            cin>>w;
            g.insertEdge(i,w);
        }
    }
    int sum=0,flag=0;
    rep(i,1,n){
        if (g.visited[i]==0){
        	g.BFS(i);
            ++sum;
        }
    }
    rep(i,1,n){
    	g.check();
    	flag=max(flag,g.BFS(i));
	}
	flag=(flag>=2?flag-2:flag-1);
    cout<<sum<<' '<<flag<<endl;
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值