pat-top 1014. Circles of Friends (35)

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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章