[bzoj1854][Scoi2010]游戏(并查集/二分图最大匹配)

来源:互联网 发布:过目不忘的句子 知乎 编辑:程序博客网 时间:2024/06/05 19:21

【题目链接】http://www.lydsy.com/JudgeOnline/problem.php?id=1854
【解题思路】二分图匹配顺序增广即可,还有神奇的并查集做法,详见黄学长博客(http://hzwer.com/2950.html)
【呆马(并查集)】

#include<cstdio>#include<algorithm>#include<cmath>#include<cstdlib>#include<iostream>const int N=1e4+10;using namespace std;int n,x,y,i,fa[N],f[N];int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}int main(){    freopen("bzoj1854.in","r",stdin);    freopen("bzoj1854.out","w",stdout);        scanf("%d\n",&n);        for (i=1;i<=10000;i++) fa[i]=i;        for (i=1;i<=n;i++)        {            scanf("%d%d\n",&x,&y);            x=find(x);            y=find(y);            if (x==y) f[x]=1;            else            {                if (x>y) swap(x,y);                fa[x]=y;                f[x]=1;            }        }        for (i=1;i<=n+1;i++)            if (!f[i])            {                printf("%d",i-1);                return 0;            }    fclose(stdin);    fclose(stdout);}
0 0
原创粉丝点击