zoj 3874 Permutation Graph

Permutation Graph ZOJ - 3874

Edward has a permutation {a1, a2, … an}. He finds that if he connects each pair (ai, aj) such that i < j and ai > aj, he will get a graph.

For example, if the permutation is {2, 3, 1, 4}, then 1 and 2 are connected and 1 and 3 are connected.

Edward lost his permutation, but he does know the connected components of the corresponding graph. He wants to know how many permutations will result in the same connected components.

Note that two vertices u, v belong to the same connected component if there exists a sequence of vertices starting with u and ending with v such that every two subsequent vertices in the sequence are connected by an edge.

Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers n, m (1 ≤ m ≤ n ≤ 100000), indicating the length of the permutation and the number of connected components in the graph.

Each of the following m lines contains an integer ci which denotes the size of i-th connected component, followed by ci distinct integers vi,1, vi,2, … vi,ci which denotes the connected component (1 ≤ ci, vi,1, vi,2, … vi,ci ≤ n).

It is guaranteed that every number will appear in exact one connected component and c1 + c2 + … + cm = n.

Output
For each case, output the answer modulo 786433.

Sample Input
2
4 4
1 1
1 2
1 3
1 4
4 2
3 1 2 3
1 4
Sample Output
1
3
Hint
For the second case, the three permutations is: {2, 3, 1, 4}, {3, 2, 1, 4}, {3, 1, 2, 4}.


【分析】

CDQ分治+NTT

首先脑力YY一下可以发现每一个连通块中的数字一定是连续的!而且每一个连通块在序列中的顺序一定是有序的!所以我们每个连通块的方案数单独考虑,最后π一下就好了。

考虑一个大小 n 的连通块的序列方案数,记为 d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值