TLE 72pts qwq
查看原帖
TLE 72pts qwq
317622
_Ad_Astra_楼主2023/10/2 16:57

RT,悬赏1关注(码风较丑见谅)

#include <bits/stdc++.h>
using namespace std;
//#define int long long
const int inf=0x3f3f3f3f;
int n,m,s,t,dis[500010],now[500010],head[500010],cnt,b[210][210];
struct node
{
	int to,nxt,flow;
	node(){}
	node(int _to,int _nxt,int _flow)
	{
		to=_to,nxt=_nxt,flow=_flow;
	}
}g[60000010];
void add(int u,int v,int flow)
{
// 	cout<<"output from function add(int u,int v,int flow): u="<<u<<",v="<<v<<",flow="<<flow<<endl;
	g[cnt]=node(v,head[u],flow);
	head[u]=cnt++;
	g[cnt]=node(u,head[v],0);
	head[v]=cnt++;
//	cout<<u<<" "<<v<<" "<<flow<<endl;
}
bool bfs()
{
	for(int i=1;i<=t;i++)dis[i]=-2;
	dis[s]=0;
	queue<int>q;
	q.push(s);
	now[s]=head[s];
	while(!q.empty())
	{
		int u=q.front();q.pop();
		int id=head[u];
// 		cout<<"output from function bfs(): u="<<u<<endl;
		while(id!=-1)
		{
			int v=g[id].to,flow=g[id].flow;
// 			cout<<"output from function bfs(): v="<<v<<",id="<<id<<",flow="<<flow<<endl;
			if(flow>0&&dis[v]==-2)
			{
				dis[v]=dis[u]+1;
				now[v]=head[v];
				if(v==t)return 1;
				q.push(v);
			}
			id=g[id].nxt;
		}
	}
	return 0; 
}
int dfs(int u,int flow)
{
	if(u==t)return flow;
	int ans=0,id=now[u];
	while(id!=-1)
	{
		int v=g[id].to,pflow=g[id].flow;
		if(pflow>0&&dis[v]==dis[u]+1)
		{
			int pans=dfs(v,min(flow,pflow));
			if(!pans)dis[v]=-2;
			g[id].flow-=pans;
			g[id^1].flow+=pans;
			ans+=pans;
			flow-=pans;
		}
		id=g[id].nxt;
	}
	return ans;
}
int dinic()
{
	int ans=0;
	while(bfs())ans+=dfs(s,inf);
	return ans;
}
int getid(int x,int y)
{
	return n*(x-1)+y;
}
int dx[8]={-1,-2,2,1,-1,-2,2,1};
int dy[8]={-2,-1,-1,-2,2,1,1,2};

signed main()
{
	cin>>n>>m;
	s=0;
	t=n*n+1;
	for(int i=0;i<=t;i++)head[i]=-1;
	for(int i=1;i<=m;i++)
	{
		int x,y;
		cin>>x>>y;
		b[x][y]=1;
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(!b[i][j])
				if((i+j)%2)add(s,getid(i,j),1);
				else add(getid(i,j),t,1);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if((i+j)%2)
				for(int k=0;k<8;k++)
				{
					int x=i+dx[k],y=j+dy[k];
					if(x>=1&&x<=n&&y>=1&&y<=n&&!b[x][y])
						add(getid(i,j),getid(x,y),inf);
				}
	cout<<n*n-m-dinic()<<endl;
	return 0; 
}
2023/10/2 16:57
加载中...