$ Noip2018 / Luogu5022 $ Travel

$ Luogu $

 

$Description$

$ $ A n-point, $ m $ edges FIG. $ M = n-1 $ or $ m = n $. Arbitrarily selected point as a starting point, you can go not been a point, or back to the first when this point arrives from the a point. FIG traverse the entire claim, the sequence will be traversed a point (in the order of arrival of the row). lexicographically smallest output sequence.

 

$Sol$

First $ m = n-1 $ is very simple case of a tree, $ 1 $ selected as the root node, and similarly traverse down to $ $ DFS, as long as each node in the selected sub lexicographically smallest one go on good. $ 60get. $

$ M = n $, is a tree with a ring, apparently there is an edge will not be walked so long to enumerate ring in an edge to delete, then press the trees do just fine.

 

$Code$

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define il inline
#define Rg register
#define go(i,a,b) for(Rg int i=a;i<=b;++i)
#define yes(i,a,b) for(Rg int i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define u(i) b[i].u
#define v(i) b[i].v
#define ll long long
#define db double
#define inf 2147483647
using namespace std;
il int read()
{
    Rg int x=0,y=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    return x*y;
}
const int N=5010;
int n,m,as[N],ans[N],tmp[N],ct,ct1,x,y;
struct node{int u,v;}b[N],h[N];
bool vis[N];
vector<int>q[N];
il void dfs1(int u,int fa)
{
    as[++ct]=u;
    go(i,0,(int)q[u].size()-1)
    {
        Rg int v=q[u][i];
        if(v==fa)continue;
        dfs1(v,u);
    }
}
il bool dfs2(int u,int fa)
{
    vis[u]=1;
    go(i,0,(int)q[u].size()-1)
    {
        Rg int v=q[u][i];
        if(v==fa)continue;
        if(vis[v]){h[++ct1]=(node){u,v};return 1;}
        if(dfs2(v,u)){h[++ct1]=(node){u,v};return 1;}
    }
    return 0;
}
il void dfs3(int u,int fa)
{
    as[++ct]=u;
    go(i,0,(int)q[u].size()-1)
    {
        Rg int v=q[u][i];
        if(v==fa)continue;
        if(u==x && v==y)continue;
        if(u==y && v==x)continue;
        dfs3(v,u);
    }
}
int main()
{
    n=read(),m=read();
    go(i,1,m)b[i]=(node){read(),read()};
    go(i,1,m)q[u(i)].push_back(v(i)),q[v(i)].push_back(u(i));
    go(i,1,n)sort(q[i].begin(),q[i].end());
    if(m==n-1)
    {
        dfs1(1,0);
        go(i,1,ct)printf("%d ",as[i]);
        return 0;
    }
    vis[1]=1;dfs2(1,0);
    go(i,2,ct1)if(h[i].v==h[1].v){ct1=i-1;break;}
    go(i,1,ct1)
    {
        x=h[i].u,y=h[i].v;ct=0;
        dfs3(1,0);
        if(ans[1]==0)go(i,1,n)ans[i]=as[i];
        else
        {
            bool fl=0;
            go(j,1,ct)
                if(as[j]<ans[j]){fl=1;break;}
                else if(as[j]>ans[j]){break;}
            if(fl)go(j,1,n)ans[j]=as[j];
        }
    }
    go(i,1,n)printf("%d ",ans[i]);
    return 0;
}
View Code

 


 

 

Guess you like

Origin www.cnblogs.com/forward777/p/11402841.html