Poj_1466 Girls and Boys -最大独立集

题目:找出相互不恋爱的人。

吐槽:500个点用邻接矩阵4000多ms,用了邻接表1000ms不到,所以大约500个点的时候就要考虑邻接表了。

/************************************************
Author        :DarkTong
Created Time  :2016/7/31 20:12:30
File Name     :Poj_1466.cpp
*************************************************/

//#include <bits/stdc++.h>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 500 + 10;
vector<int> w[maxn];
int n, m;
int Left[maxn];
bool used[maxn];
bool match(int j)
{
    for(int i=0;i<w[j].size();++i) if(!used[w[j][i]])
    {
        int v = w[j][i];
        used[v] = true;
        if(Left[v]==-1||match(Left[v]))
        {
            Left[v] = j;
            return true;
        }
    }
    return false;
}
//返回最大匹配数
int hungary()
{
    int res=0;
    memset(Left, -1, sizeof(Left));
    for(int i=0;i<m;++i)
    {
        memset(used, 0, sizeof(used));
        if(match(i)) res++;
    }
    return res;
}

int main()
{
    int T, cas=1;
    while(scanf("%d", &n)==1)
    {
        memset(w, 0, sizeof(w));
        m=n;
        for(int i=0;i<n;++i)
        {
            int u, t, v;
            scanf("%d: (%d)", &u, &t);
            for(int j=0;j<t;++j)
            {
                scanf("%d", &v);
                w[u].push_back(v);
            }
        }
        printf("%d\n", (2*n-hungary())>>1);
    }
    return 0;
}

转载于:https://www.cnblogs.com/DarkTong/p/5724662.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值