码迷,mamicode.com
首页 > 其他好文 > 详细

HDU - 1054 Strategic Game

时间:2019-03-26 22:51:50      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:二分图匹配   lse   模板题   clu   turn   max   efi   build   dfs   

二分图匹配模板题

 

#include <bits/stdc++.h>
#define FOPI freopen("in.txt", "r", stdin);
#define FOPO freopen("out.txt", "w", stdout);
using namespace std;
typedef long long LL;
const int maxn = 1500 + 1000;
int n, k, x, y;
int link[maxn], vis[maxn];
vector<int> v[maxn];

void build(int x, int y)
{
    v[x].push_back(y), v[y].push_back(x);
}

bool dfs(int k)
{
    int sz = v[k].size();
    for (int i = 0; i < sz; i++)
    {
        if (!vis[v[k][i]])
        {
            vis[v[k][i]] = 1;
            if (link[v[k][i]] == -1 || dfs(link[v[k][i]]))
            {
                link[v[k][i]] = k;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    int u;
    int res = 0;
    memset(link, -1, sizeof(link));
    for (int i = 0; i < n; i++)
    {
        memset(vis, 0, sizeof(vis));
        if (dfs(i)) res++;
    }
    return res;
}


int main()
{
    while(~scanf("%d", &n))
    {
        for (int i = 0; i < n; i++) v[i].clear();
        for (int i = 1; i <= n; i++)
        {
            scanf("%d:(%d)", &x, &k);
            for (int j = 1; j <= k; j++)
                scanf("%d", &y), build(x, y), build(y, x);
        }
        printf("%d\n", hungary()/2);
    }
}

 

HDU - 1054 Strategic Game

标签:二分图匹配   lse   模板题   clu   turn   max   efi   build   dfs   

原文地址:https://www.cnblogs.com/ruthank/p/10604211.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!