bzoj2744: [HEOI2012]朋友圈
Description
在很久很久以前,曾经有两个国家和睦相处,无忧无虑的生活着。一年一度的评比大会开始了,作为和平的两国,一个朋友圈数量最多的永远都是最值得他人的尊敬,所以现在就是需要你求朋友圈的最大数目。
两个国家看成是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 是朋友
由于落后的古代,没有电脑这个也就成了每年最大的难题,而你能帮他们求出最大朋 友圈的人数吗?
Input
第一行t<=6,表示输入数据总数。
接下来t个数据:
第一行输入三个整数A,B,M,表示A国人数、B国人数、AB两国之间是朋友的对数;第二行A个数ai,表示A国第i个人的友善值;第三行B个数bi,表示B国第j个人的友善值;
第4——3+M行,每行两个整数(i,j),表示第i个A国人和第j个B国人是朋友。
Output
输出t行,每行,输出一个整数,表示最大朋友圈的数目。
Sample Input
2 4 7
1 2
2 6 5 4
1 1
1 2
1 3
2 1
2 2
2 3
2 4
Sample Output
5
【样例说明】
最大朋友圈包含A国第1、2人和B国第1、2、3人。
HINT
【数据范围】
两类数据
第一类:|A|<=200 |B| <= 200
第二类:|A| <= 10 |B| <= 3000
题解
xxcc的博客
看这份就可以了。
代码
/**************************************************************
Problem: 2744
User: 2014lvzelong
Language: C++
Result: Accepted
Time:236 ms
Memory:86916 kb
****************************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
const int M = 5500000;
const int N = 3300;
int read() {
char ch = getchar();int x = 0, f = 1;
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
int to[M], nxt[M], pre[N], ti[N], tag[N], g[N][N], top, n, m;
int last[N], vis[N], b[N], a[N], L[N], R[N], tim, cnt, A, B, ans;
void add(int u, int v) {to[++top] = v; nxt[top] = pre[u]; pre[u] = top;}
int count(int x) {int res = 0;for(; x;x -= x & -x, ++res) ;return res;}
bool augment(int u) {
for(int i = pre[u]; i; i = nxt[i])
if(tag[to[i]] == cnt && vis[to[i]] != tim) {
vis[to[i]] = tim;
if(ti[to[i]] != tim || !last[to[i]] || augment(last[to[i]])) {
last[to[i]] = u;
ti[to[i]] = tim;
return true;
}
}
return false;
}
int work() {
int res = 0;
for(int i = 1;i <= B; ++i)
if(tag[i] == cnt) {
++tim;
if(augment(i)) ++res;
}
else ++res;
return B - res;
}
int main() {
A = read(); B = read(); m = read();
for(int i = 1;i <= A; ++i) a[i] = read();
for(int i = 1;i <= B; ++i) b[i] = read();
while(m--) {int u = read(), v = read(); g[u][v] = 1;}
for(int i = 1;i <= B; ++i)
if(b[i] & 1)
for(int j = 1;j <= B; ++j)
if(~b[j] & 1)
if(~count(b[i] | b[j])&1) add(i, j);
ans = max(ans, work());
for(int u = 1;u <= A; ++u) {
++cnt; for(int v = 1; v <= B; ++v) if(g[u][v]) tag[v] = cnt;
ans = max(ans, work() + 1);
}
for(int u = 1;u <= A; ++u)
if(a[u] & 1)
for(int v = u + 1;v <= A; ++v)
if(~a[v] & 1) {
++cnt; for(int i = 1;i <= B; ++i) if(g[u][i] && g[v][i]) tag[i] = cnt;
ans = max(ans, work() + 2);
}
printf("%d\n", ans);
return 0;
}