WA求调(急
查看原帖
WA求调(急
763878
Jerry_heng楼主2024/3/5 15:50
#include<bits/stdc++.h>
using namespace std;
const int mxn=2e5+10;
stack<int>st;
int n,head1[mxn],col,cnt1,cnt2,cnt,dfn[mxn],low[mxn],head2[mxn],beg,end,ans=INT_MAX;
struct node{
	int to,nxt;
}edge1[1000010],edge2[1000010];
void add1(int u,int v){
	edge1[++cnt1]=(node){v,head1[u]};
	head1[u]=cnt1;
	edge1[++cnt1]=(node){u,head1[v]};
	head1[v]=cnt1;
}
void add2(int u,int v){
	edge2[++cnt2]=(node){v,head2[u]};
	head2[u]=cnt2;
	edge2[++cnt2]=(node){u,head2[v]};
	head2[v]=cnt2;
}
void tarjan(int u){
	dfn[u]=low[u]=++cnt;
	st.push(u);
	for(int i=head1[u];i;i=edge1[i].nxt){
		int v=edge1[i].to;
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
			if(low[v]>=dfn[u]){
				col++;
				add2(col,v);
				int x=0;
				do{
					x=st.top();
					st.pop();
					add2(col,x);
				}while(x!=u);
			}
		}
		else low[u]=min(low[u],dfn[v]);
	}
}
void dfs(int u,int f){
	if(u==end){
		if(ans==INT_MAX)puts("No solution");
		else printf("%d\n",ans);
		exit(0);
	}
	for(int i=head2[u];i;i=edge2[i].nxt){
		int v=edge2[i].to;
		if(v==f)continue;
		ans=min(ans,v);
		dfs(v,u);
	}
}
int main(){
	scanf("%d",&n);
	while(1){
		int u,v;
		scanf("%d%d",&u,&v);
		if(!u&&!v)break;
		add1(u,v);
	}
	scanf("%d%d",&beg,&end);
	col=n;
	tarjan(beg);
	if(!dfn[end]){
		puts("No solution");
		return 0;
	}
	dfs(beg,0);
	return 0;
}
2024/3/5 15:50
加载中...