首页 > 其他分享 >[ABC319G] Counting Shortest Paths

[ABC319G] Counting Shortest Paths

时间:2023-09-10 15:44:31浏览次数:33  
标签:Paths 遍历 短路 ABC319G Counting Shortest

[ABC319G] Counting Shortest Paths

Atcoder:[ABC319G] Counting Shortest Paths

洛谷:[ABC319G] Counting Shortest Paths

Problem

经典问题:求补图的最短路,边权均为 \(1\),并顺带求出 \(1 \to n\) 最短路的数量。\(n \le 2 \times 10^5\)。

Solution

每次从队列中把最早遍历到的节点 \(u\) 取出来,更新所有未被遍历并且与 \(u\) 有边相连的节点 \(v\) 的距离:\(d_v = d_u + 1\),标记 \(v\) 已被遍历,并把 \(v\) 加进队列。由于每个节点只会被遍历到一次,但维护未被遍历的点集以及快速查询 \(u, v\) 是否存在连边需要用到 set,因此求出 \(1\) 到每个点的最短路长度 \(d\) 的时间复杂度是 \(O(n\log{n})\) 的。

求最短路数量就很简单了,记 \(f_u\) 表示 \(1 \to u\) 的最短路数量,按 \(d\) 从小到大遍历点,记 \(s_i\) 表示 \(d\) 数值为 \(i\) 的所有节点的 \(f\) 之和。

则 \(f_u = s_{d_u - 1} - \sum\limits_{v, d_v = d_u - 1, v \text{ is not connected to } u}f_v\)。这部分的时间复杂度也是 \(O(n\log{n})\) 的。

warning:不要同时遍历 set 和删除 set 内的元素。

code ABC319G

标签:Paths,遍历,短路,ABC319G,Counting,Shortest
From: https://www.cnblogs.com/Schucking-Sattin/p/17691337.html

相关文章

  • 1155 Heap Paths
    Incomputerscience,a heap isaspecializedtree-baseddatastructurethatsatisfiestheheapproperty:ifPisaparentnodeofC,thenthekey(thevalue)ofPiseithergreaterthanorequalto(inamaxheap)orlessthanorequalto(inaminheap)......
  • [LeetCode][62]unique-paths
    ContentThereisarobotonanmxngrid.Therobotisinitiallylocatedatthetop-leftcorner(i.e.,grid[0][0]).Therobottriestomovetothebottom-rightcorner(i.e.,grid[m-1][n-1]).Therobotcanonlymoveeitherdownorrightatanypointi......
  • LeetCode[64]MinimumPathSum
    ContentGivenamxngridfilledwithnon-negativenumbers,findapathfromtoplefttobottomright,whichminimizesthesumofallnumbersalongitspath.Note:Youcanonlymoveeitherdownorrightatanypointintime. Example1:Input:grid=[[......
  • LeetCode[62]UniquePaths
    ContentThereisarobotonanmxngrid.Therobotisinitiallylocatedatthetop-leftcorner(i.e.,grid[0][0]).Therobottriestomovetothebottom-rightcorner(i.e.,grid[m-1][n-1]).Therobotcanonlymoveeitherdownorrightatanypointi......
  • Atcoder_[abc284E]Count Simple Paths题解
    题目链接这题就是很简单的图上深搜,我觉得放在E题太水了,代码里有详细注释。#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvector<int>v[200010];//邻接表intans;//答案boolvis[200010];//vis[i]记录i号点有没有被访问过voiddfs(intx)......
  • No_62_UniquePaths
    ContentThereisarobotonanmxngrid.Therobotisinitiallylocatedatthetop-leftcorner(i.e.,grid[0][0]).Therobottriestomovetothebottom-rightcorner(i.e.,grid[m-1][n-1]).Therobotcanonlymoveeitherdownorrightatanypointi......
  • 杭电多校赛第9场1002 Shortest path
    给定一个数字n,每次可以选择一项。令n=n-1令n=n/2,ifn%2==0令n=n/3,ifn%3==0求最少需要多少步可以让其变成1.减1操作可以看作是为了除法做准备,连续超过两次减1再除2是不优的,连续超过三次减1再除2也是不优的。可以根据下一步除2还是除3来分出两个分......
  • next.js 源码解析 - getStaticProps、getStaticPaths 篇
    ......
  • CF938G Shortest Path Queries 题解
    目录题目链接题目分析为什么使用生成树建树对于异或贡献的分析code题目链接CF938G洛谷挂了只能交CF题目分析本题有以下几个关键点:为什么使用生成树建树首先根据\(WC2011\)我们发现可以使用\(dfs\)序来保存节点之间的关系但是我们发现本题目中存在加边删边操作不......
  • UE5 FPaths 路径 使用记录
    相关路径节点获取配置文件路径FStringUBlueprintPathsLibrary::EngineConfigDir(){ returnFPaths::EngineConfigDir();}注意ProjectContentDir函数编辑模式下返回全路径,运行模式下返回相对路径GetProjectContentDirectory函数返回全路径......