Noip2018旅行

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include     
#include 
#include 
#include <set>
#include 
#include <string>
#include 
#define R register
using namespace std;
typedef long long ull;
const int maxn = 5e3 + 100;

inline int read() 
{
    int s = 0, w = 1;
    char ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    return s * w;
}

inline void write(int x)
{
    if(x < 0) putchar('-'),x = -x;
    if(x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

vector <int> vec[maxn];
int ans[maxn], edge[maxn][2], t[maxn], vis[maxn];
int n, m, da, db, tsize = 0;

inline void dfs(int x) 
{
    t[++tsize] = x; 
    vis[x] = 1;
    int l = vec[x].size();
    for (R int i = 0; i < l; ++i) 
    {
        int y = vec[x][i];
        if (!vis[y] && !((x == da && y == db) || (x == db && y == da))) 
        //如果y点没有走过,且要走的边,不是删去的边 
           dfs(y);
    }
    return ;
}

inline void check() 
{
    if (tsize != n) return ;
    for (R int i = 1; i <= n; ++i) 
    {
        if (t[i] != ans[i]) 
        {
            if (t[i] > ans[i]) return ;
            break;
        }
    }
    for (R int i = 1; i <= n; ++i) 
    {
        ans[i] = t[i];
    }
    return ;
}

int main() 
{
    memset(ans, 0x3f, sizeof(ans));
    n = read(), m = read();
    for (R int i = 1; i <= m; ++i) {
        int a = read(), b = read();
        vec[a].push_back(b);
        vec[b].push_back(a);
        edge[i][0] = a;
        edge[i][1] = b;
    }
    for (R int i  = 1; i <= n; ++i) 
         sort(vec[i].begin(), vec[i].end());
    if (n > m) 
    {
        da = -1, db = -1;
        dfs(1);
        check();
    }
    else {
        for (R int i = 1; i <= m; ++i) 
        {
            tsize = 0;
            da = edge[i][0];
            db = edge[i][1];
            memset(vis, 0, sizeof(vis));
            dfs(1);
            check();
        }
    }
    for (R int i = 1; i <= n-1; ++i) write(ans[i]), putchar(' ');
    write(ans[n]);
    return 0;
}


#include 
#include 
#include 
#include 
using namespace std;
const int N=5005;
const int INF=0x7fffffff/2;
vector<int> G[N];
vector<int> temp,ans;
int du[N],cst,ced;
int n,m,pt,vis[N],nxt[N];
int f[N],st;
void Add(int x,int y) 
{
    G[x].push_back(y);
}
void Dfs(int x,int prt) 
{
    ans.push_back(x);
    for (int i=0;i) 
    {
        int y=G[x][i];
        if (y==prt) continue;
        Dfs(y,x);
    }
}
void FindCircle(int x,int prt) 
{
    vis[x]=1;
    for (int i=0;i) {
        int y=G[x][i];
        if (y==prt) 
            continue;
        if (vis[y]&&!pt) //如果y已访问过且从前未找到过环时 
        {
            nxt[x]=y;
            f[x]=1;//y在环上 
            pt=1;
            st=y;//环的开始点为y 
        } 
        else if (!vis[y]) 
        {
            FindCircle(y,x);
            if (pt&&f[y]) //如果找到环且y在环上 
            {
                nxt[x]=y;
                f[x]=1;
            }
        }
    }
    if (x==st) 
        pt=0;
}
void Dfs2(int x,int prt) 
{
    temp.push_back(x);
    for (int i=0;i) 
    {
        int y=G[x][i];
        if (y==prt) continue;
        if ((x==cst&&y==ced)||(y==cst&&x==ced)) continue;
        Dfs2(y,x);
    }
}
int main() 
{
  
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++) 
    {
        int u,v;
        scanf("%d%d",&u,&v);
        Add(u,v);
        Add(v,u);
    }
    for (int i=1;i<=n;i++) 
    {
        sort(G[i].begin(),G[i].end());
        du[i]=G[i].size();
    }
    if (m==n-1) 
    {
        Dfs(1,0);
        for (int i=0;i1;i++) printf("%d ",ans[i]);
        printf("%d",ans[n-1]);
    } else 
    {
        pt=0;
        FindCircle(1,0);
        int rt=st;
        for (int i=1;i<=n;i++) 
            ans.push_back(INF);
        do 
        {
            cst=rt;
            ced=nxt[rt];
            temp.clear();
            Dfs2(1,0);
            rt=nxt[rt];
            if (ans>temp) 
            ans=temp;
        } 
        while (st!=rt);
        for (int i=0;i1;i++) 
            printf("%d ",ans[i]);
        printf("%d",ans[n-1]);
    }
    return 0;
}

 

你可能感兴趣的:(Noip2018旅行)