day56-graph theory-part06-8.26

2024-08-27 17:52
文章标签 graph theory day56 part06 8.26

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

tasks for today:

1. 108.冗余连接

2. 109.冗余连接II

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

1. 108.冗余连接

in this practice, there is a rule needed to be identified: if u & v have been connected to a same root, then if a new couple come up, and isSame is true, there would be a circle showing up, which means this edge should be removed.

def find(u, father):if u == father[u]: return uelse: return find(father[u], father)def isSame(u, v, father):u = find(u, father)v = find(v, father)return u == vdef join(u, v, father):u = find(u, father)v = find(v, father)if u == v: returnfather[v] = udef main():n = int(input())father = list(range(0,n+1))for _ in range(n):s, t = map(int, input().split())if isSame(s, t, father):print(' '.join([str(s),str(t)]))return 0else:join(s, t, father)if __name__ == "__main__":main()

2. 109.冗余连接II

the key difference in this practice is the direct requirement for the edge, the introduce more complicated operation in the main function. 

def find(u, father):if father[u] == u:return uelse:return find(father[u], father)def isSame(u, v, father):u = find(u,father)v = find(v,father)return u == vdef join(u, v, father):u = find(u,father)v = find(v,father)if u == v: returnfather[v] = udef isTreeAfterRemove(edges, deleteedge, father):# print(type(edges))# print(edges)# for i, j in edges:#     print(i,j)for s, t in edges:if s == edges[deleteedge][0] and t == edges[deleteedge][1]: continueif isSame(s, t, father):return False            join(s, t, father)return Truedef getRemoveEdge(edges, father):for s, t in edges:if isSame(s, t, father):print(' '.join([str(s), str(t)]))returnjoin(s, t, father)def main():n = int(input())edges = []isDegree = [0] * (n+1)father = list(range(0, n+1))for _ in range(n):s, t = map(int, input().split())isDegree[t] += 1edges.append([s,t])suspect = []for i in range(n-1, -1, -1):if isDegree[edges[i][1]] == 2:suspect.append(i)# print(type(edges))# isTreeAfterRemove(edges, suspect[0], father)if len(suspect)>0:if isTreeAfterRemove(edges, suspect[0], father):print(' '.join(list(map(str, edges[suspect[0]]))))returnelse:print(' '.join(list(map(str, edges[suspect[1]]))))returngetRemoveEdge(edges, father)if __name__ == "__main__":main()

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



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

相关文章

图神经网络框架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)模型,用来处理大规模图结构数据 背景

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条(因为它们还和父亲连着一条边)

LEAN 类型理论之注解(Annotations of LEAN Type Theory)—— 定义上相等(Definitional Equality)

定义上相等(Definitional Equality)指的是意义上相等,即同义,包括了,定义的缩写(Abbreviatory Definition),alpha转换,相同替代(substituting equals for equals)等。下面是LEAN给定的何谓 定义上相等。          注:罗列的推演规则中,如自明其义的,则不多加解析其前提、结果、或特定注解。

知识图谱(knowledge graph)——概述

知识图谱总结 概念技术链概括通用知识图谱和垂直领域知识图谱国内外开放知识图谱 技术链详解知识获取知识融合知识表示知识推理知识存储 知识图谱构建流程其他挑战跨语言知识抽取跨语言知识链接 思考参考 概念 知识图谱(Knowledge Graph)以结构化的形式描述客观世界中概念、实体及其关系。是融合了认知计算、知识表示与推理、信息检索与抽取、自然语言处理、Web技术、机器学习与大数据