QOJ9017. Tourist

· · 题解

:::::success[Hint 1] 根据强连通竞赛图一定存在哈密顿回路的性质,我们考虑求出每一个强连通分量的哈密顿回路,然后最终拼接为一个最长路。 :::::

对应 Hint 1:

我们根据强连通竞赛图一定存在哈密顿回路的性质,考虑求出每一个强连通分量的哈密顿回路(要求是回路是为了确保从任意一个点开始都能走出哈密顿路径),然后最终将 DAG 上位于这个强连通分量后的点拼接为一个最长路,由于本图是竞赛图那么不管这个强连通分量最终路径落到哪个点上都能前往下一个强连通分量。

我们考虑怎么证明强连通竞赛图一定存在哈密顿回路,我们考虑先证明任意竞赛图一定存在哈密顿路径。

我们考虑证明任意竞赛图一定存在哈密顿路径,我们考虑进行归纳,首先当 n=1 时结论显然成立,然后我们归纳假设 n-1 个点的无向图存在哈密顿路径 v_1\rightarrow v_2\rightarrow\dots\rightarrow v_{n-1},现在我们考虑把 n 插入进去。

我们进行分类讨论:

接下来我们考虑证明强连通竞赛图一定存在哈密顿回路,设这个竞赛图中有一个哈密顿路径为 v_1\rightarrow v_2\rightarrow\dots\rightarrow v_{n-1}\rightarrow v_n,接下来我们分类讨论:

那么我们根据上面的讨论进行模拟即可,显然答案是可以取到上界的。

这样我们就做完了这道题。
时间复杂度为 \Theta(n^2),空间复杂度为 \Theta(n)。

:::::success[code]

#include<bits/stdc++.h>
#define inl inline
#define int long long
#define lll __int128
#define fst ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,x,y) for(int i=x;i<=(y);++i) 
#define per(i,x,y) for(int i=x;i>=(y);--i)
#define rpr(i,x,y,z) for(int i=x;i<=(y);i+=z)
#define epe(i,x,y,z) for(int i=x;i>=(y);i-=z)
#define repe(i,x,y) for(i=x;i<=(y);++i) 
#define endl '\n'
#define INF 1e16
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front 
#define fi first
#define se second
#define lcm(x,y) x/__gcd(x,y)*y
#define ull unsigned long long
#define prr make_pair
#define pii pair<int,int> 
#define gt(s) getline(cin,s)
#define at(x,y) for(const auto &x:y)
#define ff fflush(stdout)
#define mt(x,y) memset(x,y,sizeof(x))
#define idg isdigit
#define fp(s) string ssss=s;freopen((ssss+".in").c_str(),"r",stdin);freopen((ssss+\
".out").c_str(),"w",stdout);
#define sstr stringstream 
#define all(x) x.begin(),x.end()
#define mcy(a,b) memcpy(a,b,sizeof(b))
#define ui unsigned
#define si signed
#define eb emplace_back
#define pff(x) ((x)*(x))
#define eush emplace
#define double long double
#define pdi pair<double,int>
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
#ifdef __unix__
#define pc putchar_unlocked
#else
#define pc _putchar_nolock
#endif
using namespace std;
const int N=2005;
bool a[N][N],vis[N];
int dfn[N],low[N],cnt,col[N],color,n,s[N];
vector<int>idx[N],c[N];
stack<int>st;
inl void tarjan(int u){
    dfn[u]=low[u]=++cnt;
    st.push(u);
    vis[u]=1;
    rep(v,1,n)
        if(a[u][v])
            if(!dfn[v]){
                tarjan(v);
                low[u]=min(low[u],low[v]);
            }else if(vis[v]) low[u]=min(low[u],low[v]);
    if(dfn[u]==low[u]){
        ++color;
        while(!st.empty()){
            int v=st.top();
            st.pop();
            col[v]=color;
            vis[v]=0;
            idx[color].pb(v);
            if(u==v) break;
        }
    }
}
si main(){
    fst;
    cin>>n;
    rep(i,2,n)
        rep(j,1,i-1){
            bool x;
            cin>>x;
            a[j][i]=x;
            a[i][j]=!x;
        }
    rep(i,1,n)
        if(!dfn[i]) tarjan(i);
    rep(i,1,color){
        c[i].pb(idx[i][0]);
        if((int)idx[i].size()==1) continue;
        rep(j,1,(int)idx[i].size()-1){
            bool flg=0;
            rep(k,0,(int)c[i].size()-1)
                if(a[idx[i][j]][c[i][k]]){
                    c[i].insert(c[i].begin()+k,idx[i][j]);
                    flg=1;
                    break;
                }
            if(!flg) c[i].pb(idx[i][j]);
        }
        while(!a[c[i].back()][c[i][0]]){
            int x=c[i].back();
            c[i].pob();
            rep(j,0,(int)c[i].size()-1)
                if(a[x][c[i][j]]){
                    c[i].insert(c[i].begin()+j,x);
                    break;
                }
        }
    }
    rep(i,1,color) s[i]=s[i-1]+idx[i].size();
//  rep(i,1,color){
//      rep(j,0,(int)c[i].size()-1) cout<<c[i][j]<<' ';
//      cout<<endl;
//  } 
//  rep(i,1,n) cout<<col[i]<<' ';
//  cout<<endl; 
    rep(i,1,n){
        cout<<s[col[i]]<<' ';
        int id=-1;
        rep(j,0,(int)c[col[i]].size()-1)
            if(c[col[i]][j]==i){
                id=j;
                break;
            }
//      cout<<id<<' '; 
        rep(j,id,(int)c[col[i]].size()-1) cout<<c[col[i]][j]<<' ';
        rep(j,0,id-1) cout<<c[col[i]][j]<<' ';
        per(j,col[i]-1,1)
            at(k,c[j]) cout<<k<<' ';
        cout<<endl;
    } 
    return 'T'^'0'^'m'^'l'^'e';
}

:::::