Count BFS Graph
Time limit2sMemory limit1024 MB
Count the simple undirected graphs on N nodes whose BFS from node 1 visits the nodes in the given order A, modulo 998244353.
- Level
Medium7 of 10
- Topics
- Graph, BFS, Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
You are researching a graph traversal algorithm called Breadth First Search (BFS). Suppose you have a graph with nodes numbered from to . The graph is given as an adjacency matrix , where node can traverse to node if is , and otherwise is . The algorithm outputs the order in which nodes are visited.
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
Given an array that is a permutation of to with , count the simple undirected graphs with nodes whose adjacency matrix satisfies . Output the count modulo .
A simple graph has no self-loops ( for ), and each pair of nodes is connected by at most one edge. In an undirected graph, for . Two graphs are different if their adjacency matrices differ.
Input
The first line contains an integer ().
The second line contains integers . The array is a permutation of to , and .
Output
Print the number of simple undirected graphs with nodes whose adjacency matrix satisfies , modulo .