首页 > 其他 > 详细

BZOJ 1854 [Scoi2010]游戏 二分图最大匹配

时间:2015-09-24 08:16:51      阅读:217      评论:0      收藏:0      [点我收藏+]

题意:
n种装备,两种属性,属性的值在1~10000范围内,遇到一个boss,需要用从1开始的连续递增的属性值来攻击,并且一件装备只能用一次,询问最多能打多少下。
解析:
比?较经典的二分图最大匹配?
属性值与第几件匹配。
所以直接建图跑下最大匹配即可。
但是这题tmd每一次清一遍数组居然不行- -
也是卡我挺长时间,还以为二分图最大匹配过不了,后来看题解说用时间戳,太神了太神了还有这么好玩的东西- -我怎么不知道。
代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1000100
using namespace std;
int v[N<<1];
int match[N<<1];
int head[N<<1];
int cnt;
int n;
int nowtime;
struct node
{
    int from,to,next;
}edge[N<<1];
void init()
{
    memset(head,-1,sizeof(head));
    cnt=1;
}
void edgeadd(int from,int to)
{
    edge[cnt].from=from,edge[cnt].to=to,edge[cnt].next=head[from];
    head[from]=cnt++;
}
bool find(int x)
{
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(v[to]!=nowtime)
        {
            v[to]=nowtime;
            if(!match[to]||find(match[to]))
            {
                match[to]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    init();
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        edgeadd(x,i),edgeadd(y,i);
    }
    for(int i=1;i<=n;i++)
    {
        nowtime++;
        if(!find(i))
        {
            printf("%d\n",i-1);
            return 0;
        }
    }
    printf("%d\n",n);
} 

版权声明:本文为博主原创文章,未经博主允许不得转载。

BZOJ 1854 [Scoi2010]游戏 二分图最大匹配

原文:http://blog.csdn.net/wzq_qwq/article/details/48706075

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!