MLE72pts求调
查看原帖
MLE72pts求调
1015670
Lyco_Reco楼主2024/3/17 11:28
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <deque>
#include <queue>
#include <iomanip>
using namespace std;
#define int long long
#define pb emplace_back
#define inf 0x7f7f7f7f
#define maxn 5010
int n,m,vis[maxn],ans[maxn],outp[maxn],tot,low[maxn],dfn[maxn],idx,cnt,st[maxn],top,bel[maxn];
bool tag[maxn][maxn];
vector<int> g[maxn];
struct edge{
	int u,v;
}a[maxn];
namespace Main{
	int rd(){
		int x=0,f=1;
		char ch=getchar();
		while(ch<'0'||ch>'9'){
			if(ch=='-'){
				f=-1;
			}
			ch=getchar();
		}
		while(ch>='0'&&ch<='9'){
			x=x*10+ch-'0';
			ch=getchar();
		}
		return x*f;
	}
	void wrt(int x){
		int y=10,len=1;
		if(x<0){
			x=-x;
			putchar('-');
		}
		while(y<=x){
			len++;
			y*=10;
		}
		while(len--){
			y/=10;
			putchar(x/y+'0');
			x%=y;
		}
	}
	bool check(){
		for(int i=1;i<=n;i++){
			if(outp[i]>ans[i]){
				return false;
			}
			if(outp[i]<ans[i]){
				return true;
			}
		}
		return true;
	}
	void dfs(int u,int fa){
		if(!vis[u]){
			vis[u]=1;
			ans[++tot]=u;
		}
		for(int i=0;i<g[u].size();i++){
			int v=g[u][i];
			if(fa==v||tag[u][v]){
				continue;
			}
			dfs(v,u);
		}
	}
	void tarjan(int u,int fa){
		low[u]=dfn[u]=++idx;
		st[++top]=u;
		for(int i=0;i<g[u].size();i++){
			int v=g[u][i];
			if((fa^1)==i+1){
				continue;
			}
			if(!dfn[v]){
				tarjan(v,i+1);
			}
			low[u]=min(low[u],low[v]);
		}
		if(dfn[u]==low[u]){
			cnt++;
			while(true){
				int v=st[top--];
				bel[v]=cnt;
				if(u==v){
					break;
				}
			}
		}
	}
	void MAIN(){
		n=rd();
		m=rd();
		for(int i=1;i<=m;i++){
			int u=rd(),v=rd();
			a[i].u=u;
			a[i].v=v;
			g[u].pb(v);
			g[v].pb(u);
		}
		for(int i=1;i<=n;i++){
			sort(g[i].begin(),g[i].end());
		}
		if(m==n-1){
			dfs(1,0);
			for(int i=1;i<=tot;i++){
				wrt(ans[i]);
				putchar(' ');
			}
		}
		else if(m==n){
			for(int i=1;i<=n;i++){
				outp[i]=n+1;
				if(!dfn[i]){
					tarjan(i,0);
				}
			}
			for(int i=1;i<=m;i++){
				if(bel[a[i].u]==bel[a[i].v]){
					tag[a[i].u][a[i].v]=tag[a[i].v][a[i].u]=1;
					tot=0;
					fill(vis+1,vis+n+1,0);
					dfs(1,0);
					if(!check()){
						for(int j=1;j<=n;j++){
							outp[j]=ans[j];
						}
					}
					tag[a[i].u][a[i].v]=tag[a[i].v][a[i].u]=0;
				}
			}
			for(int i=1;i<=tot;i++){
				wrt(outp[i]);
				putchar(' ');
			}
		}
	}
}
signed main(){
//	freopen("travel.in","r",stdin);
//	freopen("travel.out","w",stdout);
//	cin.tie(nullptr)->sync_with_stdio(false);
	Main::MAIN();
	return 0;
}
2024/3/17 11:28
加载中...