P1640 [SCOI2010] 连续攻击游戏

P1640 [SCOI2010] 连续攻击游戏
实质上也就是一个匹配问题,然后提供一些类似于后悔的操作,首先分成两堆
然后看当前这个人能从另外一堆中选择谁,一个个看,如果这个人已经被选了,那就看选择这个人的哪一个人是不是可以考虑换一个新的对象

#include <bits/stdc++.h>
using namespace std;
const int N=1e4+5;
const int M=1e6+5;

inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;
}

int h[N],ne[M<<1],e[M<<1],tot;
void add(int from,int to) {
    e[++tot]=to;
    ne[tot]=h[from];
    h[from]=tot;
}

int cnt[N],match[M];
//这个数组,比直接用vis强太多了

bool dfs(int now,int num) {
    for(int i=h[now];i;i=ne[i]) {
        int to=e[i];
        if(cnt[to]==num)continue;
        cnt[to]=num;
        if(match[to]==0||dfs(match[to],num)) {
            match[to]=now;
            return 1;
        }
    }
    return 0;
}

int main() {
    int n=0,m=read();
    for(int i=1;i<=m;i++) {
        int x=read(),y=read();
        add(x,i);//我可以选择你
        add(y,i);
        n=max({n,x,y});
    }
    int ans=0;
    for(int i=1;i<=n;i++) {
        //这里不采用memset(vis,0,sizeof(vis)),会快上很多,也可以使得匈牙利算法使用的数据范围更大一点
        if(dfs(i,i)==0)break;
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}
posted @ 2022-12-02 16:32  basicecho  阅读(39)  评论(0)    收藏  举报