随笔分类 - 图论
摘要:前言 本部分代码的存图方式为 \(\text{vector}\) 存图。 它与标准邻接表差别并不是很大。只是 \(\text{vector}\) 比较好写。 例题 题目链接 题目给我们一个有向图,要求我们求出从一个入度为 \(0\) 的点开始,DFS 和 BFS 的遍历顺序。 请对这个图分别进行 D
阅读全文
摘要:Tarjan B3609 [图论与代数结构 701] 强连通分量 std::vector<int> t[maxn]; //vector 存图 std::vector<int> SCC[maxn]; std::stack<int> stk; int n, m, tot, cnt; int vis[ma
阅读全文
摘要:ll ans[100] ,cnt; //拓扑序列及其元素个数 ll deg[100]; //所有点的入度 void topsort() { queue<ll> q; for (int i = 1; i <= n; ++i) if (deg[i] == 0) //寻找最开始入度就为0的点 q.push
阅读全文
摘要:Kruskal 前置:并查集 struct node { ll u, v, w; } t[200005]; ll fa[200005], n, m, ans, eu, ev, cnt; inline bool cmp(node a, node b) { return a.w < b.w; } inl
阅读全文
摘要:P3379 【模板】最近公共祖先(LCA) 邻接表存图。 struct node{...}; void add(...){} ll dep[500010], fa[500010][23]; ll head[500010], tot; ll n, m, s; ll dep[N], fa[N][25];
阅读全文
摘要:介绍 我们遇到一些有 \(n\) 个元素的集合应用问题中,当给出两个元素的一个无序对 \((a,b)\) 时,需要快速合并 \(a\) 和 \(b\) 分别所在的集合,并查集就是这样的用于处理分离集合的抽象数据类型。它的作用就是动态地维护和处理集合元素之间的复杂关系。 ### 操作 使用并查集应首先
阅读全文
摘要:SPFA SPFA能处理负边权,可以判断负环。也可以求最长路。 最短路 #include <queue> queue<int> q; void SPFA(int s) { fill(dis + 1, dis + 1 + n, 2147483647); //初始边无限大 memset(vis, 0,
阅读全文
摘要:求单源 \(s\) 到任意一点的最短路径。最短路径保存在数组 dis 中。 链式前向星 #include <queue> priority_queue<pair<ll, ll>> q; void dijkstra(int s) { memset(dis, 0x3f, sizeof(dis)); //
阅读全文
摘要:vector 存图 struct node{ ll to, w; }; vector<node> t[maxn]; void add(const int u, const int v, const int w) { t[u].push_back((node){v, w}); } 链式前向星存图 如果
阅读全文
摘要:搜索(深搜回溯与广搜) 1.深搜与回溯 深度优先搜索,简称为深搜或 "DFS" (Depth First Search), 是图运算的一种搜索方式,简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次.大致的搜索过程如下 深度优先遍历图的方法是, 从图中某顶点v出发: (1
阅读全文
摘要:dj变形 求最长路 题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; typedef long long i
阅读全文

浙公网安备 33010602011771号