LightOJ 1209 Strange Voting


1209 - Strange Voting
Time Limit: 2 second(s)Memory Limit: 32 MB

Its year 3000, and the voting system in Ajobdesh has changed to a new era. Instead of the boring old style voting, the new style voting is applied as follows:

1.      Initially there are m male candidates and f female candidates for the parliament. For simplicity the male candidates are numbered as 'M1', 'M2' ... 'Mm' and the female candidates are numbered as 'F1', 'F2' ... 'Ff'.

2.      There are v voters, and each of them can vote like 'P Q', which means, he wants to see P in the parliament and he wants Q to be thrown out of the parliament. For example, if a person voted like, 'M3 F7', that means he wants M3 to be elected and F7 to be thrown out.

3.      The parliament will be formed in such a way that the maximum number of votes is satisfied. A voter, who voted 'P Q', is said to be satisfied if P is in the parliament and Q is not. For example, let the parliament be {M1, M3, F3}, then voter who voted 'M1 F4' is satisfied but neither of 'M1 F3', 'F3 M3' or 'M2 F3' is satisfied.

Since Men don't want to see any Woman in the parliament, each man always votes like 'Mx Fy'. And Women don't want Men in the parliament, so each woman always votes like 'Fy Mx'. Assume that only Men and Women are eligible for voting.

Since you are the leading programmer in Ajobdesh, you have to form the parliament such that maximum number of voters is satisfied. Just report the maximum number of satisfied voters.

Input

Input starts with an integer T (≤ 25), denoting the number of test cases.

Each case starts with a line containing three integers: m, f, v (1 ≤ m, f ≤ 100, 0 ≤ v ≤ 500). Each of the next v lines contains a vote either in the form 'Mx Fy' or 'Fy Mx' (1 ≤ x ≤ m, 1 ≤ y ≤ f).

Output

For each case, print the case number and the maximum number of satisfied voters.

Sample Input

Output for Sample Input

2

1 1 2

M1 F1

F1 M1

1 2 5

M1 F1

M1 F1

M1 F2

F2 M1

F1 M1

Case 1: 1

Case 2: 3


题目大意:是说V个投票人给m个男的(M),f个女的投票(F),M1 F1代表希望M1不希望F1,求出一种安排使满足最多的投票人。

不算题解的题解:只能水的去刷水题,,开始想的是1对于每个投票人中的M1 F1连线匹配,看第二组样例明显不对,因为有两个M1 F1,,然后一时没想出好办法,,实在忍不住瞅了眼别人的,秒懂哭,原来是投票人之间连线匹配,,果然姿势又不对,,。,。把投票人的男女分别看做一个集合,如何两个人之间不满足则连边,这样的二分图求得的最大匹配,即也是最小点覆盖,即为不满足的最少人数。用V减去这个即为要求的结果。
代码:
/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
#define INF 100000000
const double pi=acos(-1.0);
const double eqs=1e-10;
int a[505][505];
struct vote{
    int u,v;
}b[501],c[501];
int flag[505],cx[505],cy[505];
char s1[5],s2[5];
int v,n,m;
bool yy(int u)
{
    for(int i=1;i<=m;i++)
    {
        if(a[u][i]&&!flag[i])
        {
            flag[i]=1;
            if(cy[i]==-1||yy(cy[i]))
            {
                cy[i]=u;
                cx[u]=i;
                return true;
            }
        }
    }
    return false;
}
int gao()
{
    int ans=0;
    memset(cx,-1,sizeof(cx));
    memset(cy,-1,sizeof(cy));
    for(int i=1;i<=n;i++)
    {
        if(cx[i]==-1)
        {
            memset(flag,0,sizeof(flag));
            if(yy(i))
                ans++;
        }
    }
    return ans;
}
int main()
{
    int T,cnt=0;
    scanf("%d",&T);
    int f;
    while(T--)
    {
        scanf("%d%d%d",&m,&f,&v);
        m=n=0;
        for(int i=1;i<=v;i++)
        {
            scanf("%s%s",s1,s2);
            if(s1[0]=='M')
            {
                n++;
                sscanf(s1,"%*c%d",&b[n].u);
                sscanf(s2,"%*c%d",&b[n].v);
            }
            else
            {
                m++;
                sscanf(s1,"%*c%d",&c[m].u);
                sscanf(s2,"%*c%d",&c[m].v);
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                a[i][j]=0;
                if(b[i].u==c[j].v||b[i].v==c[j].u)
                    a[i][j]=1;
            }
        }
        printf("Case %d: %d\n",++cnt,v-gao());
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值