noip 2018 Day2 T1 旅行

暴力删边,暴力枚举

#include <bits/stdc++.h>

using namespace std;

#define MAXM 5010

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

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

int a,b,x,y,dx,dy,top = 0,c[MAXM],vis[MAXM],T[MAXM],m[MAXM],e[MAXM][5]; 
vector < int > q[MAXM];

inline void dfs(int x)
{

    T[++top] = x; vis[x] = true;
    int l = q[x].size();
    for(int i = 0;i < l;++i)
    {        
        int y = q[x][i];
        if((vis[y]) || (x == dx && y == dy) || (x == dy && y == dx)) continue;
        dfs(y);
    }
    return ;
}

inline void check()
{
    for(int i = 1;i <= a;++i)
    {
        if(m[i] != T[i])
        {
            if(m[i] < T[i]) return ;
            break;
        }
    }
    for(int i = 1;i <= a;++i)
    m[i] = T[i];
}

int main()
{ 
    memset(m,0x3f,sizeof(m));
    a = read(); b = read();
    for(int i = 1;i <= b;++i)
    {
        int x,y;
        x = read(); y = read();
        q[x].push_back(y);
        q[y].push_back(x);
        e[i][0] = x;
        e[i][1] = y;
    }
    for(int i = 1;i <= a;++i)
        sort(q[i].begin(),q[i].end());
    if(b == a - 1)    
    {
        dx = -1; dy = -1;
        dfs(1);
        for(int i = 1;i <= a;++i)
        write(T[i]),putchar(' ');
    }
    else
    {
        for(int i = 1;i <= b;++i)
        {
            top = 0;
            dx = e[i][0];
            dy = e[i][1];
            memset(T,0,sizeof(T));
            memset(vis,false,sizeof(vis));
            dfs(1);
            if(top != a) continue;
            check(); 
        } 
        for(int i = 1;i <= a;++i)
        write(m[i]),putchar(' ');
    }
    return 0;
}

 

posted @ 2019-02-15 18:39  海边微风起  阅读(223)  评论(0编辑  收藏  举报