-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path1014_Circles of Friends (35).cpp
46 lines (45 loc) · 1.24 KB
/
1014_Circles of Friends (35).cpp
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
#include <bits/stdc++.h>
using namespace std;
/*
1. 邻接矩阵+floyd, O(n^3)第四组用力超时了
2. 邻接表+BFS,从每一个点BFS一遍,找到最大的diameter,用circle数组标记维护friends的关系,遇到circle未被标记的点,circle的num加1
*/
const int N = 1001;
bool circle[N], vis[N];
vector<int> G[N];
int main() {
int n, k, p;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
circle[i] = false;
scanf("%d", &k);
for (int j = 1; j <= k; j++) {
scanf("%d", &p);
G[i].push_back(p);
G[p].push_back(i);
}
}
int num = 0;
int diameter = 0;
for (int i = 1; i <= n; i++) {
if (!circle[i]) {
circle[i] = true;
num++;
}
memset(vis, false, sizeof(vis));
queue<pair<int, int>> q;
q.push({i, -1});
vis[i] = true;
while (!q.empty()) {
auto c = q.front(); q.pop();
diameter = max(diameter, c.second);
for (auto &it : G[c.first]) {
if (vis[it]) continue;
vis[it] = circle[it] = true;
q.push({it, c.second+1});
}
}
}
printf("%d %d\n", num, diameter);
return 0;
}