请问我这怎么MLE了
查看原帖
请问我这怎么MLE了
204705
KiDDOwithTopTree楼主2021/12/9 19:01

感觉应该也不怎么会爆栈吧…

#include<iostream>
#include<cstring>
using namespace std;
const int N=4e6+10;
struct edge_node{
	int from,to;
	int nxt;
	edge_node(){
		nxt=-1;
	}
};
struct edge{
	edge_node e[N];
	int head[N],tot;
	edge(){
		memset(head,-1,sizeof head);
	}
	inline void add(int from,int to){
		e[tot].from=from;
		e[tot].to=to;
		e[tot].nxt=head[from];
		head[from]=tot++;
	}
};
edge e;
edge_node tmp;
int low[N],dfn[N],cnt;
int sta[N],top;
int size[N],color[N],col;
int ans[N];
void tarjan(int u){
	low[u]=dfn[u]=++cnt;
	sta[++top]=u;
	for(int i=e.head[u];~i;i=e.e[i].nxt){
		int v=e.e[i].to;
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(!color[v])
			low[u]=min(low[u],low[v]);
	}
	if(low[u]==dfn[u]){
		ans[++col]=u;
		color[u]=col;
		size[col]++;
		while(sta[top]!=u){
			int v=sta[top--];
			color[v]=col;
			size[col]++;
		}
		top--;
	}
}
void topo(){
	for(int u=1;u<=col;u++)
		for(int i=e.head[u];~i;i=e.e[i].nxt)
			low[e.e[i].to]++;
	for(int i=1;i<=col;i++) dfn[i]=size[i];
	for(int i=1;i<=col;i++)
		if(!low[i]) sta[++top]=i;
	while(top){
		int u=sta[top--];
		for(int i=e.head[u];~i;i=e.e[i].nxt){
			int v=e.e[i].to;
			if(dfn[v]<dfn[u]+size[v]){
				dfn[v]=dfn[u]+size[v];
				ans[v]=ans[u];
			}
			low[v]--;
			if(!low[v]) sta[++top]=v;
		}
	}
}
void clear(int n){
	top=cnt=col=e.tot=0;
	for(int i=0;i<=n;i++){
		low[i]=dfn[i]=color[i]=sta[i]=size[i]=ans[i]=0;
		e.head[i]=-1,e.e[i]=tmp;
	}
}
int main(){
	int t;
	cin>>t;
	while(t--){
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++){
				char ch;
				cin>>ch;
				int p=(i-1)*m+j,q=p;
				if(ch=='L')
					if(j!=1) e.add(p,q-1);
				if(ch=='R')
					if(j!=m) e.add(p,q+1);
				if(ch=='U')
					if(i!=1) e.add(p,q-m);
				if(ch=='D')
					if(i!=n) e.add(p,q+m);
			}
		for(int i=1;i<=n*m;i++)
			if(!dfn[i]) tarjan(i);
		for(int i=1;i<=n*m;i++) dfn[i]=low[i]=0;
		int s=e.tot;
		for(int i=1;i<=n*m;i++) e.head[i]=-1;
		e.tot=0;
		for(int i=0;i<s;i++){
			int u=e.e[i].from,v=e.e[i].to;
			if(color[u]!=color[v]) e.add(color[u],color[v]);
		}
		topo();
		int res=0,p=0;
		for(int i=1;i<=col;i++)
			if(res<dfn[i]){
				res=dfn[i];
				p=i;
			}
		cout<<(ans[p]-1)/m+1<<' '<<(ans[p]-1)%m+1<<' '<<res<<'\n';
		clear(n*m);
	}
}
2021/12/9 19:01
加载中...