当前位置:网站首页>HUSTPC2022
HUSTPC2022
2022-07-02 11:55:00 【不是张胖胖】
H. Permutation Counting
思路:
有 n ( 1 ≤ n ≤ 2 ⋅ 1 0 6 ) n(1≤n≤2⋅10^6) n(1≤n≤2⋅106)个数字的全排列 a a a,已知这 n n n个数字间有 m m m个约束 x i , y i x_i,y_i xi,yi,已知 a x i < a y i a_{xi}<a_{yi} axi<ayi,且 x i x_i xi互不相同, y i y_i yi可能相同
问有多少种满足所有约束的可能?对答案模 998244353 998244353 998244353
思路:
根据约束建立有向图,若图中存在环,则认为无解。反之,则一定至少存在一种合理解。
其次,由于题目中 x i x_i xi互不相同,我们可以得知,若将 y i → x i y_i \rightarrow x_i yi→xi连边,则一定会形成森林
为了将森林变为一整颗树,加入额外点 n + 1 n+1 n+1,与所有树的树根连接,于是我们将森林变为了一整颗树
树形 d p dp dp, s z i sz_i szi表示以 i i i为树根的子树节点大小, f i f_i fi表示 s z i sz_i szi个数字在以 i i i为根的子树中,有多少种排列方法。那么
考虑将 10 10 10个不同的石头分成 5 , 3 , 2 5,3,2 5,3,2三堆
有 C 10 5 ⋅ C 5 3 ⋅ C 2 2 C_{10}^{5}⋅C_5^3⋅C_2^2 C105⋅C53⋅C22种可能
那么本题将一颗大小的 10 10 10个节点的树分为 4 , 3 , 2 4,3,2 4,3,2的子树
有 C 9 4 ⋅ C 5 3 ⋅ C 2 2 C_{9}^{4}⋅C_5^3⋅C_2^2 C94⋅C53⋅C22种可能,其中最大的数字一定是根节点。
所以只需要满足根节点最大,剩下的节点任意分即可
/* * @Author: zhangpangpang * @Date: 2022-06-09 09:29:49 * @Last Modified by: zhangpangpang * @Last Modified time: 2022-06-09 09:29:49 */
#include<bits/stdc++.h>
#define fi first
#define gcd __gcd
#define se second
#define pb push_back
#define lowbit(x) x&-x
#define inf 0x3f3f3f3f
#define mem(x,y) memset(x,y,sizeof(x))
#define lrb666 ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
typedef pair<int,int> PII;
const int maxn=2e6+7;
int n,m,fa[maxn],d[maxn];
ll mod=998244353,sz[maxn],f[maxn],fac[maxn],inv[maxn],finv[maxn];
ll ksm(ll b,ll p){
ll r=1;while(p>0){
if(p&1){
r=(r%mod*(b%mod))%mod;}p>>=1;b=(b%mod*(b%mod))%mod;}return r;}
void binom_init(int x) {
fac[0] = fac[1] = 1;
inv[1] = 1;
finv[0] = finv[1] = 1;
for(int i=2; i<x; i++){
fac[i] = fac[i-1]*i%mod;
inv[i] = mod-mod/i*inv[mod%i]%mod;
finv[i] = finv[i-1]*inv[i]%mod;
}
}
ll binom(ll n, ll r){
if(n<r || n<0 || r<0) return 0;
return fac[n]*finv[r]%mod*finv[n-r]%mod;
}
int find(int x)
{
if(x==fa[x])
return fa[x];
else
return fa[x]=find(fa[x]);
}
void lianjie(int x,int y)
{
int px=find(x),py=find(y);
fa[px]=py;
}
vector<int>v[maxn];
void dfs(int u,int fa)
{
sz[u]=1;f[u]=1;
ll use=0;
for(auto nx:v[u])
{
if(u==fa) continue;
dfs(nx,u);
sz[u]+=sz[nx];
}
use++;
for(auto nx:v[u])
{
if(u==fa) continue;
f[u]=(f[u]*f[nx])%mod;
f[u]=(f[u]*binom(sz[u]-use,sz[nx]))%mod;
use+=sz[nx];
}
}
int main()
{
binom_init(maxn-5);
scanf("%d%d",&n,&m);int ok=1;
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++)
{
int a,b;scanf("%d%d",&a,&b);
d[a]++;
v[b].pb(a);
if(find(a)==find(b)) ok=0;
else lianjie(a,b);
}
for(int i=1;i<=n;i++)
{
if(d[i]==0) v[n+1].pb(i);
}
if(!ok)
{
puts("0");return 0;
}
dfs(n+1,-1);
printf("%lld\n",f[n+1]);
}
边栏推荐
- CTO如何帮助业务?
- 用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
- [untitled] leetcode 2321 Maximum score of concatenated array
- 报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
- C RichTextBox controls the maximum number of lines displayed
- taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
- 实现一个多进程并发的服务器
- Ad20 cannot select the solution of component packaging in PCB editor
- 【C语音】详解指针进阶和注意点(2)
- 蜻蜓低代码安全工具平台开发之路
猜你喜欢
[email protected]: The platform “win32“ is incompatible with this module."/>info [email protected]: The platform “win32“ is incompatible with this module.

LeetCode 2310. The number of digits is the sum of integers of K

Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting

Fabric. JS zoom canvas

MathML to latex

A white hole formed by antineutrons produced by particle accelerators

关于网页中的文本选择以及统计选中文本长度
[email protected] : The platform “win32“ is incompatible with this module."/>info [email protected] : The platform “win32“ is incompatible with this module.

Introduction to mathjax (web display of mathematical formulas, vector)

Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
随机推荐
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
btrace-(字节码)动态跟踪工具
info [email protected]: The platform “win32“ is incompatible with this module.
Fundamentals of software testing
C # delay, start the timer in the thread, and obtain the system time
Have you learned the wrong usage of foreach
geoserver离线地图服务搭建和图层发布
LeetCode 2310. The number of digits is the sum of integers of K
C RichTextBox controls the maximum number of lines displayed
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
Xilinx Vivado set *. svh as SystemVerilog Header
【apipost】使用教程
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
LeetCode 2310. 个位数字为 K 的整数之和
3、函数指针和指针函数
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
MQ tutorial | exchange (switch)
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
MFC timer usage
tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口