Skip to content

1014 Circles of Friends

Statement

Metadata

  • 作者: CHEN, Yue
  • 单位: 浙江大学
  • 代码长度限制: 16 KB
  • 时间限制: 1000 ms
  • 内存限制: 64 MB

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 \le N \le 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_1p_k

where k (0 \le 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

Solution

#include <bits/stdc++.h>
using namespace std;

const int N = 1e3 + 10, INF = 0x3f3f3f3f;
int n, use[N];

vector<vector<int>> G;

struct BFS {
    int dis[N];
    void gao(int s) {
        memset(dis, 0x3f, sizeof dis);
        dis[s] = 0;
        queue<int> que;
        que.push(s);
        while (!que.empty()) {
            int u = que.front();
            que.pop();
            use[u] = 1;
            for (auto &v : G[u]) {
                if (dis[v] > dis[u] + 1) {
                    dis[v] = dis[u] + 1;
                    que.push(v);
                }
            }
        }
    }
} bfs[N];

int main() {
    scanf("%d", &n);
    G.clear();
    G.resize(n + 1);
    for (int i = 1; i <= n; ++i) {
        int sze;
        scanf("%d", &sze);
        for (int j = 1, x; j <= sze; ++j) {
            scanf("%d", &x);
            G[i].push_back(x);
            G[x].push_back(i);
        }
    }
    memset(use, 0, sizeof use);
    int cnt = 0;
    int Max = 1;
    for (int i = 1; i <= n; ++i) {
        if (!use[i]) {
            ++cnt;
        }
        bfs[i].gao(i);
        for (int j = 1; j <= n; ++j)
            if (bfs[i].dis[j] != INF) {
                Max = max(Max, bfs[i].dis[j]);
            }
    }
    printf("%d %d\n", cnt, Max - 1);
    return 0;
}

Last update: May 4, 2022
Back to top