![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
图算法
枫流仁武
这个作者很懒,什么都没留下…
展开
-
LeetCode LCP 04 覆盖
你有一块棋盘,棋盘上有一些格子已经坏掉了。你还有无穷块大小为1 * 2的多米诺骨牌,你想把这些骨牌不重叠地覆盖在完好的格子上,请找出你最多能在棋盘上放多少块骨牌?这些骨牌可以横着或者竖着放。输入:n, m代表棋盘的大小;broken是一个b * 2的二维数组,其中每个元素代表棋盘上每一个坏掉的格子的位置。输出:一个整数,代表最多能在棋盘上放的骨牌数。from typing import *class Solution: def __init__(self): .原创 2020-11-03 15:17:18 · 358 阅读 · 0 评论 -
LeetCode 797 所有可能的路径
给一个有n个结点的有向无环图,找到所有从0到n-1的路径并输出(不要求按顺序)二维数组的第 i 个数组中的单元都表示有向图中 i 号结点所能到达的下一些结点(译者注:有向图是有方向的,即规定了 a→b 你就不能从 b→a )空就是没有下一个结点了。from typing import *class Solution: def __init__(self): self.res = [] self.visited = None ...原创 2020-11-03 11:53:14 · 245 阅读 · 0 评论 -
LeetCode 1627 带阈值的图连通性
有 n 座城市,编号从 1 到 n 。编号为 x 和 y 的两座城市直接连通的前提是: x 和 y 的公因数中,至少有一个 严格大于 某个阈值 threshold 。更正式地说,如果存在整数 z ,且满足以下所有条件,则编号 x 和 y 的城市之间有一条道路:x % z == 0y % z == 0z > threshold给你两个整数 n 和 threshold ,以及一个待查询数组,请你判断每个查询 queries[i] = [ai, bi] 指向的城市 ai 和 bi 是否连通(即,它原创 2020-11-03 11:51:11 · 243 阅读 · 0 评论 -
LeetCode 1267 统计参与通信的服务器
这里有一幅服务器分布图,服务器的位置标识在m * n的整数矩阵网格grid中,1 表示单元格上有服务器,0 表示没有。如果两台服务器位于同一行或者同一列,我们就认为它们之间可以进行通信。请你统计并返回能够与至少一台其他服务器进行通信的服务器的数量。from typing import *class Solution: def countServers(self, grid: List[List[int]]) -> int: r = len(grid)...原创 2020-10-26 20:59:58 · 209 阅读 · 0 评论 -
LeetCode 1627 带阈值的图连通性
有 n 座城市,编号从 1 到 n 。编号为 x 和 y 的两座城市直接连通的前提是: x 和 y 的公因数中,至少有一个 严格大于 某个阈值 threshold 。更正式地说,如果存在整数 z ,且满足以下所有条件,则编号 x 和 y 的城市之间有一条道路:x % z == 0y % z == 0z > threshold给你两个整数 n 和 threshold ,以及一个待查询数组,请你判断每个查询 queries[i] = [ai, bi] 指向的城市 ai 和 bi 是否连通(即,它原创 2020-10-21 14:56:55 · 139 阅读 · 0 评论 -
LeetCode 886 可能的二分法
给定一组N人(编号为1, 2, ..., N),我们想把每个人分进任意大小的两组。每个人都可能不喜欢其他人,那么他们不应该属于同一组。形式上,如果 dislikes[i] = [a, b],表示不允许将编号为 a 和 b 的人归入同一组。当可以用这种方法将所有人分进两组时,返回 true;否则返回 false。染色法from typing import *from collections import defaultdictclass Solution: def...原创 2020-10-18 07:33:44 · 148 阅读 · 0 评论 -
LeetCode 743 网络延迟问题
有N个网络节点,标记为1到N。给定一个列表times,表示信号经过有向边的传递时间。times[i] = (u, v, w),其中u是源节点,v是目标节点, w是一个信号从源节点传递到目标节点的时间。现在,我们从某个节点K发出一个信号。需要多久才能使所有节点都收到信号?如果不能使所有节点收到信号,返回-1。注意本题的w可以取到0,因此图初始化时节点值得大小不能为0,可以设为-1。采用Dij算法解决from typing import *import sys...原创 2020-10-14 10:36:17 · 159 阅读 · 0 评论 -
LeetCode 310 最小高度树
对于一个具有树特征的无向图,我们可选择任何一个节点作为根。图因此可以成为树,在所有可能的树中,具有最小高度的树被称为最小高度树。给出这样的一个图,写出一个函数找到所有的最小高度树并返回他们的根节点。格式该图包含n个节点,标记为0到n - 1。给定数字n和一个无向边edges列表(每一个边都是一对标签)。你可以假设没有重复的边会出现在edges中。由于所有的边都是无向边, [0, 1]和[1, 0]是相同的,因此不会同时出现在edges里。第一感觉,最小高度树对应...原创 2020-10-13 09:08:07 · 171 阅读 · 0 评论 -
LeetCode 785 判断二分图
给定一个无向图graph,当这个图为二分图时返回true。from typing import *from collections import defaultdictclass Solution: """ 利用染色的方法判断一个图是否是二分图 """ def __init__(self): self.flag = True self.colors = None self.graph = None d原创 2020-09-19 08:24:53 · 94 阅读 · 0 评论 -
LeetCode 685 冗余连接2
在本问题中,有根树指满足以下条件的有向图。该树只有一个根节点,所有其他节点都是该根节点的后继。每一个节点只有一个父节点,除了根节点没有父节点。输入一个有向图,该图由一个有着N个节点 (节点值不重复1, 2, ..., N) 的树及一条附加的边构成。附加的边的两个顶点包含在1到N中间,这条附加的边不属于树中已存在的边。结果图是一个以边组成的二维数组。 每一个边 的元素是一对 [u, v],用以表示有向图中连接顶点 u 和顶点 v 的边,其中 u 是 v 的一个父节点。返回一条能删除的边,使得剩下的原创 2020-09-17 18:36:03 · 94 阅读 · 0 评论 -
LeeoCode 684 冗余连接
在本问题中, 树指的是一个连通且无环的无向图。输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, ..., N) 的树及一条附加的边构成。附加的边的两个顶点包含在1到N中间,这条附加的边不属于树中已存在的边。结果图是一个以边组成的二维数组。每一个边的元素是一对[u, v],满足u < v,表示连接顶点u和v的无向图的边。返回一条可以删去的边,使得结果图是一个有着N个节点的树。如果有多个答案,则返回二维数组中最后出现的边。答案边[u, v] 应满足相同的格式u < ...原创 2020-09-17 18:24:10 · 68 阅读 · 0 评论 -
PAT 1122 Hamiltonian Cycle
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a "Hamiltonian cycle".In this problem, you are supposed to tell if a given cycle ...原创 2019-06-20 19:23:20 · 60 阅读 · 0 评论 -
PAT 1126 Eulerian Path
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the same vertex. They were firs...原创 2019-06-21 11:26:03 · 61 阅读 · 0 评论 -
PAT 1003 Universal Travel Sites
第一次尝试做顶级的试题,虽说代码还有很多不成熟的地方但能够做出来还是挺高兴的.考察的是最大流问题#include <iostream>#include <string>#include <map>#include <vector>#include <stack>#include <climits>#inc...原创 2019-06-11 19:56:17 · 1471 阅读 · 0 评论 -
PAT 1030 Travel Plan
题目:A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest p...原创 2019-08-10 16:32:01 · 62 阅读 · 0 评论 -
PAT 1111 Online Map
题目要求:Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastes...原创 2019-08-10 17:38:59 · 72 阅读 · 0 评论 -
1154 Vertex Coloring
Aproper vertex coloringis a labeling of the graph's vertices with colors such that no two vertices sharing the same edge have the same color. A coloring using at mostkcolors is called a (proper)k...原创 2019-09-02 12:23:20 · 62 阅读 · 0 评论 -
PAT 1139 First Contact
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle in the early years. When a boy A had a crush on a girl B, he would usually not contact her directly i...原创 2019-09-03 16:16:09 · 126 阅读 · 0 评论 -
PAT 1150 Travelling Salesman Problem
旅行商问题需要判断是否是一条路径,是否是一条简单回路.可能判断过程有些冗余...#include <iostream>#include<vector>#include <cstring>#include <climits>#include <unordered_set>using namespace std;in...原创 2019-06-13 20:42:48 · 71 阅读 · 0 评论