社区讨论
求助 40分代码 匈牙利最大独立集
P3033[USACO11NOV] Cow Steeplechase G参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @mi7ctptl
- 此快照首次捕获于
- 2025/11/20 19:34 4 个月前
- 此快照最后确认于
- 2025/11/20 19:34 4 个月前
CPP
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int pos;
int maxx;
int minn;
int bh;
};
node th[3000],tl[3000];
int book[3000],match[3000],tu[3000][3000];
int n,h,l;
int dfs1(int);
int dfs2(int);
int main()
{
/*freopen("testdata (25).in","r",stdin);
freopen("test.out","w",stdout);*/
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
tu[i][j]=-1;
int x1,y1,x2,y2;
h=1;l=1;
for(int i=1;i<=n;i++)//读图
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1==x2)//同行
{
th[h].pos=x1;
th[h].maxx=max(y1,y2);
th[h].minn=min(y1,y2);
th[h].bh=i;
h++;
}
if(y1==y2)//同列
{
tl[l].pos=y1;
tl[l].maxx=max(x1,x2);
tl[l].minn=min(x1,x2);
tl[l].bh=i;
l++;
}
}
memset(match,0,sizeof(match));
for(int i=1;i<l;i++)//列
for(int j=1;j<h;j++)//行//建图
{
int bj1=th[j].maxx,bj2=th[j].minn;//取横线的左右端点
if(bj1>=tl[i].pos or bj2<=tl[i].pos)
{
tu[th[j].bh][tl[i].bh]=1;
tu[tl[i].bh][th[j].bh]=1;
}
}
int sum=0;
if(l>=h)
for(int i=1;i<=l;i++)
{
memset(book,0,sizeof(book));
if(dfs1(tl[i].bh))
sum++;
}
else
for(int i=1;i<=h;i++)
{
memset(book,0,sizeof(book));
if(dfs2(th[i].bh))
sum++;
}
printf("%d",n-sum);
getchar();
getchar();
getchar();
return 0;
}
int dfs1(int u)
{
for(int i=1;i<h;i++)
if(tu[u][th[i].bh]==1 and book[th[i].bh]==0)
{
book[th[i].bh]=1;
if(match[th[i].bh]==0 or dfs1(match[th[i].bh]))
{
match[th[i].bh]=u;
return 1;
}
}
return 0;
}
int dfs2(int u)
{
for(int i=1;i<l;i++)
if(tu[u][tl[i].bh]==1 and book[tl[i].bh]==0)
{
book[tl[i].bh]=1;
if(match[tl[i].bh]==0 or dfs2(match[tl[i].bh]))
{
match[tl[i].bh]=u;
return 1;
}
}
return 0;
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...