day52-graph theory-part03-8.23

2024-08-24 07:12
文章标签 graph theory day52 8.23 part03

本文主要是介绍day52-graph theory-part03-8.23,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

tasks for today:

1. 101.孤岛总面积

2. 102.沉默孤岛

3. 103.水流问题

4. 104.建造最大岛屿

--------------------------------------------------------------------------------------

1. 101.孤岛总面积

pay attention to the requirement, it is not calculating the total area, instead, it is required to claculate the islands area which do not have edges touching the sides.

here, we use a boolen variable to track if the area is an island which linked to the edge. This varibale is used to decide if the area would be calculated into the final result.

direction = [[0,1],[0,-1],[1,0],[-1,0]]def dfs(graph, visited, x, y, area, if_touch):if x == 0 or x == len(graph)-1 or y == 0 or y == len(graph[0])-1:if_touch = Truefor dirx, diry in direction:searchx = x + dirxsearchy = y + diryif searchx < 0 or searchx >= len(graph) or searchy < 0 or searchy >= len(graph[0]):continueif graph[searchx][searchy] == 1 and not visited[searchx][searchy]:area += 1visited[searchx][searchy] = Truearea, if_touch = dfs(graph, visited, searchx, searchy, area, if_touch)return area, if_touchdef main():n, m = map(int, input().split())graph = []for i in range(n):graph.append(list(map(int, input().split())))visited = [[False] * m for _ in range(n)]result = 0for i in range(n):for j in range(m):if graph[i][j] == 1 and not visited[i][j]:if_touch = Falsearea = 0area += 1visited[i][j] = Truearea, if_touch = dfs(graph, visited, i, j, area, if_touch)if if_touch == False:result += area# print(i, j, area)print(result)if __name__ == "__main__":main()

2. 102.沉默孤岛

In this practice, because the requirement is doing modification onto the map, so the operation should be done along with the dfs process.

changing these island to 2, preserve the isolated island as 1, then reduce 2 to 1, while 1 to 0

direction = [[0,1],[0,-1],[1,0],[-1,0]]def dfs(graph, x, y):graph[x][y] = 2for dirx, diry in direction:searchx = x + dirxsearchy = y + diryif searchx < 0 or searchx >= len(graph) or searchy < 0 or searchy >= len(graph[0]):continueif graph[searchx][searchy] == 0 or graph[searchx][searchy] == 2:continuedfs(graph, searchx, searchy)returndef main():n, m = map(int, input().split())graph = []for _ in range(n):graph.append(list(map(int, input().split())))for i in range(n):if graph[i][0] == 1: dfs(graph, i, 0)if graph[i][m-1] == 1: dfs(graph, i, m-1)for j in range(m):if graph[0][j] == 1: dfs(graph, 0, j)if graph[n-1][j] == 1: dfs(graph, n-1, j)for i in range(n):for j in range(m):if graph[i][j] == 2: graph[i][j] = 1elif graph[i][j] == 1: graph[i][j] = 0for i in range(n):print(' '.join(list(map(str,graph[i]))))if __name__ == "__main__":main()

3. 103.水流问题

This practice takes a reverse mindset, search from sides, then see which point can be both searched by two kinds of sides.

direction = [[0,1],[0,-1],[1,0],[-1,0]]def dfs(graph, visited, x, y):if visited[x][y]:returnvisited[x][y] = Truefor dirx, diry in direction:searchx = x + dirxsearchy = y + diry# print(x, y, searchx, searchy, len(graph), len(graph[0]))if searchx < 0 or searchx >= len(graph) or searchy < 0 or searchy >= len(graph[0]):continueif graph[searchx][searchy] < graph[x][y]:continuedfs(graph, visited, searchx, searchy)returndef main():n, m = map(int, input().split())graph = []for _ in range(n):graph.append(list(map(int, input().split())))visited_1 = [[False] * m for _ in range(n)]visited_2 = [[False] * m for _ in range(n)]for i in range(n):dfs(graph, visited_1, i, 0)dfs(graph, visited_2, i, m-1)for j in range(m):dfs(graph, visited_1, 0, j)dfs(graph, visited_2, n-1, j)result = []for i in range(n):for j in range(m):if visited_1[i][j] and visited_2[i][j]:result.append([i,j])print(' '.join([str(i), str(j)]))if __name__ == "__main__":main()

4. 104.建造最大岛屿

from collections import defaultdict
direction = [[0,1],[0,-1],[1,0],[-1,0]]def dfs(graph, visited, x, y, area, mark):for dirx, diry in direction:searchx = x + dirxsearchy = y + diryif searchx < 0 or searchy >= len(graph) or searchy < 0 or searchy >= len(graph[0]):continueif graph[searchx][searchy] == 1 and not visited[searchx][searchy]:area += 1graph[searchx][searchy] = markvisited[searchx][searchy] = Truearea = dfs(graph, visited, searchx, searchy, area, mark)return areadef main():n, m = map(int, input().split())graph = []for _ in range(n):graph.append(list(map(int, input().split())))visited = [[False] * m for _ in range(n)]mark = 2gridArea = defaultdict(int)for i in range(n):for j in range(m):if graph[i][j] == 1 and not visited[i][i]:visited[i][j] = Truegraph[i][j] = markarea = 1area = dfs(graph, visited, i, j, area, mark)gridArea[mark] = areamark += 1countIsland = set()result = 0for i in range(n):for j in range(m):if graph[i][j] == 0:newarea = 1for x, y in direction:newx = x + inewy = y + jif graph[newx][newy] in countIsland: continuenewarea += gridArea[graph[newx][newy]]countIsland.add(graph[newx][newy])result = max(result, newarea)print(result)if __name__ == "__main__":main()

这篇关于day52-graph theory-part03-8.23的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1101803

相关文章

图神经网络框架DGL实现Graph Attention Network (GAT)笔记

参考列表: [1]深入理解图注意力机制 [2]DGL官方学习教程一 ——基础操作&消息传递 [3]Cora数据集介绍+python读取 一、DGL实现GAT分类机器学习论文 程序摘自[1],该程序实现了利用图神经网络框架——DGL,实现图注意网络(GAT)。应用demo为对机器学习论文数据集——Cora,对论文所属类别进行分类。(下图摘自[3]) 1. 程序 Ubuntu:18.04

SIGMOD-24概览Part7: Industry Session (Graph Data Management)

👇BG3: A Cost Effective and I/O Efficient Graph Database in ByteDance 🏛机构:字节 ➡️领域: Information systems → Data management systemsStorage management 📚摘要:介绍了字节新提出的ByteGraph 3.0(BG3)模型,用来处理大规模图结构数据 背景

【代码随想录训练营第42期 续Day52打卡 - 图论Part3 - 卡码网 103. 水流问题 104. 建造最大岛屿

目录 一、做题心得 二、题目与题解 题目一:卡码网 103. 水流问题 题目链接 题解:DFS 题目二:卡码网 104. 建造最大岛屿 题目链接 题解:DFS  三、小结 一、做题心得 也是成功补上昨天的打卡了。 这里继续图论章节,还是选择使用 DFS 来解决这类搜索问题(单纯因为我更熟悉 DFS 一点),今天补卡的是水流问题和岛屿问题。个人感觉这一章节题对于刚

A Comprehensive Survey on Graph Neural Networks笔记

一、摘要-Abstract 1、传统的深度学习模型主要处理欧几里得数据(如图像、文本),而图神经网络的出现和发展是为了有效处理和学习非欧几里得域(即图结构数据)的信息。 2、将GNN划分为四类:recurrent GNNs(RecGNN), convolutional GNNs,(GCN), graph autoencoders(GAE), and spatial–temporal GNNs(S

Neighborhood Homophily-based Graph Convolutional Network

#paper/ccfB 推荐指数: #paper/⭐ #pp/图结构学习 流程 重定义同配性指标: N H i k = ∣ N ( i , k , c m a x ) ∣ ∣ N ( i , k ) ∣ with c m a x = arg ⁡ max ⁡ c ∈ [ 1 , C ] ∣ N ( i , k , c ) ∣ NH_i^k=\frac{|\mathcal{N}(i,k,c_{

boost.graph之属性

相关宏 BOOST_INSTALL_PROPERTY #define BOOST_INSTALL_PROPERTY(KIND, NAME) \template <> struct property_kind<KIND##_##NAME##_t> { \typedef KIND##_property_tag type; \} 最终形式为 template <> struct proper

【ZOJ】3874 Permutation Graph 【FFT+CDQ分治】

传送门:【ZOJ】3874 Permutation Graph 题目分析: 容易知道一个个连通块内部的标号都是连续的,否则一定会有另一个连通块向这个连通块建边,或者这个连通块向另一个连通块建边。而且从左到右左边的连通块内最大的标号小于右边连通块内最小的标号。 然后我们可以构造dp方程: dp[n]=n!−i!∗dp[n−i] \qquad \qquad dp[n] = n! - i! *

【HDU】5333 Undirected Graph【LCT+BIT】

传送门:【HDU】5333 Undirected Graph my  code: my~~code: #pragma comment(linker, "/STACK:1024000000")#include <stdio.h>#include <string.h>#include <map>#include <algorithm>using namespace std ;typed

CodeForces 404C Restore Graph

题意: n个点的图  最大度为k  已知从某个点到每个点的距离dis[i]  求  这幅图的边 思路: 告诉了距离  很容易想到dis是从距离为0的那个点开始bfs求出来的 那么复原这幅图的办法就是重新构造这棵bfs形成的树就好了 每层利用点数计算一下是不是违反了最大度k的限制 这里注意  只有dis=0的那个点可以连出k条边  其余的只有k-1条(因为它们还和父亲连着一条边)

学习大数据DAY52 Docker中的Mysql主从配置

Mysql 数据库主从同步 上机练习 1 容器生成命令 压缩包获取镜像 docker image load -i /root/mysql.tar 创建并开启两个镜像: docker run --name mysql1 -d -p 3333:3306 \ -v /opt/mysql1/conf:/etc/mysql/conf.d/