JZOJ5934. 【NOIP2018模拟10.29】列队

Description

Sylvia是一个热爱学习的女孩子。
在平时的练习中,他总是能考到std以上的成绩,前段时间,他参加了一场练习赛,众所周知,机房是一个 的方阵。这天,他又打爆了std,感到十分无聊,便想要hack机房内同学的程序,他会挑选一整行或一整列的同学进行hack ( 而且每行每列只会hack一次 ),然而有些同学不是那么好惹,如果你hack了他两次,他会私下寻求解决,Sylvia十分害怕,只会hack他们一次。假设Sylvia的水平十分高超,每次hack都能成功,求他最 多能hack多少次?

Input

第一行两个数 表示机房的大小和不好惹的同学个数
接下来x行,每行两个数 表示不好惹的同学坐标

Output

一个数表示最多hack多少次

Sample Input

2 1
1 1

Sample Output

6

Data Constraint

数据规模和约定
对于20%的数据 n<=10, x<=100
对于40%的数据 n<=20 , x<=400
对于100%的数据 n<=1000 , x<=4000
1<=x,y<=n且同一个点不会重复出现

题解

分析一下题目的意思,
就是说如果某个点有一个不好惹的同学,
那么这个行和这个列就不能同时取到。
而其他没有不好惹同学的位置就可以同时取到。
这样问题件变为了求能最多去多少行和列,使得他们之间不冲突。

code

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#define N 4003
#define P putchar
#define G getchar
using namespace std;
char ch;
void read(int &n)
{
	n=0;
	ch=G();
	while((ch<'0' || ch>'9') && ch!='-')ch=G();
	int w=1;
	if(ch=='-')w=-1,ch=G();
	while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
	n*=w;
}

int n,m,x,y,tot,nxt[N*2],to[N*2],lst[N],ans,f[N];
bool bz[N];

void ins(int x,int y)
{
	nxt[++tot]=lst[x];
	to[tot]=y;
	lst[x]=tot;
}

bool find(int x)
{
	for(int i=lst[x];i;i=nxt[i])
	{
		if(bz[to[i]])continue;bz[to[i]]=1;
		if(f[to[i]]==0 || find(f[to[i]]))
		{
			f[to[i]]=x;
			return 1;
		}
	}
	return 0;
}

int main()
{
	freopen("phalanx.in","r",stdin);
	freopen("phalanx.out","w",stdout);
	
	read(n);read(m);
	for(int i=1;i<=m;i++)read(x),read(y),ins(x,y);
	ans=n*2;
	for(int i=1;i<=n;i++)
	{
		memset(bz,0,sizeof(bz));
		if(find(i))ans--;
	}
	printf("%d",ans*n);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lijf2001/article/details/83584425