https://www.patest.cn/contests/pat-t-practise/1014
照抄了http://blog.csdn.net/jtjy568805874/article/details/53610228
这道题开始时想用并查集,但是如何计算半径很复杂。
解题思路:
对每个节点进行bsf得到最大半径(所有bfs得最大深度), 然后bfs可以得到最大团(dfs也可以), 每次bfs开始前看当前开始得节点是否被visit过,如果没有最大团个数加一
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define loop(i,j,k) for(int i=j;i!=-1;i=k[i])
#define inone(i) scanf("%d",&i)
const int maxn = 1e5 + 10;
//ft[i] 中的i是节点的编号, nt[i] 是邻接表中所有边的sequence id. ft[i]的值也是这个sequence id
int ft[maxn], nt[maxn], v[maxn], l[maxn], vt[maxn], n, k, x, tot;
int main()
{
freopen("in.txt", "r", stdin);
tot = 0;
int cnt = 0, ans = 0;
inone(n);
rep(i, 1, n) { ft[i] = -1; vt[i] = 0; }
rep(i, 1, n)
{
inone(k);
rep(j, 1, k) {
inone(x);
v[tot] = x; nt[tot] = ft[i]; ft[i] = tot++;
v[tot] = i; nt[tot] = ft[x]; ft[x] = tot++;
}
}
rep(i, 1, n)
{
queue<int> q;
q.push(i);
if (!vt[i]) cnt++;
rep(j, 1, n) l[j] = 0; l[i] = 1;
while (!q.empty())
{
int t = q.front(); q.pop();
vt[t] = 1;
ans = max(ans, l[t] - 2);
loop(k, ft[t], nt)
{
if (l[v[k]]) continue;
q.push(v[k]); l[v[k]] = l[t] + 1;
}
}
}
printf("%d %d\n", cnt, ans);
return 0;
}