CF1866C.Completely Searching for Inversions
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Pak Chanek has a directed acyclic graph (a directed graph that does not have any cycles) containing N vertices. Vertex i has Si edges directed away from that vertex. The j-th edge of vertex i that is directed away from it, is directed towards vertex Li,j and has an integer Wi,j (0≤Wi,j≤1). Another information about the graph is that the graph is shaped in such a way such that each vertex can be reached from vertex 1 via zero or more directed edges.
Pak Chanek has an array Z that is initially empty.
Pak Chanek defines the function dfs as follows:
// dfs from vertex ivoid dfs(int i) { // iterate each edge of vertex i that is directed away from it for(int j = 1; j <= S[i]; j++) { Z.push_back(W[i][j]); // add the integer in the edge to the end of Z dfs(L[i][j]); // recurse to the next vertex }}
Note that the function does not keep track of which vertices have been visited, so each vertex can be processed more than once.
Let's say Pak Chanek does dfs(1) once. After that, Pak Chanek will get an array Z containing some elements 0 or 1. Define an inversion in array Z as a pair of indices (x,y) (x<y) such that Zx>Zy. How many different inversions in Z are there if Pak Chanek does dfs(1) once? Since the answer can be very big, output the answer modulo 998244353.
Pak Chanek 有一张包含 N 个顶点的有向无环图(即不含任何有向环的有向图)。顶点 i 有 Si 条从该顶点出发的有向边。从顶点 i 出发的第 j 条边指向顶点 Li,j,且该边上的整数权值为 Wi,j(满足 0≤Wi,j≤1)。关于该图的另一条信息是:图的结构保证每个顶点均可从顶点 1 出发、经零条或若干条有向边到达。
Pak Chanek 拥有一个初始为空的数组 Z。
Pak Chanek 定义如下函数 dfs:
// 从顶点 i 开始执行 dfs
void dfs(int i) {
// 遍历从顶点 i 出发的每一条边
for(int j = 1; j <= S[i]; j++) {
Z.push_back(W[i][j]); // 将该边上的整数值添加至数组 Z 末尾
dfs(L[i][j]); // 递归访问下一个顶点
}
}
注意:该函数不记录哪些顶点已被访问过,因此每个顶点可能被处理多次。
假设 Pak Chanek 执行一次 dfs(1)。执行完毕后,Pak Chanek 将得到一个仅含元素 0 或 1 的数组 Z。定义数组 Z 中的一个逆序对为一对下标 (x,y)(满足 x<y),使得 Zx>Zy。若 Pak Chanek 执行一次 dfs(1),则数组 Z 中共有多少个不同的逆序对?由于答案可能非常大,请将结果对 998244353 取模后输出。
输入格式
The first line contains a single integer N (2≤N≤105) — the number of vertices in the graph. The following lines contain the description of each vertex from vertex 1 to vertex N.
The first line of each vertex i contains a single integer Si (0≤Si≤N−1) — the number of edges directed away from vertex i.
The j-th of the next Si lines of each vertex i contains two integers Li,j and Wi,j (1≤Li,j≤N; 0≤Wi,j≤1) — an edge directed away from vertex i that is directed towards vertex Li,j and has an integer Wi,j. For each i, the values of Li,1, Li,2, ..., Li,Si are pairwise distinct.
It is guaranteed that the sum of Si over all vertices does not exceed 2⋅105. There are no cycles in the graph. Each vertex can be reached from vertex 1 via zero or more directed edges.
第一行包含一个整数 N(2≤N≤105)——图中顶点的数量。接下来的若干行依次描述顶点 1 至顶点 N 的信息。
对于每个顶点 i,其首行包含一个整数 Si(0≤Si≤N−1)——从顶点 i 出发的有向边数量。
随后的 Si 行中,第 j 行包含两个整数 Li,j 和 Wi,j(1≤Li,j≤N;0≤Wi,j≤1)——表示一条从顶点 i 出发、指向顶点 Li,j 的有向边,其权值为整数 Wi,j。对每个 i,Li,1,Li,2,…,Li,Si 两两互不相同。
保证所有顶点的 Si 之和不超过 2⋅105。图中不存在环。每个顶点均可通过零条或多条有向边从顶点 1 到达。
输出格式
An integer representing the number of inversions in Z if Pak Chanek does dfs(1) once. Since the answer can be very big, output the answer modulo 998244353.
一个整数,表示若 Pak Chanek 对节点 1 执行一次 dfs(1) 后,Z 中逆序对的个数。由于答案可能非常大,请输出答案对 998244353 取模的结果。
输入输出样例
输入#1
5 2 4 0 3 1 0 1 2 0 2 3 1 5 1 0
输出#1
4
说明/提示
The following is the dfs(1) process on the graph.

In the end, Z=[0,1,0,1,1,0]. All of its inversions are (2,3), (2,6), (4,6), and (5,6).
以下是图上执行 dfs(1) 的过程。

最终,Z=[0,1,0,1,1,0]。它的所有逆序对为 (2,3)、(2,6)、(4,6) 和 (5,6)。
输入解题思路,AI测评打分。不知道怎么写?