标签:scan sizeof 二分图匹配 swap ret size int cow gis
这里分析一类常见的二分图匹配:线段相交
给定n根平行与x或y轴的线段,求最大独立集
最大独立集=总数-最小割=总数-最大流
匹配一下即可
代码:
#include<bits/stdc++.h> using namespace std; #define re register #define il inline const int N=505; const int M=500005; struct line{ int x1,y1,x2,y2; }K[N],K1[N],K2[N]; int cnt1=0; int cnt2=0; struct node{ int x,y; int val; }; int n; int s[N][N]; int l=1,r,ans; node a[N*N+100]; int c[N][N]; int sx[N],sy[N],us[N]; il bool cmp(node x,node y){ return x.val<y.val; } il bool work(int u){ //cout<<u<<endl; for(re int v=1;v<=cnt2;v++){ if(c[u][v]&&!us[v]){ us[v]=1; if(sy[v]==-1||work(sy[v])){ sx[u]=v; sy[v]=u; return 1; } } } return 0; } il int check(){ int sum=0; memset(c,0,sizeof(c)); memset(sx,-1,sizeof(sx)); memset(sy,-1,sizeof(sy)); for(re int i=1;i<=cnt1;i++){ for(re int j=1;j<=cnt2;j++){ if(K1[i].x1>=K2[j].x1&&K1[i].x1<=K2[j].x2&&K2[j].y1>=K1[i].y1&&K2[j].y1<=K1[i].y2){ c[i][j]=1; } } } for(int i=1;i<=cnt1;i++){ memset(us,0,sizeof(us)); sum+=work(i); } return sum; } int main(){ scanf("%d",&n); for(re int i=1;i<=n;i++){ scanf("%d%d%d%d",&K[i].x1,&K[i].y1,&K[i].x2,&K[i].y2); if(K[i].x1>K[i].x2) swap(K[i].x1,K[i].x2); if(K[i].y1>K[i].y2) swap(K[i].y1,K[i].y2); if(K[i].x1==K[i].x2) K1[++cnt1]=K[i]; else K2[++cnt2]=K[i]; } printf("%d\n",n-check()); return 0; }
P3033 [USACO11NOV]牛的障碍Cow Steeplechase
标签:scan sizeof 二分图匹配 swap ret size int cow gis
原文地址:https://www.cnblogs.com/QYJ060604/p/11520632.html