首页 > 其他分享 >Count BFS Graph

Count BFS Graph

时间:2024-06-03 15:34:35浏览次数:12  
标签:Count node graph 样例 BFS leq Graph nodes

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题解
数学容斥原理

标签:Count,node,graph,样例,BFS,leq,Graph,nodes
From: https://www.cnblogs.com/gutongxing/p/18228989

相关文章

  • [AIGC] 广度优先搜索(Breadth-First Search,BFS)详解
    广度优先搜索(Breadth-FirstSearch,简称BFS)是一种用于图或者树的搜索算法,它的特点是按照“广度”进行搜索,即在扩展搜索路线的时候,BFS会先考虑当前节点的所有邻近节点,也就是说,它逐层地进行搜索。文章目录基本原理实现方法应用场景总结基本原理广度优先搜索的基本......
  • linux 系统上图形生成错误 java.lang.NoClassDefFoundError: Could not initialize cl
    错误信息:02-Jun-202409:11:09.421SEVERE[Thread-32]org.apache.catalina.core.StandardWrapperValve.invokeServlet.service()forservlet[springDispatcherServlet]incontextwithpath[]threwexception[Handlerdispatchfailed;nestedexceptionisjava.lang.......
  • css40 CSS Counters
    https://www.w3schools.com/css/css_counters.asp CSScountersare"variables"maintainedbyCSSwhosevaluescanbeincrementedbyCSSrules(totrackhowmanytimestheyareused).Countersletyouadjusttheappearanceofcontentbasedonits......
  • ABC 312D题 Count Bracket Sequences
    题意给定一个非空的字符串,其由(,),?三个字符构成,其中?可以被(或者)给替换掉,求替换后的字符串是符合括号匹配的情况下的方案数。最后答案对mod=998244353取模思路应该算是一个板题,一开始的想法是往卡特兰数的方向思考,但是可能是我太水了没想出来,然后一想到卡特兰数的dp求法,就......
  • GraphEdit论文阅读笔记
    GraphEdit:LargeLanguageModelsforGraphStructureLearning论文阅读笔记读一下图结构学习的论文,找找灵感Abstract​ 图结构学习(GSL)侧重于通过生成新的图结构来捕获图结构数据中节点之间的内在依赖性和交互作用。许多现有的GSL方法严重依赖于显式的图结构信息作为监督信......
  • BFS
    BFS通常用于解决最短路问题。一旦题目提到“最短”,就要考虑是否可以用BFS。BFS适用于边权为0或1的问题。如果所有边权都是1,则可以直接用BFS。如果有边权为0,则需用双端BFS,也可用Dijkstra算法。在BFS中,分为单源BFS和多源BFS。单源BFS从一个起点开始,逐层扩展搜索。多源BFS允许同时......
  • echarts渐变内置生成器echarts.graphic.LinearGradient
    在使用echarts绘制图表时,如果需要使用渐变色,则应使用echarts内置的渐变色生成器echarts.graphic.LinearGradientseries:[{name:'',type:'bar',barMaxWidth:20,label:{show:true,color:'#fff',},......
  • open graph 简述
    场景在我们使用twitter的时候,会发现有的链接会显示预览卡片,有的不会。这是因为有的网站设置了opengraph,有的没有。那么什么是opengraph?opengraph是一个由facebook在2010年发布的协议,用于在社交网络上分享链接时,显示预览卡片。我觉得无论是它的名称还是意图,都能看出fac......
  • NodeJs + GraphQl 基于Apollo Server (2)
     基于上一篇的项目继续深入学习和介绍项目中如何使用GraphQl.本篇更侧重于实际项目中使用GraphQL.如果还没有了解基本的GraphQl知识和没用想要新建项目开始演练的可从上一篇开始如何在NodeJs搭建GraphQLserviceApolloServer(1).1.重构app.js和service.mjs 1.1修改s......
  • 测试C#GDI+双缓冲高效绘图--BufferedGraphicsContext
    奥斯卡好的b、测试C#GDI+双缓冲高效绘图```#regionC#GDI+双缓冲高效绘图#regiontemp//Rectanglerectangle=e.ClipRectangle;//取出次窗体或者画布的有效区的矩形区域//BufferedGraphicsContextGraphicsContext=BufferedGraphicsM......