300879: CF167E. Wizards and Bets

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Wizards and Bets

题意翻译

给定n个点m条边的有向无环图,其中没有入度的点被视为源点,没有出度的点被视为汇点。 保证源点和汇点数目相同。 考虑所有把源汇点两两配对,并用两两不相交的路径把它们两两连接起来的所有方案。 如果这个方案中,把源点按标号1到n排序后,得到的对应汇点序列的逆序数对的个数是奇数,那么A给B一块钱,否则B给A一块钱。 问最后A的收益,对大质数取模。 n ≤ 600

题目描述

In some country live wizards. They like to make weird bets. Two wizards draw an acyclic directed graph with $ n $ vertices and $ m $ edges (the graph's vertices are numbered from $ 1 $ to $ n $ ). A source is a vertex with no incoming edges, and a sink is the vertex with no outgoing edges. Note that a vertex could be the sink and the source simultaneously. In the wizards' graph the number of the sinks and the sources is the same. Wizards numbered the sources in the order of increasing numbers of the vertices from $ 1 $ to $ k $ . The sinks are numbered from $ 1 $ to $ k $ in the similar way. To make a bet, they, as are real wizards, cast a spell, which selects a set of $ k $ paths from all sources to the sinks in such a way that no two paths intersect at the vertices. In this case, each sink has exactly one path going to it from exactly one source. Let's suppose that the $ i $ -th sink has a path going to it from the $ a_{i} $ 's source. Then let's call pair $ (i,j) $ an inversion if $ i&lt;j $ and $ a_{i}&gt;a_{j} $ . If the number of inversions among all possible pairs $ (i,j) $ , such that $ (1<=i&lt;j<=k) $ , is even, then the first wizard wins (the second one gives him one magic coin). Otherwise, the second wizard wins (he gets one magic coin from the first one). Our wizards are captured with feverish excitement, so they kept choosing new paths again and again for so long that eventually they have chosen every possible set of paths for exactly once. The two sets of non-intersecting pathes are considered to be different, if and only if there is an edge, which lies at some path in one set and doesn't lie at any path of another set. To check their notes, they asked you to count the total winnings of the first player for all possible sets of paths modulo a prime number $ p $ .

输入输出格式

输入格式


The first line contains three space-separated integers $ n $ , $ m $ , $ p $ ( $ 1<=n<=600 $ , $ 0<=m<=10^{5} $ , $ 2<=p<=10^{9}+7 $ ). It is guaranteed that $ p $ is prime number. Next $ m $ lines contain edges of the graph. Each line contains a pair of space-separated integers, $ a_{i} b_{i} $ — an edge from vertex $ a_{i} $ to vertex $ b_{i} $ . It is guaranteed that the graph is acyclic and that the graph contains the same number of sources and sinks. Please note that the graph can have multiple edges.

输出格式


Print the answer to the problem — the total winnings of the first player modulo a prime number $ p $ . Please note that the winnings may be negative, but the modulo residue must be non-negative (see the sample).

输入输出样例

输入样例 #1

4 2 1000003
1 3
2 4

输出样例 #1

1

输入样例 #2

4 2 1000003
4 1
3 2

输出样例 #2

1000002

输入样例 #3

4 4 1000003
2 1
2 4
3 1
3 4

输出样例 #3

0

输入样例 #4

6 5 1000003
1 4
1 5
1 6
2 6
3 6

输出样例 #4

0

输入样例 #5

5 2 1000003
5 1
3 4

输出样例 #5

1

说明

In the first sample, there is exactly one set of paths — ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF167E/f318518b6ffb008e02124abd4cb76a626deeb5fb.png). The number of inversions is 0, which is an even number. Therefore, the first wizard gets 1 coin. In the second sample there is exactly one set of paths — ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF167E/6d283e8aeff0ca7a9e663e172f1adbbc15545055.png). There is exactly one inversion. Therefore, the first wizard gets -1 coin. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF167E/ca976762e569d31f793b3b6521d252e9d79dd896.png). In the third sample, there are two sets of paths, which are counted with opposite signs. In the fourth sample there are no set of paths at all. In the fifth sample, there are three sources — the vertices with the numbers (2, 3, 5) and three sinks — the vertices with numbers (1, 2, 4). For a single set of paths ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF167E/05f1743a91c88656033911310c3c49d2ecb6ab74.png) are 2 inversions, that is, their number is even.

Input

题意翻译

给定n个点m条边的有向无环图,其中没有入度的点被视为源点,没有出度的点被视为汇点。 保证源点和汇点数目相同。 考虑所有把源汇点两两配对,并用两两不相交的路径把它们两两连接起来的所有方案。 如果这个方案中,把源点按标号1到n排序后,得到的对应汇点序列的逆序数对的个数是奇数,那么A给B一块钱,否则B给A一块钱。 问最后A的收益,对大质数取模。 n ≤ 600

加入题单

算法标签: