USACO - Cows in a Row

标准解法是枚举其实也可以做二分匹配,建图注意标号

#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define INF (int)(1e9)
#define maxn 50010
using namespace std;
typedef long long ll;
int Mx[maxn],My[maxn],uN,vN;
int dx[maxn],dy[maxn],dis;
bool vis[maxn];
int p = 0;
struct node {
    int to, w, next;
};
node edge[maxn];
int head[maxn];
void add_edge(int u, int v, int w) {
    edge[p].to = v;
    edge[p].w = w;
    edge[p].next = head[u];
    head[u] = p;
    ++ p;
}
bool search_path() {
    queue<int> Q;
    dis = INF;
    memset(dx, -1, sizeof(dx));
    memset(dy, -1, sizeof(dy));
    for (int i = 0; i < uN; i++)
        if(Mx[i] == -1) {
            Q.push(i);
            dx[i] = 0;
        }
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();
        if (dx[u] > dis)  break;
        int v;
        for (int k = head[u]; k != -1; k = edge[k].next) {
            v = edge[k].to;
            if (dy[v] == -1) {
                dy[v] = dx[u] + 1;
                if (My[v] == -1)  dis = dy[v];
                else {
                    dx[My[v]] = dy[v]+1;
                    Q.push(My[v]);
                }
            }
        }
    }
    return dis != INF;
}
bool dfs(int u) {
    int v;
    for (int k = head[u]; k != -1; k = edge[k].next) {
        v = edge[k].to;
        if (!vis[v] && dy[v]==dx[u]+1) {
            vis[v] = 1;
            if (My[v] != -1 && dy[v] == dis) continue;
            if(My[v] == -1 || dfs(My[v])) {
                My[v] = u;
                Mx[u] = v;
                return true;
            }
        }
    }
    return false;
}
int max_match() {
    int res = 0;
    memset(Mx, -1, sizeof(Mx));
    memset(My, -1, sizeof(My));
    while (search_path()) {
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < uN; i ++)
            if (Mx[i] == -1 && dfs(i))  {
                res ++;
                if (res > 3) return -1;
            }
    }
    return res;
}
struct Point {
    int x, y, a, b;
}no[maxn];
bool cmp1(Point a, Point b) {
    return a.x < b.x;
}
bool cmp2(Point a, Point b) {
    return a.y < b.y;
}
int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        memset(head, -1, sizeof(head)), p = 0;
        for (int i = 0; i < n; ++ i) {
            scanf("%d%d", &no[i].x, &no[i].y);
        }
        sort(no, no+n, cmp1);
        int pp = 0;
        for (int i = 1; i < n; ++ i) {
            if (no[i].x == no[i-1].x) {
                no[i].a = pp;
                no[i-1].a = pp;
            }
            else {
                ++ pp;
                no[i].a = pp;
            }
        }
        uN = pp+1;
        sort(no, no+n, cmp2);
        pp = 0;
        for (int i = 1; i < n; ++ i) {
            if (no[i].y == no[i-1].y) {
                no[i].b = pp;
                no[i-1].b = pp;
            }
            else {
                ++ pp;
                no[i].b = pp;
            }
        }
        vN = pp+1;
        for (int i = 0; i < n; ++ i) {
            add_edge(no[i].a, no[i].b, 0);
        }
        if (max_match() == -1) {
            cout << 0 << endl;
        }
        else cout << 1 << endl;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值