【贪心】 FOJ 2141 Sub-Bipartite Graph

贪心。。。挨个判断每个点染成白点优还是黑点优,取个最优的染色即可。。。

#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 105
#define maxm 100005
#define eps 1e-7
#define mod 1000000009
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
//head

bool g[maxn][maxn];
int color[maxn];
int aa, bb;
int n, m;

void solve(int u)
{
	int a = 0, b = 0;
	color[u] = 1;
	for(int i = 1; i <= n; i++) if(g[u][i] && color[u] != color[i]) a++;
	color[u] = 2;
	for(int i = 1; i <= n; i++) if(g[u][i] && color[u] != color[i]) b++;
	if(a > b) color[u] = 1, aa++;
	else color[u] = 2, bb++;
}

void work()
{
	memset(g, 0, sizeof g);
	memset(color, 0, sizeof color);
	scanf("%d%d", &n, &m);
	while(m--) {
		int u, v;
		scanf("%d%d", &u, &v);
		g[u][v] = g[v][u] = true;
	}
	aa = bb = 0;
	for(int i = 1; i <= n; i++) solve(i);
	printf("%d", aa);
	for(int i = 1; i <= n; i++) if(color[i] == 1) printf(" %d", i);
	printf("\n");
	printf("%d", bb);
	for(int i = 1; i <= n; i++) if(color[i] == 2) printf(" %d", i);
	printf("\n");
}

int main()
{
	int _;
	scanf("%d", &_);
	while(_--) work();
	
	return 0;
}


你可能感兴趣的:(贪心)