很玄学的错误
  • 板块学术版
  • 楼主osmanlin
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/8/12 17:35
  • 上次更新2024/8/12 19:14:16
查看原帖
很玄学的错误
668649
osmanlin楼主2024/8/12 17:35

在P1640 中,本人写了两个代码:

码①:

#include<iostream>
#include<cstring>
const int M=10100;
const int N=1100000;
using namespace std;
int n,a,b,tot;
bool st[N];
int head[M],match[N],ne[N],to[N];
int read()
{
	char ch;int x=0,f=1;
	while(!isdigit(ch=getchar()))
	{(ch=='-')&&(f=-f);}
	while(isdigit(ch))
	{x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;
}
void add(int x,int y)
{
	to[++tot]=y;
	ne[tot]=head[x];
	head[x]=tot;
}
bool find(int x)
{
	for(int i=head[x];i;i=ne[i])
	{
		int y=to[i];
		if(!st[y])
		{
			st[y]=true;
			if(!match[y]||find(match[y]))
			{
				match[y]=x;
				return true;
			} 
		}
	}
	return false;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		a=read();
		b=read();
		add(a,i);
		add(b,i);
	}
	for(int i=1;i<=10001;i++)
	{
		memset(st,false,sizeof(st));
		if(!find(i))
		{
			cout<<i-1<<endl;
			return 0;
		} 
	}
	return 0;
}

码②:

#include<iostream>
#include<cstring>
const int M=10100;
const int N=1100000;
using namespace std;
int n,a,b,tot;
bool st[N];
int match[N],head[M],ne[N],to[N];
int read()
{
	char ch;int x=0,f=1;
	while(!isdigit(ch=getchar()))
	{(ch=='-')&&(f=-f);}
	while(isdigit(ch))
	{x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;
}
void add(int x,int y)
{
	to[++tot]=y;
	ne[tot]=head[x];
	head[x]=tot;
}
bool find(int x)
{
	for(int i=head[x];i;i=ne[i])
	{
		int y=to[i];
		if(!st[y])
		{
			st[y]=true;
			if(!match[y]||find(match[y]))
			{
				match[y]=x;
				return true;
			} 
		}
	}
	return false;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		a=read();
		b=read();
		add(a,i);
		add(b,i);
	}
	for(int i=1;i<=10001;i++)
	{
		memset(st,false,sizeof(st));
		if(!find(i))
		{
			cout<<i-1<<endl;
			return 0;
		} 
	}
	return 0;
}

结果码①100分,码②60分。。。

2024/8/12 17:35
加载中...