pat-top 1014. Circles of Friends (35)

6 篇文章 0 订阅
1 篇文章 0 订阅

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;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值