POJ1419 Graph Coloring

嘟嘟嘟


求无向图的最大独立集。
有这么一回事:最大独立集=补图的最大团。
所谓的最大团,就是一个子图,满足图中任意两点都有边。
然后ssy巨佬告诉了我一个很没有道理强的做法:随机。
每一次random_shuffle储存节点的数组,然后从头开始扫每一个点,能加入最大团就加入,否则continue。
最后看看是不是比当前答案优。
一直随机,直到一秒。
我也不知道这有什么道理,但就是过了。
.p.s:poj太慢了,得随机到0.3秒(竟然还能过)。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 105;
inline ll read()
{
	ll ans = 0;
	char ch = getchar(), last = ' ';
	while(!isdigit(ch)) last = ch, ch = getchar();
	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	if(last == '-') ans = -ans;
	return ans;
}
inline void write(ll x)
{
	if(x < 0) x = -x, putchar('-');
	if(x >= 10) write(x / 10);
	putchar(x % 10 + '0');
}

int n, m, G[maxn][maxn], pos[maxn];
int ans[maxn], Ans = 0;

int a[maxn], cnt = 0;
In bool check(int now)
{
	for(int i = 1; i <= cnt; ++i) if(G[a[i]][now] == -1) return 0;
	return 1; 
}
In int solve()
{
	cnt = 0;
	for(int i = 1; i <= n; ++i) if(check(pos[i])) a[++cnt] = pos[i];
	return cnt;
}

int main()
{
	int T = read();
	const db lim = 0.3 * CLOCKS_PER_SEC / T;
	while(T--)
	{
		Mem(G, 0);
		n = read(); m = read();
		for(int i = 1; i <= m; ++i)
		{
			int x = read(), y = read();
			G[x][y] = G[y][x] = -1;
		}
		Ans = 0;
		for(int i = 1; i <= n; ++i) pos[i] = i;
		Ans = 0;
		db beg = clock(), end = beg;
		while(end - beg < lim)
		{
			random_shuffle(pos + 1, pos + n + 1);
			int tp = solve();
			if(tp > Ans)
			{
				Ans = tp;
				for(int i = 1; i <= Ans; ++i) ans[i] = a[i];
				if(Ans == n) break;
			}
			end = clock();
		}
		write(Ans), enter;
		sort(ans + 1, ans + Ans + 1);
		for(int i = 1; i <= Ans; ++i) write(ans[i]), i == n ? enter : space;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/mrclr/p/10265588.html