LA 3415 Guardian of Decency (二分图最大独立集)








分析:

详见白书 P356

按照男女分为两个集合a,b      两个集合中的每个元素默认连通     若集合a中的一个元素与集合b中的一个元素不满足其他三个条件   则将他们连通     然后求最大独立集就行了     独立集就是不连通的     连通的不能选去郊游   不连通的就可以选去郊游了









AC代码:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <bitset>
#include <climits>
#include <algorithm>
#define mset(a,x) memset(a,x,sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define FIN     freopen("input","r",stdin)
#define FOUT    freopen("output","w",stdout)
typedef long long LL;
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
using namespace std;
int n;
struct node{
    int hight;
    string st1;
    string st2;
}a[505],b[505];
int mapx[505][505];
int vis[505];
int ans[505];
int len1,len2;
int find(int k){
    for (int i=1;i<len2;i++){
        if (!vis[i]&&mapx[k][i]){
            vis[i]=1;
            if (!ans[i]||find(ans[i])){
                ans[i]=k;return 1;
            }
        }
    }
    return 0;
}
int main (){
    int t;
    //FIN;
    cin>>t;
    while (t--){
        cin>>n;
        int h;
        char f;
        string s1,s2;
        len1=1,len2=1;
        for (int i=0;i<n;i++){
            cin>>h>>f>>s1>>s2;
            if (f=='M') a[len1].hight=h,a[len1].st1=s1,a[len1++].st2=s2;
            else b[len2].hight=h,b[len2].st1=s1,b[len2++].st2=s2;
        }
        mset(mapx,0);
        for (int i=1;i<len1;i++){
            for (int j=1;j<len2;j++){
                if (abs(a[i].hight-b[j].hight)<=40&&a[i].st1==b[j].st1&&a[i].st2!=b[j].st2){
                    mapx[i][j]=1;
                }
            }
        }
        int sum=0;
        mset(ans,0);
        for (int i=1;i<len1;i++){
            mset(vis,0);
            if (find(i)) sum++;
        }
        cout<<n-sum<<endl;
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值