[AGC059C] Guessing Permutation for as Long as Possible

· · 题解

或许更好的阅读体验。

找性质题。

思路:

考虑找一下排列合法的充要条件,直接蹬完全看不出来,分析一下,如果在 (A_i, B_i) 时不合法说明什么:

然后你发现对于这个路径上任意两个点 p_i, p_j 都知道它们的大小关系,所以如果后面询问了 (p_i, p_j),那就死了。

收到启发,进一步的,你考虑任意三个点 a, b, c,如果出现了 a \to b \to c 活着 c \to b \to a 这样的结构且 (a, c) 还没有询问那就似了;你发现这是充要条件,证明是显然的。

于是对于任意 a, b, c,当 (a, b), (b, c) 比 (a, c) 先询问时,一定要有 A_b 不能在 A_a 和 A_c 之间,即 A_b \not\in (\min(A_a, A_c), \max(A_a, A_c)),拆开来看,要满足以下两个条件之一:

即 A_b 和 A_a 的关系与 A_b 与 A_c 的关系是绑在一起的;欸,这不是并查集吗?考虑将所有绑在一起的关系 (b, a) 与 (b, c) 合并在一起,那么最后对于每个集合都只有全大于或者全小于两者方式。

注意到,对于满足满的 \frac{n(n - 1)}{2} 条大小关系的排列数量要么是 0 要么是 1,为 0 当且仅当出现了环冲突了;那怎么考虑判环呢?你发现不需要,因为如果出现了环,考虑最后接上环的那次询问,一定是不合法的,于是一定不满足限制。

所以只要有解,那么对于所有满足条件的 \frac{n(n - 1)}{2} 都会唯一提供一个排列,于是答案是 2^{cnt},其中 cnt 是连通块数量。

考虑怎么判定无解,对每个 (i, j) 扩展域并查集一下,建两种点,一个是 P_i > P_j 一个是 P_i < P_j,最后如果 (i, j) 和 (j, i) 在一个集合中就无解!

时间复杂度是 O(N^3)。

完整代码:

#include<bits/stdc++.h>
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define fi first
#define se second
#define popcnt(x) __builtin_popcount(x)
#define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout);
using namespace std;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
const int N = 405, M = N * N + 10, mod = 1e9 + 7;
inline ll read(){
    ll x = 0, f = 1;
    char c = getchar();
    while(c < '0' || c > '9'){
        if(c == '-')
          f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9'){
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return x * f;
}
inline void write(ll x){
    if(x < 0){
        putchar('-');
        x = -x;
    }
    if(x > 9)
      write(x / 10);
    putchar(x % 10 + '0');
}
int n, cnt;
int tim[N][N], id[N][N];
int fa[M];
inline int getfa(int u){
    if(u != fa[u])
      return fa[u] = getfa(fa[u]);
    return fa[u];
}
inline void merge(int x, int y){
    x = getfa(x), y = getfa(y);
    // if(x == y){
    //     puts("0");
    //     exit(0);
    // }
    fa[x] = y;
}
int main(){
    n = read();
    for(int i = 1; i <= (n * (n - 1) >> 1); ++i){
        int x = read(), y = read();
        tim[x][y] = i;
    }
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            if(i == j)
              continue;
            id[i][j] = ++cnt;
            fa[cnt] = cnt;
        }
    }
    for(int b = 1; b <= n; ++b){
        for(int a = 1; a <= n; ++a){
            if(a == b)
              continue;
            for(int c = a + 1; c <= n; ++c){
                if(c == b)
                  continue;
                if(tim[a][c] > tim[min(a, b)][max(a, b)] && tim[a][c] > tim[min(b, c)][max(b, c)]){
                    merge(id[b][a], id[b][c]);
                    merge(id[a][b], id[c][b]);
                }
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        for(int j = i + 1; j <= n; ++j){
            if(getfa(id[i][j]) == getfa(id[j][i])){
                puts("0");
                exit(0);
            }
        }
    }
    int sum = 0;
    for(int i = 1; i <= cnt; ++i)
      if(getfa(i) == i)
        ++sum;
    sum >>= 1;
    int ans = 1;
    while(sum--)
      ans = 2ll * ans % mod;
    write(ans);
    return 0;
}