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

来源:互联网 发布:雍正王朝 知乎 编辑:程序博客网 时间:2024/06/14 03:02

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:
172 15 121 172 16 91 84 10 13 15 1402 11 141 42 2 32 13 112 15 72 1 142 5 15001 31 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
原创粉丝点击