[AGC059C] Guessing Permutation for as Long as Possible
Genius_Star · · 题解
或许更好的阅读体验。
找性质题。
思路:
考虑找一下排列合法的充要条件,直接蹬完全看不出来,分析一下,如果在
- 两者之间存在一条有向路径
p_0 = A_i \to p_1 \to \cdots \to p_k \to p_{k + 1} = B_i 。
然后你发现对于这个路径上任意两个点
收到启发,进一步的,你考虑任意三个点
于是对于任意
即
注意到,对于满足满的
所以只要有解,那么对于所有满足条件的
考虑怎么判定无解,对每个
时间复杂度是
完整代码:
#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;
}