【BZOJ3632】外太空旅行【随机化】【最大团】

【题目链接】

目测是个最大团问题,那么直接随机化...

/* Pigonometry */
#include <cstdio>
#include <cstdlib>
#include <algorithm>

using namespace std;

const int maxn = 55;

int n, ans, g[maxn][maxn], ord[maxn], vis[maxn], clo;

inline int calc() {
	clo++;
	int res = 0;
	for(int i = 1; i <= n; i++) if(vis[i] != clo) {
		res++;
		vis[i] = clo;
		for(int j = i + 1; j <= n; j++) if(!g[ord[i]][ord[j]])
			vis[j] = clo;
	}
	return res;
}

int main() {
	scanf("%d", &n);
	while(1) {
		int u, v;
		if(scanf("%d%d", &u, &v) != EOF) g[u][v] = g[v][u] = 1;
		else break;
	}

	for(int i = 1; i <= n; i++) ord[i] = i;
	for(int T = 1; T <= 100000; T++) {
		for(int i = 1; i <= n; i++) swap(ord[i], ord[rand() % i + 1]);
		ans = max(ans, calc());
	}

	printf("%d\n", ans);
	return 0;
}


你可能感兴趣的:(搜索)