luogu P2423 [HEOI2012]朋友圈 (最大团)

在很久很久以前,曾经有两个国家和睦相处,无忧无虑的生活着。

一年一度的评比大会开始了,作为和平的两国,一个朋友圈数量最多的永远都是最值得他人的尊敬,所以现在就是需要你求朋友圈的最大数目。两个国家看成是AB两国,现在是两个国家的描述:

  1. A国:每个人都有一个友善值,当两个A国人的友善值a、b,如果a xor b mod 2=1,那么这两个人都是朋友,否则不是;
  2. B国:每个人都有一个友善值,当两个B国人的友善值a、b,如果a xor b mod 2=0 或者 (a or b)化成二进制有奇数个1,那么两个人是朋友,否则不是朋友;
  3. A、B两国之间的人也有可能是朋友,数据中将会给出A、B之间“朋友”的情况。
  4. 对于朋友的定义,关系是是双向的。

在AB两国,朋友圈的定义:一个朋友圈集合S,满足

S⊂A∪B,对于所有的i,j∈S,i 和 j 是朋友

由于落后的古代,没有电脑这个也就成了每年最大的难题,而你能帮他们求出最大朋友圈的人数吗?

最大团转为补图的最大独立集, 可以发现A的补图是两个团, B的补图是二分图, 暴力枚举A中选点的情况跑最大匹配即可.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '
'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 1e6+10;
int A, B, m, clk;
int a[N], b[N], f[210][3010];
int a1[N], a2[N], b1[N], b2[N];
int vis[N], fa[N], del[N];
vector<int> g[N];

int dfs(int x) {
	for (int y:g[x]) if (!del[y]&&vis[y]!=clk) {
		vis[y] = clk;
		if (!fa[y]||dfs(fa[y])) return fa[y]=x;
	}
	return 0;
}
int solve(int x, int y) {
	REP(i,1,B) fa[i]=del[i]=0;
	int ans = B;
	REP(i,1,B) if (f[x][i]||f[y][i]) del[i]=1,--ans;
	REP(i,1,*b1) if (!del[b1[i]]) {
		++clk;
		if (dfs(b1[i])) --ans;
	}
	return ans+!!x+!!y;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d%d", &A, &B, &m);
		*b1=*b2=*a1=*a2=0;
		REP(i,1,A) { 
			scanf("%d",a+i);
			if (a[i]&1) a1[++*a1]=i;
			else a2[++*a2]=i;
		}
		REP(i,1,B) { 
			scanf("%d",b+i);
			if (b[i]&1) b1[++*b1]=i;
			else b2[++*b2]=i;
		}
		REP(i,1,A) REP(j,1,B) f[i][j]=1;
		REP(i,1,m) f[rd()][rd()]=0;
		REP(i,1,B) g[i].clear();
		REP(i,1,*b1) REP(j,1,*b2) {
			if (__builtin_popcount(b[b1[i]]|b[b2[j]])&1^1) {
				g[b1[i]].pb(b2[j]),g[b2[j]].pb(b1[i]);
			}
		}
		int ans = 0;
		a1[++*a1]=0,a2[++*a2]=0;
		REP(i,1,*a1) REP(j,1,*a2) ans=max(ans,solve(a1[i],a2[j]));
		printf("%d
", ans);
	}
}
原文地址:https://www.cnblogs.com/uid001/p/10905037.html