Arbiter HDU

//构建二分图
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int N=155;
const int INF=0x3f3f3f3f;
struct Edge {
	int from,to;
} edge[N*N];
int tot,n,m;
int read() {
	int res=0,ch,flag=0;
	if((ch=getchar())=='-')             //判断正负
		flag=1;
	else if(ch>='0'&&ch<='9')           //得到完整的数
		res=ch-'0';
	while((ch=getchar())>='0'&&ch<='9')
		res=res*10+ch-'0';
	return flag?-res:res;
}
int delete_num() {
	int minn=INF;
	//枚举每种状态
	//每个点,在二分图中的那一边
	for(int i=0; i<(1<<n); ++i) {
		int ans=0;
		for(int j=2; j<=tot; ++j) { //每条边。
			int x=edge[j].from,y=edge[j].to;
			if( ( (i>>x)&1)==( (i>>y)&1) )    //假如每条边的左右两点在二分图的一边,删除这条边。
				++ans;
		}
		if(minn>ans)
			minn=ans;
	}
	return minn;
}
int main() {
	int T;
	T=read();
	while(T--) {
		tot=1;
		n=read(),m=read();
		while(m--) {
			int x,y;
			x=read(),y=read();
			edge[++tot]=(Edge) {
				x,y
			};
		}
		printf("%d
",delete_num());
	}
}

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12402793.html