luogu 1640 连续攻击游戏

时间:2023-03-09 13:35:56
luogu 1640 连续攻击游戏

二分图匹配,将需要进行的编号(1-10000)和物件进行匹配,而非编号之间,编号对应物品

#include<bits/stdc++.h>
using namespace std;
const int N=;
const int M=; inline int read(){
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+(ch^);ch=getchar();}
return x*f;} int n,tot,ans,vis[N],head[N],link[M];
struct node{int v,next;}e[M<<];
void insert(int u,int v){
e[++tot]=(node){v,head[u]};head[u]=tot;} int match(int u){
if(vis[u]) return ;
vis[u]=;
for(int i=head[u];i;i=e[i].next){
int v=e[i].v;
if(!link[v]||match(link[v])){
link[v]=u;return ;}
}return ;
}
/*
int match(int u){
if(vis[u]) return 0;
vis[u]=1;
for(int i=head[u];i;i=e[i].next){
int v=e[i].v;
if(vis[v]) continue;
vis[v]=1;
if(!link[v]||match(link[v])){
link[v]=u;return 1;}
}return 0;
}
上述写法会漏掉在match(link[v])中对link[v]的vis判断
*/ //将编号和属性相连接进行二分图匹配 //寻找每个属性由1-10000(x,y) 匹配到的物件编号(i)
int main(){
n=read();
for(int x,y,i=;i<=n;i++){
x=read(),y=read();
insert(x,i),insert(y,i);}
for(int i=;i<=;i++){
memset(vis,,sizeof vis);
if(match(i)) ans++;
else break;}
printf("%d",ans);return ;
}