[AGC59C] Guessing Permutation for as Long as Possible
Problem Statement
A teacher has a hidden permutation $P=(P_1,P_2,\ldots,P_N)$ of $(1,2,\cdots,N)$.
You are going to determine it.
To do this, you prepared a sequence of pairs of integers $(A_1,B_1),(A_2,B_2),\ldots,(A_{N(N-1)/2},B_{N(N-1)/2})$; this is a permutation of all pairs of the form $(a,b)$ ($1 \le a < b \le N$).
Now, you will go over the pairs from the beginning. For a pair $(A_i, B_i)$, you will ask if $P_{A_i}<P_{B_i}$, and the teacher will tell you the answer.
However, you will skip this question if you can already determine the answer to it from previous answers.
Find the number of permutations $P$, for which with your algorithm you will have to ask all $\frac{N(N-1)}{2}$ questions, modulo $10^9+7$.
Constraints
- $2 \le N \leq 400$
- $1 \le A_i < B_i \le N$
- $(A_i,B_i) \neq (A_j,B_j)$ ($i \neq j$)
- All values in the input are integers.
Input
Input is given from Standard Input in the following format:
$N$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_{N(N-1)/2}$ $B_{N(N-1)/2}$
Output
Print the answer.
Sample Input 1
2
1 2
Sample Output 1
2
Clearly, for any permutation $P$, you need to ask $1$ question.
Sample Input 2
4
1 2
1 3
2 3
2 4
3 4
1 4
Sample Output 2
4
Consider $P=(2,3,1,4)$ as an example.
In this case, you will skip the third question since you know $P_1 < P_2$ and $P_1 > P_3$ from previous questions and you can determine $P_2 > P_3$.
Therefore, $P=(2,3,1,4)$ should not be counted.
Sample Input 3
5
1 2
2 3
3 4
4 5
1 5
1 3
2 4
3 5
1 4
2 5
会发现如果我们给每一个询问钦定了答案,那么最后的序列可以由我们钦定的答案推出。仔细想想,一个答案序列只对应一个排列,一个排列也只对应一个答案序列。所以我们去考虑有多少种钦定答案的方式。
这还不简单,\(2^{n*(n-1)/2}\) 种吗。肯定不对,不对就在于会出现冲突。而且按照题目的要求,我们不能由某两个询问去推出后面的询问的答案。
如果现在有两个询问 \((a,b)\) 和 \((b,c)\),他们的回答都为真的话,我们可以推断出 \(p_a<p_c\),而如果询问 \((a,c)\) 还没被问到,那就不好了。所以出现这种情况时,如果 \((a,b)\) 为真,\((b,c)\) 必定为假,如果 \((a,b)\) 为假, \(b,c\) 必定为真。
那么考虑使用并查集维护。之所以不用 2-sat 是因为这里的边是双向的。知道了 \((a,b)\) 为假时 \((b,c)\) 必定为真,而 \((b,c)\) 为真时 \((a,b)\) 必然为假。可以参考上面的例子得知。并查集中一个记录为真的域,一个记录为假的域。然后把互相之间可以推出的一些关系合并。
具体而言,枚举到一个询问,枚举之前的询问,判断四个数中是否有两个相等,如果有,那就按照上面的方式连边就好了。有时候是真真连假假连,有时候是真假互相连。但这样复杂度 \(O(n^4)\)
考虑优化,发现我们要四个数中有两个相等。可以用vector记录下那些询问 \(a_i=x\),哪些询问满足 \(b_i=y\)。然后每次只用遍历vector中的数,分成四种情况。这样复杂度就是 \(O(n^3)\).
如果最终某一个询问为真可以推出这个询问为假,无解。这个图极具对称性,如果 \(a_1(True),a_2(False),a_3(True)\) 在一个连通块,那么 \(a_1(False),a_2(True),a_3(False)\) 也在一个连通块。而这两种答案序列分配方式任选一个都是可以的。所以如果最后总共有 \(c\) 个连通块,答案为 \(2^{\frac c2}\)
#include<bits/stdc++.h>
using namespace std;
const int N=805,P=1e9+7;
int fa[N*N],n,v[N][N],m,a[N*N],b[N*N],cnt,pw=1;
vector<int>ga[N],gb[N];
int find(int x)
{
if(fa[x]==x)
return x;
return fa[x]=find(fa[x]);
}
int main()
{
scanf("%d",&n),m=n*(n-1)/2;
for(int i=1;i<=m;i++)
{
// printf("%d\n",i);
fa[i]=i,fa[i+m]=i+m;
scanf("%d%d",&a[i],&b[i]);
v[a[i]][b[i]]=v[b[i]][a[i]]=1;
for(int j=0;j<ga[a[i]].size();j++)
{
if(!v[b[i]][b[ga[a[i]][j]]])
{
fa[find(ga[a[i]][j]+m)]=find(i+m);
fa[find(ga[a[i]][j])]=find(i);
}
}
// puts("hjhyyds");
for(int j=0;j<gb[b[i]].size();j++)
{
if(!v[a[i]][a[gb[b[i]][j]]])
{
fa[find(gb[b[i]][j]+m)]=find(i+m);
fa[find(gb[b[i]][j])]=find(i);
}
}
// puts("hjhakioi");
for(int j=0;j<ga[b[i]].size();j++)
{
if(!v[a[i]][b[ga[b[i]][j]]])
{
fa[find(ga[b[i]][j]+m)]=find(i);
fa[find(ga[b[i]][j])]=find(i+m);
}
}
// puts("qzmyyds");
for(int j=0;j<gb[a[i]].size();j++)
{
if(!v[b[i]][a[gb[a[i]][j]]])
{
fa[find(gb[a[i]][j]+m)]=find(i);
fa[find(gb[a[i]][j])]=find(i+m);
}
}
// puts("qzmakioi");
ga[a[i]].push_back(i);
gb[b[i]].push_back(i);
// for(int j=1;j<i;j++)
// {
// if(a[i]==b[j]&&!v[a[j]][b[i]]||a[j]==b[i]&&!v[a[i]][b[j]])
// {
// fa[find(i+m)]=find(j);
// fa[find(i)]=find(j+m);
// }
// }
}
for(int i=1;i<=2*m;i++)
{
if(find(i)==find(i+m))
{
puts("0");
return 0;
}
if(fa[i]==i)
++cnt;
}
for(int i=1;i<=cnt/2;i++)
(pw<<=1)%=P;
printf("%d",pw);
}
[AGC59C] Guessing Permutation for as Long as Possible的更多相关文章
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 2632: [neerc2011]Gcd guessing game
2632: [neerc2011]Gcd guessing game Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 144 Solved: 84[S ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- Permutation test: p, CI, CI of P 置换检验相关统计量的计算
For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...
- Permutation
(M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II
随机推荐
- [Lua] IT技术熟练度生成器 | 根据IT活动记录生成md表格 | 自创
IT技术熟练度 v1.0 为衡量个人能力水平自创的一套评分机制,根据时间.代码行数.基础理论三个变量生成.最近在学lua,正好练下基本功.效果可见 个人介绍 | 代码统计 - 小能日记 - 博客园 ( ...
- 5、Mybatis之获取参数值
5.1.创建新module 5.1.1.右击SSM文件夹,创建新module 5.1.2.选择maven 5.1.3.配置module名称和路径 5.1.4.module初始状态 5.1.5.复制打包 ...
- 高性能MySQL实战(一):表结构
最近因需求改动新增了一些数据库表,但是在定义表结构时,具体列属性的选择有些不知其所以然,索引的添加也有遗漏和不规范的地方,所以我打算为创建一个高性能表的过程以实战的形式写一个专题,以此来学习和巩固这些 ...
- 零代码,使用 Dify 和 Laf 两分钟接入企业微信 AI 机器人
Dify 允许创建 AI 应用,并提供二次开发的能力.这里我将演示创建一个法律问答助手的 AI 应用,称作"知法".在本篇教程中,我将指导你为"知法"接入企业微 ...
- 《Linux基础》06. 进程管理 · 服务管理
@ 目录 1:进程管理 1.1:查看进程 1.1.1:ps 1.1.2:pstree 1.1.3:top 1.2:终止进程 1.2.1:kill 1.2.2:killall 2:服务管理 2.1:运行 ...
- 1.15 自实现GetProcAddress
在正常情况下,要想使用GetProcAddress函数,需要首先调用LoadLibraryA函数获取到kernel32.dll动态链接库的内存地址,接着在调用GetProcAddress函数时传入模块 ...
- Solution -「CSP 2019」Centroid
Description Link. 给定一棵 \(n\) 个点的树,设 \(E\) 为边集,\(V'_x,\ V'_y\) 分别为删去边 \((x,y)\) 后 点 \(x\) 所在的树的点集和点 \ ...
- 整理DB2左补零,右补零的方法
在项目中经常遇到需要左补零,右补零的情况,在DB2实验环境中展示 1.左补零(1)数字左补零,数字长度不定用right(digits(cast(expression as bigint)),NUM)能 ...
- Redis系列之——主从复制原理与优化、缓存的使用和优化
@ 目录 一 什么是主从复制 1.1 原理 1.2 主库是否要开启持久化 1.3 辅助配置(主从数据一致性配置) 二 复制的 配置 2.1 slave 命令 2.2 配置文件 四 故障处理 五 复制常 ...
- 记Halo1.5版本迁移Halo2.10.0版本
原文地址: 记Halo1.5版本迁移Halo2.10.0版本 - Stars-One的杂货小窝 上一篇Window10安装linux子系统及子系统安装1Panel面板 - Stars-One的杂货小窝 ...