牛客4370 K Color Graph (二分图)

题目链接:https://ac.nowcoder.com/acm/contest/4370/K

利用二分图的性质:必定不存在奇环
那么手动枚举二分图,统计最大答案即可

枚举二分图方法:枚举左部图,与右部图构成的图就一定是二分图

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;

const int maxn = 20;

int T,n,m,ans,tot,Case;
int color[maxn], u[maxn * maxn], v[maxn * maxn];

ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }

int main(){
	T = read(); Case = 0;
	while(T--){
		++Case;
		ans = 0;
		n = read(), m = read();
		for(int i=1;i<=m;++i){
			u[i] = read(), v[i] = read();
		}
		
		for(int S=1;S<(1<<n);++S){
			tot = 0;
			memset(color,0,sizeof(color));
			
			for(int i=1;i<=n;++i){
				if((S>>i-1) & 1) color[i] = 1;
			}
			
			for(int i=1;i<=m;++i){
				if(color[u[i]] ^ color[v[i]]) ++tot;
			}
			ans = max(ans,tot);
		}
		
		printf("Case #%d: %d
",Case,ans);
	} 
	return 0;
}
原文地址:https://www.cnblogs.com/tuchen/p/13888623.html