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 NN vertices. Vertex ii has SiS_i edges directed away from that vertex. The jj-th edge of vertex ii that is directed away from it, is directed towards vertex Li,jL_{i,j} and has an integer Wi,jW_{i,j} (0≤Wi,j≤10\leq W_{i,j}\leq1). Another information about the graph is that the graph is shaped in such a way such that each vertex can be reached from vertex 11 via zero or more directed edges.

Pak Chanek has an array ZZ 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 ZZ containing some elements 00 or 11. Define an inversion in array ZZ as a pair of indices (x,y)(x, y) (x<yx \lt y) such that Zx>ZyZ_x \gt Z_y. How many different inversions in ZZ are there if Pak Chanek does dfs(1) once? Since the answer can be very big, output the answer modulo 998 244 353998\,244\,353.

Pak Chanek 有一张包含 NN 个顶点的有向无环图(即不含任何有向环的有向图)。顶点 ii 有 SiS_i 条从该顶点出发的有向边。从顶点 ii 出发的第 jj 条边指向顶点 Li,jL_{i,j},且该边上的整数权值为 Wi,jW_{i,j}(满足 0≤Wi,j≤10 \leq W_{i,j} \leq 1)。关于该图的另一条信息是:图的结构保证每个顶点均可从顶点 11 出发、经零条或若干条有向边到达。

Pak Chanek 拥有一个初始为空的数组 ZZ。

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 将得到一个仅含元素 00 或 11 的数组 ZZ。定义数组 ZZ 中的一个逆序对为一对下标 (x,y)(x, y)(满足 x<yx < y),使得 Zx>ZyZ_x > Z_y。若 Pak Chanek 执行一次 dfs(1),则数组 ZZ 中共有多少个不同的逆序对?由于答案可能非常大,请将结果对 998 244 353998\,244\,353 取模后输出。

输入格式

The first line contains a single integer NN (2≤N≤1052 \leq N \leq 10^5) — the number of vertices in the graph. The following lines contain the description of each vertex from vertex 11 to vertex NN.

The first line of each vertex ii contains a single integer SiS_i (0≤Si≤N−10 \leq S_i \leq N-1) — the number of edges directed away from vertex ii.

The jj-th of the next SiS_i lines of each vertex ii contains two integers Li,jL_{i,j} and Wi,jW_{i,j} (1≤Li,j≤N1 \leq L_{i,j} \leq N; 0≤Wi,j≤10 \leq W_{i,j} \leq 1) — an edge directed away from vertex ii that is directed towards vertex Li,jL_{i,j} and has an integer Wi,jW_{i,j}. For each ii, the values of Li,1L_{i,1}, Li,2L_{i,2}, ..., Li,SiL_{i,S_i} are pairwise distinct.

It is guaranteed that the sum of SiS_i over all vertices does not exceed 2⋅1052 \cdot 10^5. There are no cycles in the graph. Each vertex can be reached from vertex 11 via zero or more directed edges.

第一行包含一个整数 NN(2≤N≤1052 \leq N \leq 10^5)——图中顶点的数量。接下来的若干行依次描述顶点 11 至顶点 NN 的信息。

对于每个顶点 ii,其首行包含一个整数 SiS_i(0≤Si≤N−10 \leq S_i \leq N-1)——从顶点 ii 出发的有向边数量。

随后的 SiS_i 行中,第 jj 行包含两个整数 Li,jL_{i,j} 和 Wi,jW_{i,j}(1≤Li,j≤N1 \leq L_{i,j} \leq N;0≤Wi,j≤10 \leq W_{i,j} \leq 1)——表示一条从顶点 ii 出发、指向顶点 Li,jL_{i,j} 的有向边,其权值为整数 Wi,jW_{i,j}。对每个 ii,Li,1, Li,2, …, Li,SiL_{i,1},\,L_{i,2},\,\dots,\,L_{i,S_i} 两两互不相同。

保证所有顶点的 SiS_i 之和不超过 2⋅1052 \cdot 10^5。图中不存在环。每个顶点均可通过零条或多条有向边从顶点 11 到达。

输出格式

An integer representing the number of inversions in ZZ if Pak Chanek does dfs(1) once. Since the answer can be very big, output the answer modulo 998 244 353998\,244\,353.

一个整数,表示若 Pak Chanek 对节点 1 执行一次 dfs(1) 后,ZZ 中逆序对的个数。由于答案可能非常大,请输出答案对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#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]Z=[0,1,0,1,1,0]. All of its inversions are (2,3)(2,3), (2,6)(2,6), (4,6)(4,6), and (5,6)(5,6).

以下是图上执行 dfs(1) 的过程。

最终,Z=[0,1,0,1,1,0]Z=[0,1,0,1,1,0]。它的所有逆序对为 (2,3)(2,3)、(2,6)(2,6)、(4,6)(4,6) 和 (5,6)(5,6)。

输入解题思路,AI测评打分。不知道怎么写?

首页