Count BFS Graph

辜铜星發表於2024-06-03

Count BFS Graph

題目資訊

Luogu CF1906JCodeforces 1906J

題面翻譯

對於一個 \(n\) 個節點的無向圖的鄰接矩陣 \(M\),滿足 \(M_{i,i}=0,M_{u,v}=M_{v,u}\)\(M_{i,j}=1\) 表示 \(M_{i,j}\) 右邊,進行下面的 bfs 生成 \(A\) 陣列。

BFS():
	清空陣列 A, 清空佇列 Q
        在 A 中加入 1, 在 Q 中加入 1
        while Q 不為空:
            令 u 為 Q 的隊頭, 並彈出對頭
            將 v 從 1 遍歷到 n:
            	if(M[u][v] == 1 並且 v 不在 A 中):
                	Q 中加入 v,在 A 末尾加入 v

給定 \(A\) 陣列,求多少個 \(M\) 滿足條件。

\(1\le n\le5000\)\(A\)\(1\sim n\) 的排列。

答案對 \(998\,244\,353\) 取模。

題目描述

You are currently researching a graph traversal algorithm called the Breadth First Search (BFS). Suppose you have an input graph of $ N $ nodes (numbered from $ 1 $ to $ N $ ). The graph is represented by an adjacency matrix $ M $ , for which node $ u $ can traverse to node $ v $ if $ M_{u, v} $ is $ 1 $ , otherwise it is $ 0 $ . Your algorithm will output the order the nodes are visited in the BFS. The pseudocode of the algorithm is presented as follows.

BFS(M[1..N][1..N]):
let A be an empty array
let Q be an empty queue

append 1 to A
push 1 to Q

while Q is not empty:
pop the front element of Q into u
for v = 1 to N:
if M[u][v] == 1 and v is not in A:
append v to A
push v to Q

return A

During your research, you are interested in the following problem. Given an array $ A $ such that $ A $ is a permutation of $ 1 $ to $ N $ and $ A_1 = 1 $ . How many simple undirected graph with $ N $ nodes and adjacency matrix $ M $ such that $ \text{BFS}(M) = A $ ? Since the answer can be very large, calculate the answer modulo $ 998,244,353 $ .

A simple graph has no self-loop ( $ M_{i, i} = 0 $ for $ 1 \leq i \leq N $ ) and there is at most one edge that connects a pair of nodes. In an undirected graph, if node $ u $ is adjacent to node $ v $ , then node $ v $ is also adjacent to node $ u $ ; formally, $ M_{u, v} = M_{v, u} $ for $ 1 \leq u < v \leq N $ .

Two graphs are considered different if there is an edge that exists in one graph but not the other. In other words, two graphs are considered different if their adjacency matrices are different.

輸入格式

The first line consists of an integer $ N $ ( $ 2 \leq N \leq 5000 $ ).

The second line consists of $ N $ integers $ A_i $ . The array $ A $ is a permutation of $ 1 $ to $ N $ and $ A_1 = 1 $ .

輸出格式

Output an integer representing the number of simple undirected graphs with $ N $ nodes and adjacency matrix $ M $ such that $ \text{BFS}(M) = A $ . Since the answer can be very large, output the answer modulo $ 998,244,353 $ .

樣例 #1

樣例輸入 #1

3
1 2 3

樣例輸出 #1

3

樣例 #2

樣例輸入 #2

3
1 3 2

樣例輸出 #2

1

樣例 #3

樣例輸入 #3

5
1 3 2 4 5

樣例輸出 #3

17

樣例 #4

樣例輸入 #4

11
1 2 3 4 5 6 7 8 9 10 11

樣例輸出 #4

379394847

提示

Explanation for the sample input/output #1

The following illustration shows all graphs that satisfy the requirements.

Explanation for the sample input/output #2

The only graph that satisfies the requirements is a graph with two edges: one that connects nodes $ 1 $ and $ 3 $ , and another one that connects nodes $ 3 $ and $ 2 $ .

tag

Codeforces題解
數學容斥原理

相關文章