【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之间“朋友”的情况。

  1. 在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人。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cctype>
#include <algorithm>
#define digit (ch <  '0' || ch >  '9')
#define clr(x) memset(x, 0, sizeof x)
#define ms(a, x) memset(x, a, sizeof x)
#define LL long long
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif

using namespace std;

const int maxn = 3010;
const int maxm = 3010*3010;
int b[maxn][maxn],head[maxn],to[maxm],next[maxm];
int A[maxn],B[maxn];
int tag[maxn],lk[maxn],tim[maxn],vis[maxn];
int na,nb,m,num,ans,T,T1,T2;

inline void addedge(int x, int y) { to[++num] = y; next[num] = head[x]; head[x] = num; }

inline bool find(int x) {
    for(int p = head[x]; p; p = next[p])
        if(tag[to[p]] == T1 && vis[to[p]] != T2) {
            vis[to[p]] = T2;
            if (tim[to[p]] != T2 || !lk[to[p]] || find(lk[to[p]])) { lk[to[p]] = x; tim[to[p]] = T2; return 1; }
        }
    return 0;
}

int Count(int x) {
    int ans = 0;
    for(int i = x; i; i -= (i&(-i))) ans++;
    return ans;
}

int solve() {
    int ans = 0;
    for(int i = 1; i <= nb; i++) if (tag[i]==T1) {
        T2++;
        if(find(i)) ans++;
    } else ans++;
    return nb-ans;
}

int main() {
    T1 = T2 = ans = 0;
    scanf("%d%d%d",&na,&nb,&m);
    for(int i = 1; i <= na; i++) scanf("%d",&A[i]);
    for(int i = 1; i <= nb; i++) scanf("%d",&B[i]);
    for(int x, y, i = 1; i <= m; i++) scanf("%d%d",&x,&y), b[x][y] = 1;
    for(int i = 1; i <= nb; i++) if(B[i]&1)
        for(int j = 1; j <= nb; j++) if(!(B[j]&1))
            if(!(Count(B[i]|B[j])&1)) addedge(i, j);
    ans = max(ans,solve());
    for(int i = 1; i <= na; i++) {
        T1++;
        for(int j = 1; j <= nb; j++) if(b[i][j]) tag[j] = T1;
        ans = max(ans, solve()+1);
    }
    for(int i = 1; i <= na; i++) if(A[i]&1)
        for(int j = i+1;j<=na;j++) if(!(A[j]&1)) {
            T1++;
            for(int k = 1; k <= nb; k++) if(b[i][k] && b[j][k]) tag[k] = T1;
            ans = max(ans,solve()+2);
        }
    printf("%d\n",ans);
    return 0;    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值