A16459 | Count BFS Graph
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
```
<pre class="verbatim"><br></br> BFS(M[1..N][1..N]):<br></br> let A be an empty array<br></br> let Q be an empty queue<br></br><br></br> append 1 to A<br></br> push 1 to Q<br></br><br></br> while Q is not empty:<br></br> pop the front element of Q into u<br></br> for v = 1 to N:<br></br> if M[u][v] == 1 and v is not in A:<br></br> append v to A<br></br> push v to Q<br></br><br></br> return A<br></br>
```
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.
```
<pre class="verbatim"><br></br> BFS(M[1..N][1..N]):<br></br> let A be an empty array<br></br> let Q be an empty queue<br></br><br></br> append 1 to A<br></br> push 1 to Q<br></br><br></br> while Q is not empty:<br></br> pop the front element of Q into u<br></br> for v = 1 to N:<br></br> if M[u][v] == 1 and v is not in A:<br></br> append v to A<br></br> push v to Q<br></br><br></br> return A<br></br>
```
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$ .
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
3 1 2 3
输出 #1
3
输入 #2
3 1 3 2
输出 #2
1
输入 #3
5 1 3 2 4 5
输出 #3
17
输入 #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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted