hdu2063 过山车&&poj1469 COURSES(二分匹配)

65 篇文章 0 订阅
7 篇文章 0 订阅


hdu2063:

http://acm.hdu.edu.cn/showproblem.php?pid=2063

题意思路:裸求最大匹配数。


#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <ctype.h>

using namespace std;

typedef long long LL;

const int N = 505;
const int INF = 0x3f3f3f3f;

int head[N], match[N];
bool vis[N];
int n, m, cnt;

struct Edge
{
    int to,next;
}edge[N*N];

void add(int u, int v)
{
    edge[cnt] = (struct Edge){v, head[u]};
    head[u] = cnt++;
}

void init()
{
    cnt = 0;
    memset(head, -1, sizeof(head));
}

bool Augment(int u)
{
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if(!vis[v])
        {
            vis[v] = true;
            if(match[v]==-1 || Augment(match[v]))
            {
                match[v] = u;
                return true;
            }
        }
    }
    return false;
}

int Hungary(int m)
{
    int ans = 0;
    memset(match, -1, sizeof(match));
    for(int i = 1; i <= m; i++)
    {
        memset(vis, false, sizeof(vis));
        if(Augment(i)) ans++;
    }
    return ans;
}

int main()
{
   // freopen("in.txt", "r", stdin);
    int k, n, m, u, v;
    while(~scanf("%d", &k))
    {
        if(k == 0) break;
        scanf("%d%d", &m, &n);
        init();
        for(int i = 1; i <= k; i++)
        {
            scanf("%d%d", &u, &v);
            add(u, v);
        }
        printf("%d\n", Hungary(m));
    }
    return 0;
}


poj1469:

http://poj.org/problem?id=1469

题意思路:看所有课程是否可以都有匹配对象。


#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <ctype.h>

using namespace std;

typedef long long LL;

const int N = 505;
const int INF = 0x3f3f3f3f;

int head[N], match[N];
bool vis[N];
int n, m, cnt;

struct Edge
{
    int to,next;
}edge[N*N];

void add(int u, int v)
{
    edge[cnt] = (struct Edge){v, head[u]};
    head[u] = cnt++;
}

void init()
{
    cnt = 0;
    memset(head, -1, sizeof(head));
}

bool Augment(int u)
{
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if(!vis[v])
        {
            vis[v] = true;
            if(match[v]==-1 || Augment(match[v]))
            {
                match[v] = u;
                return true;
            }
        }
    }
    return false;
}

int Hungary(int p)
{
    int ans = 0;
    memset(match, -1, sizeof(match));
    for(int i = 1; i <= p; i++)
    {
        memset(vis, false, sizeof(vis));
        if(Augment(i)) ans++;
    }
    return ans;
}

int main()
{
  //  freopen("in.txt", "r", stdin);
    int t, p, n, c, u, v;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &p, &n);
        init();
        for(u = 1; u <= p; u++)
        {
            scanf("%d", &c);
            while(c--)
            {
                scanf("%d", &v);
                add(u, v);
            }
        }
        int ans = Hungary(p);
        if(ans == p) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值