题解:CF62D Wormhouse

· · 题解

题目的意思就是给定一个欧拉回路,让你找字典序比它大的欧拉回路中字典序最小的那个。

从限制的角度入手,要找字典序大的,可以钦定新的欧拉回路 P' 中 i 是第一个 p_i' > p_i 的位置,那么在 1 \cdots i 上 p_i' = p_i,i+1 \cdots m+1 上对应该对每个点贪心的往后找一个合法的最小的点,判定能不能走到 p_1。为了让找到的字典序最小,i 应该从后往前扫,这样找出的第一个 i 就是答案。

具体地,维护一个邻接矩阵 G_{i,j},枚举 i \in [1,m],对于当前的 p_i,把 (p_i,p_{i+1}) 这条边放进 G 中,钦定 i+1 这个位置的点,check 新点到 p_1 有没有一个合法的欧拉路径,有的话就记一下当前选的点,找到这个欧拉路径再和前面相同的部分呢拼起来就是一个完整的欧拉回路。

那么问题就变成一个无向图的欧拉路径判定问题了。

  1. 图中不存在奇点。
  2. 存在奇点有且仅有两个。
#include <bits/stdc++.h>
#define ll long long 
#define rep(i,a,n) for(int i = (a);i<=(n);++i)
#define endl '\n'

using namespace std;
template<typename T>inline void read(T &x){
    char c = getchar();
    short int t = 1;x = 0;
    for(;!isdigit(c);c = getchar())if(c == '-')t=-1;
    for(;isdigit(c);c = getchar())x = (x<<1) + (x<<3) + (c^48);
    x*=t;
}
const int N = 2e3 + 10;
int n = 0,m = 0;

int g[N][N],d[N],p[N];
bool vis[N];

void dfs(int u){
    vis[u] = 1;
    for(int v = 1;v<=n;++v){
        if(g[u][v] && !vis[v]){
            dfs(v);
        }
    }
}

bool check(int u,int t,int len){

//  rep(i,1,n){
//      cout <<i<<" "<<d[i]<<endl;
//  }
    int cnt = 0;
    for(int i = 1;i<=n;++i){
        if(d[i] %  2 == 1){
            ++cnt;
        }
    }

    if(cnt != 0 && cnt != 2){
        return 0;
    }
    if(len == 0){
        return u == t;
    }

    memset(vis,0,sizeof vis);
    dfs(u);
    rep(i,1,n){
        if(vis[i] == 0 && d[i] > 0){
            return 0;
        }
    }
    return 1;
}
vector<int> ans;
signed main(){
//  freopen()
//  freopen()

    read(n);read(m);
    rep(i,1,m+1){
        read(p[i]); 
    }

    int len = 0;
    for(int i = m;i >= 1;--i){
        int nw = p[i],nx = p[i+1];

//      cout <<p[i]<<" "<<p[i+1]<<endl;

        g[nw][nx] = g[nx][nw] = 1;
        d[nw]++;
        d[nx]++;

        ++len;
        // 钦定 i+1 这个点选什么。
        for(int v = nx + 1;v<=n;++v){
            if(!g[nw][v]) continue;

//          cout <<i<<" "<<nw<<" "<<v<<" "<<len<<endl; 

//          rep(j,1,n){
//              if(g[v][j]){
//                  cout <<j<<" ";
//              }
//          }
//          cout <<endl;


            g[nw][v] = g[v][nw] = 0;
            d[nw]--;
            d[v]--;

            if(check(v,p[1],len-1)){

                for(int j = 1;j<=i;++j){
                    ans.push_back(p[j]);                    
                }
                ans.push_back(v);

                int t = len - 1;// 2
                int now = v;



                while(t){
//                  cout <<"now: "<<now<<endl;
                    for(int i = 1;i<=n;++i){
                        if(!g[now][i]) continue;
//                      cout <<"nx_v: "<<i<<endl;
                        g[now][i] = g[i][now] = 0;
                        d[now]--;
                        d[i]--;

                        if(check(i,p[1],t-1)){// 剩下 t-1 能不能走。
                                         // 这里应该保证了一定存在能走的方案了吧?
                            ans.push_back(i);
                            --t;
                            now = i;
                            break;
                        }
                        g[now][i] = g[i][now] = 1;
                        d[now]++;
                        d[i]++;
                    }
                }

                for(int j : ans){
                    cout <<j<<" ";
                }
                cout<<endl;
                return 0;
            }

            g[nw][v] = 1;
            g[v][nw] = 1;
            d[nw]++;
            d[v]++;
        }
    }
    cout <<"No solution"<<endl; 


    return 0;
}
/*
1 3
2 3
1 2

idx 1 2 3
num 1 2 1

*/