hdu 1054 Strategic Game 【匈牙利算法】

题目链接:http://acm.acmcoder.com/showproblem.php?pid=1054

题意:求无向图的最小顶点覆盖 = 最大匹配数 / 2;

代码:

#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

using namespace std;

int n, m, k, num;
int p[1510][1510];
int book[1510];
int match[1510];
int t, tt;
int a, b;
char tmp;

bool dfs(int u)
{
    int i;
    for (i = 0; i < n; i++)
    {
        if (book[i] == 0 && p[u][i] == 1)
        {
            book[i] = 1;
            if (match[i] == 0 || dfs(match[i]))
            {
                match[i] = u;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    while (scanf("%d", &n) != EOF)
    {
        memset(p,0,sizeof(p));
        memset(match, 0, sizeof(match));

        for (int i = 0; i < n; i++)
        {
            scanf("%d%c%c%d%c",&m,&tmp,&tmp,&k,&tmp);
            for (int j = 1; j <= k; j++)
            {
                scanf("%d",&num);
                p[m][num] = 1;
                p[num][m] = 1;
            }
        }

        int ans = 0;

        for (int i = 0; i < n; i++)
        {
            memset(book, 0, sizeof(book));
            if (dfs(i))
                ans++;
        }
        cout << ans/2 << endl;
    }
    return 0;
}
时间: 2024-08-25 03:54:45

hdu 1054 Strategic Game 【匈牙利算法】的相关文章

hdu 1054 Strategic Game (二分匹配)

Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4697    Accepted Submission(s): 2125 Problem Description Bob enjoys playing computer games, especially strategic games, but somet

hdu 1054 Strategic Game (最小顶点覆盖+稀疏图)

Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4889    Accepted Submission(s): 2225 Problem Description Bob enjoys playing computer games, especially strategic games, but some

【I&#39;m Telling the Truth】【HDU - 3729】 【匈牙利算法,DFS】

思路 题意:该题主要说几个同学分别说出自己的名次所处区间,最后输出可能存在的未说谎的人数及对应的学生编号,而且要求字典序最大. 思路:刚刚接触匈牙利算法,了解的还不太清楚,附一个专门讲解匈牙利算法的博文,个人认为讲的比较清晰. AC代码 #include<iostream> #include<cstdio> #include<cstring> using namespace std; int T, n; struct Stue { int l, r; }; Stue p

HDU1054 Strategic Game——匈牙利算法

Strategic Game Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a t

Hdu 1083 Courses(匈牙利算法模版题)

Hdu 1083 题意:给你一个p表示测试组数,给你n和m表示课的个数和学生的个数,接下来n行首数字i表示该堂课的学生代表人数,之后为i个学生编码,问能否为每堂课找到一个学生课代表且不冲突: 题解:匈牙利算法模版 另附简单易懂匈牙利算法讲解:传送门 #include<cstring> #include<cstdio> const int N =305; using namespace std; bool h[N][N]; bool vis[N]; int link[N]; int

HDU 1054 Strategic Game 最小点覆盖

 最小点覆盖概念:选取最小的点数覆盖二分图中的所有边. 最小点覆盖 = 最大匹配数. 证明:首先假设我们求的最大匹配数为m,那么最小点覆盖必然 >= m,因为仅仅是这m条边就至少需要m个点.然后假如我们已经求得最小覆盖点集,那么在点集中每个点必然有着这样的性质,在于它相连的边里面,一定有一条边的端点不在最小点集中,因为如果连一条这样的边都没有,那这个点完全没有在最小点集的必要,我们任意选取这样的一条边,一定可以形成一个匹配,匹配数与最小点集中的点的个数相等,但现在这仅仅是一个匹配,他必然小于最大

hdu 1054 Strategic Game 二分图最小点覆盖

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题意: 给出一个无向图,求最小点覆盖. 思路: 用网络流来做设立一个超级源点和一个超级汇点. 每个点拆成i和i'. 从超级源点向点i连一条边,容量为1. 从i’向超级汇点连一条边,容量为1. 从i向i'连一条边,容量为正无穷. 然后求最小割/2.因为拆点拆成了2个. 也可以用二分图匹配来做,也是求出最大匹配然后/2. 1 #include <bits/stdc++.h> 2 using na

hdu - 1054 - Strategic Game(树形dp)

题意:一棵n个结点的无根树(0 < n <= 1500),在一个结点放一个士兵,可以守护与这个点相邻的所有边,问最少需要多少个士兵,可以守护所有边. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 -->>状态: dp[i][1]表示以 i 为结点的子树,在 i 放一个士兵,可以守护所有边的最少士兵数. dp[i][0]表示以 i 为结点的子树,在 i 不放士兵,可以守护所有边的最少士兵数. 状态转移方程(结点 j 是结点 i

HDU 1054 Strategic Game(树形DP)

Problem Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which for