这一道题嘛,水。分类讨论即可
mn 暴力删边判
mn-1 直接做即可
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,a[5010][5010],node[5010][2],cnt=0,tot=0;
int b[5010],c[5010],le,ri,tp=0;
bool bz[5010];
void dfs(int x,int fa)
{
printf("%d ",x);
for (int i=1;i<=a[x][0];i++)
if (a[x][i]!=fa) dfs(a[x][i],x);
}
void dfs_hh(int x)
{
bz[x]=1;
if (tp==2) return;
c[++tot]=x;
if (x>b[tot] && !tp) {tp=2; return;}
else if (x<b[tot]) tp=1;
if (x==le)
{
for (int i=1;i<=a[x][0];i++)
if (!bz[a[x][i]] && a[x][i]!=ri) dfs_hh(a[x][i]);
}
else if (x==ri)
{
for (int i=1;i<=a[x][0];i++)
if (!bz[a[x][i]] && a[x][i]!=le) dfs_hh(a[x][i]);
}
else
{
for (int i=1;i<=a[x][0];i++)
if (!bz[a[x][i]]) dfs_hh(a[x][i]);
}
}
int main()
{
freopen("travel.in","r",stdin);
freopen("travel.out","w",stdout);
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
scanf("%d%d",&node[i][0],&node[i][1]);
a[node[i][0]][++a[node[i][0]][0]]=node[i][1];
a[node[i][1]][++a[node[i][1]][0]]=node[i][0];
}
for (int i=1;i<=n;i++) sort(a[i]+1,a[i]+a[i][0]+1);
if (m==n-1) {dfs(1,0); return 0;}
b[1]=n+1;
for(int i=1;i<=m;i++)
{
le=node[i][0],ri=node[i][1];
memset(bz,0,sizeof(bz));
tot=0;tp=0;dfs_hh(1);
if (tp==1 && tot==n) memcpy(b,c,sizeof(c));
}
for (int i=1;i<=n;i++) printf("%d ",b[i]);
return 0;
}