[一起来刷leetcode吧][11]--No.399 Redundant Connection

YOLOv8魔改网络后训练全零?别慌,关掉AMP混合精度训练试试 本文探讨了YOLOv8魔改网络后训练出现全零指标的问题,指出AMP混合精度训练与自定义网络结构的不兼容性。通过分析梯度消失现象和AMP工作原理,提供了关闭AMP、调整学习率等解决方案,并建议预防性开发实践以避免类似问题。 阅读详情
这篇文章是程序自动发表的,详情可以见 这里
href="http://ounix1xcw.bkt.clouddn.com/github.markdown.css" rel="stylesheet">

这是leetcode的第399题--Redundant Connection

  题目 Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.

Example: Given a / b = 2.0, b / c = 3.0. queries are:a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? . return [6.0, 0.5, -1.0, 1.0, -1.0 ].

The input is: vector > equations, vector & values, vector > queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector .

According to the example above:

    equations = [ ["a", "b"], ["b", "c"] ],
    values = [2.0, 3.0],
    queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ].

The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.   思路 这道题由于是除法,会想到有向图,但是发现,a->b, c->b有可以推出a,b关系,所以连通即可。为了能方便搜索,我又增加了一条反向边vuy(代码第161行),当然权值为倒数,可是这样增加了深搜难度,于是要解决环的搜索问题(代码第127行)   

show me the code

from collections import Iterable,deque
class vertex:
    def __init__(self,mark,val=None ,firstEdge = None):
        self.mark = mark
        self.val = val
        self.firstEdge = firstEdge
        self.isVisited = False
    def __str__(self):
        if '0'<=self.mark[0]<='9':return 'v' str(self.mark)
        return str(self.mark)
    def __repr__(self):
        li=[]
        arc= self.firstEdge
        while arc!=None:
            li.append(arc)
            arc= arc.outNextEdge
        return str(self)  '  to:' str([str(i.inArrow) for i in li])
class edge:
    def __init__(self,outArrow,inArrow,outNextEdge = None,inNextEdge = None, weight = 1): 
        self.weight = weight
        self.inNextEdge = inNextEdge 
        self.outNextEdge = outNextEdge
        self.outArrow = outArrow
        self.inArrow=inArrow
        self.isVisited = False
    def __str__(self):
        return '--' str(self.weight) '-->'
    def __repr__(self):
        return str(self)
class graph:
    def __init__(self): 
        self.vertexs = {}
        self.edges = {}
    def __getitem__(self,i): 
        return self.vertexs[i]
    def __setitem__(selfi,x):
        self.vertexs[i]= x
    def __iter__(self):
        return iter(self.vertexs.values())
    def __bool__(self):
        return len(self.vertexs)!=0
    def addVertex(self,vertexs):
        '''vertexs is a iterable or just a mark that marks the vertex,whichc can be every imutable type'''
        if not isinstance(vertexs,Iterable):vertexs=[vertexs]
        for i in vertexs:
            if  not isinstance(i,vertex) and  i not in self.vertexs:self.vertexs[i]= vertex(i)
            if isinstance(i,vertex) and  i not in self.vertexs:self.vertexs[i.mark]= i
    def isConnected(self,v,u):
        v = self.__getVertex(v)
        u = self.__getVertex(u)
        arc= v.firstEdge
        while arc!=None:
            if arc.inArrow==u:return True
            arc = arc.inNextEdge
        return False
    def __getVertex(self,v):
        if not isinstance(v,vertex):
            if v not in self.vertexs:
                self.vertexs[v]=vertex(v)
            return self.vertexs[v]
        return v
    def addEdge(self,v,u,weight = 1):
        v = self.__getVertex(v)
        u = self.__getVertex(u)
        arc = v.firstEdge
        while arc!=None:         #examine that if v,u have been already connected
            if arc.inArrow==u: return
            arc= arc.outNextEdge
        newEdge = edge(v,u,v.firstEdge,u.firstEdge,weight)
        self.edges[(v.mark,u.mark)] = newEdge
        v.firstEdge = newEdge
    def delEdge(self,v,u):
        if not isinstance(v,vertex):v= self.vertexs[v]
        if not isinstance(u,vertex):u= self.vertexs[u]
        self._unrelated(v,u)
        del self.edges[(v.mark,u.mark)]
    def _unrelated(self,v,u):
        if v.firstEdge==None:return 
        if v.firstEdge.inArrow == u:
            v.firstEdge =v.firstEdge.outNextEdge
        else:
            arc = v.firstEdge
            while arc.outNextEdge!=None:
                 if arc.outNextEdge.inArrow ==u:
                     arc.outNextEdge = arc.outNextEdge.outNextEdge
                     break
    def revisit(self):
        for i in self.vertexs:
            self.vertexs[i].isVisited=False
        for i in self.edges:
            self.edges[i].isVisited=False
    def __str__(self):
        arcs= list(self.edges.keys())
        arcs=[str(i[0]) '--->' str(i[1]) '  weight:' str(self.edges[i].weight) for i in arcs]
        s= '\n'.join(arcs)
        return s
    def __repr__(self):
        return str(self)
    def notIn(self,v):
        if (isinstance(v,vertex) and  v.mark not in self.vertexs) or v not in self.vertexs:
            return True
        return False
    def visitPath(self,v,u):
        '''bfs'''
        if self.notIn(v) or  self.notIn(u):
            return  None,None
        v = self.__getVertex(v)
        u = self.__getVertex(u)
        if v.firstEdge==None:return None,None
        q=deque([v.firstEdge])
        isFind=False
        vs,es=[],[]
        while len(q)!=0:
            vs,es=[],[]
            arc= q.popleft()
            if arc.outNextEdge!=None and not arc.outNextEdge.isVisited:q.append(arc.outNextEdge)
            while arc!=None:
                if arc.isVisited:break
                arc.isVisited=True
                es.append(arc)
                vs.append(arc.inArrow)
                arc.outArrow.isVisited=True
                if arc.inArrow==u:
                     isFind=True
                     break
                arc = arc.inArrow.firstEdge
                while arc.inArrow.isVisited and arc.outNextEdge:arc = arc.outNextEdge
            if isFind:break
        else:return None,None
        '''
        se = [str(i) for i in es]
        sv = [str(i)  for i in vs]
        print(str(v),end='')
        for i,j in zip(se,sv):
            print(i,j,end='')
        '''
        return vs,es
    def hasVertex(self,mark):
        return mark in self.vertexs
    def display(self):
        print('vertexs')
        for i in self.vertexs:
            print(self.vertexs[i].__repr__())
        print('edges')
        for i in self.edges:
            arc=self.edges[i]
            print(str(arc.outArrow) str(arc) str(arc.inArrow))

class Solution(object):
    def calcEquation(self, equations, values, queries):
        """
        :type equations: List[List[str]]
        :type values: List[float]
        :type queries: List[List[str]]
        :rtype: List[float]
        """
        rst =[]
        g= graph()
        for edge,wt in zip(equations,values):
            g.addEdge(edge[0],edge[1],wt)
            g.addEdge(edge[1],edge[0],1/wt)###### to serach quickly but sacrifacing some space
        g.display()
        for i in queries:
            if i[0]==i[1]:
                if i[0] in g.vertexs:rst.append(1.0)
                else:rst.append(-1.0)
                continue
            _,path = g.visitPath(i[0],i[1])
            if path==None:
                if not path:rst.append(-1.0)
            else:
                mul = 1
                for i in path:mul*=i.weight
                rst.append(mul)
            g.revisit()
        return rst
【数据集介绍】MVTec 3D AD数据集介绍 MVTec AD(MVTec Anomaly Detection)数据集是用于异常检测的计算机视觉数据集。该数据集包含来自工业生产中不同材料和产品的图像,包括玻璃、塑料、纺织品等。每个类别都包含正常样本和异常样本。这些异常样本包括各种常见的生产缺陷,如划痕、裂纹、凹陷等。MVTec AD数据集旨在帮助研究人员和从业者开发和评估用于检测工业制造中物体缺陷的算法和系统。 阅读详情

相关推荐

openMVS ---dense point cloud(代码理解)

openMVS ---dense point cloud(代码理解)0 代码实现原理1 代码实现步骤1.1 参数传递1.2 loading .mvs数据1.3 稠密重建1.3.0 相关变量及概念1.3.1 ComputeDepthMaps(data) --计算多张深度图1.3.1.1. pre1.3.1.2 为每一张depth map Select view(s) !!!1.3.1.2.1 N neighbors --- global score + filter1.3.1.2.2 MRF ---- t

小rivaaaa的博客 5334

LeetCode 684. Redundant Connection

原题链接在这里:https://leetcode.com/problems/redundant-connection/ 题目: In this problem, a tree is anundirectedgraph that is connected and has no cycles. The given input is a graph that started as a tree...

weixin_30432579的博客 214

从备份到部署:用Clonezilla为网吧/机房批量克隆系统镜像的实战流程

本文详细介绍了使用Clonezilla为网吧/机房批量克隆系统镜像的实战流程,从准备工作到制作黄金镜像,再到批量部署系统镜像的全过程。通过Clonezilla这一开源工具,用户能够高效完成多台计算机的系统部署,确保环境一致性,大幅提升工作效率。文章还提供了优化技巧和常见问题解决方案,帮助用户顺利完成克隆任务。

weixin_34072296的博客 374

LeetCode 684. Redundant Connection [模版]

题解 这题就是并查集。然后我想总结下并查集的写法。 主要实现两块,union &amp; find。 设p[ i ]代表 i 的父类。 find() 主要就是查找i的父类集合 // 无修改 int find_1(int i){ while(p[i]!=i) i=p[i]; return i; } // 压缩路径 int find_2(int i){ if(p[i]!=i) p[i...

蟋蟀的博客 334

JW01二氧化碳传感器实战:5分钟搞定Arduino环境监测系统(附完整代码)

本文详细介绍了如何使用JW01二氧化碳传感器与Arduino快速搭建环境监测系统。通过简单的硬件连接和完整的代码示例,读者可以在5分钟内实现CO2浓度实时监测,并了解数据解析、常见问题解决及进阶应用扩展。JW01传感器的高精度和易用性使其成为创客和嵌入式开发者的理想选择。

kite3的博客 388

SAP S/4 FICO科目辅助余额表开发说明书(包括测试样例、程序代码仅作参考,不保证一定可以运行)

SAP S/4 FICO科目辅助余额表开发说明书(包括测试样例、程序代码仅作参考,不保证一定可以运行)

我好像病了的博客 332

leetcode_684. Redundant Connection

In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, …, N), with one addition

wjheha的博客 264

【软考高项】【计算专题】- 3 - 进度类 - 双代号网络图

共需压缩 20 天。在双代号网络图中,工作一般使用箭线表示,任意一条箭线都需要占用时间、消耗资源,工作名称写在箭线的上方,而消耗的时间则写在箭线的下方或者工作后面。反映的是前后工作的交接点,节点中的编号可以任意编写,但应保证后续工作的节点比前面节点的编号大,即上图图中的1⃣️4⃣️ 即表示 B 活动和 D 活动间有依赖关系,D 活动的开始,依赖 B 活动的结束。B活动完成了一半, 各计划的实际支出为A活动6500元, B活动1000元, C活动280元, D。

欢迎来到我的博客,一起探索代码里的世界! 4580

leetcode 684. Redundant Connection

leetcode 684. Redundant Connection

朝着梦想前行。 1330

[Dify实战]插件编写- 如何让插件直接输出文件对象(支持 TXT、Excel 等)

本文介绍了在Dify插件中实现文件对象返回的方法。通过create_blob_message()函数,开发者可以输出txt、Excel等格式文件,让用户直接下载而非复制文本。文章详细讲解了二进制文件生成的原理,提供了文本翻译和Excel报表生成的代码示例,并列出常见MIME类型对照表。这种技术能显著提升插件实用性,特别适用于翻译、报表生成等需要文件输出的场景,使交互体验从"复制粘贴"升级为"一键下载"。

技术小甜甜:传播可落地技术,帮助个人和企业降低解决真实场景问题的成本 1256

【智能车Code review】—曲率计算、最小二乘法拟合

【1】曲率计算函数 选三个点计算曲率 float process_curvity(byte x1, byte y1, byte x2, byte y2, byte x3, byte y3) { float K; int S_of_ABC = ((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) / 2; //面积的符号表示方向 byte q1 = (byte)((x2 - x1) * (x2 - x1) + (y2 - y1) *

hanhan的博客 1万+

LeetCode 684. Redundant Connection [Medium]

原题地址题目内容题目分析题目的意思大致为,给了一个无向图,让我们删掉组成环的最后一条边。一开始想的做法是想采用拓扑排序来判断是否由环,如果有再删去多余的边。 后面看了leetcode的discuss,发现他用了一个较为简单的方法,union find。建立一个长数组,来存储每个节点,初始化为-1,每加入一条新的边(u,v),都通过find()函数来查找u之前是否有一条到V的路,如果有,那么这条边就是

YuMingJing_的博客 713

深度学习实战(基于pytroch)系列(四十六) 小批量随机梯度下降

小批量随机梯度下降(Mini-batch SGD)是一种介于批量梯度下降和随机梯度下降之间的优化方法。它通过在每次迭代中随机采样一个小批量样本来计算梯度,平衡了计算效率和收敛速度。批量大小是重要超参数:批量较小时并行效率低但梯度估计更随机,批量较大时梯度更稳定但可能含冗余信息。实验表明,不同批量大小会影响训练速度和收敛性,小批量通常能取得较好平衡。此外,学习率衰减策略有助于减小梯度方差。PyTorch提供了简洁的优化器实现方式,使算法应用更加方便。

echo的博客 830

【LeetCode】684. Redundant Connection 解题报告(Python & C++)

【LeetCode】684. Redundant Connection 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/redundant-c...

负雪明烛 2681

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

[LeetCode 684] Redundant Connection

In this problem, a tree is anundirectedgraph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one additi...

ExcitedZhang的博客 210

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

LeetCode-Python-684. 冗余连接

在本问题中, 树指的是一个连通且无环的无向图。 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, ..., N) 的树及一条附加的边构成。附加的边的两个顶点包含在1到N中间,这条附加的边不属于树中已存在的边。 结果图是一个以边组成的二维数组。每一个边的元素是一对[u, v],满足u < v,表示连接顶点u和v的无向图的边。 返回一条可以删去的边,使得结果图是一个...

Keep Coding 911

LeetCode 685 Redundant Connection II (并查集 判树)

In this problem, a rooted tree is adirectedgraph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, excep...

Tc的专栏 490

[LeetCode] 684. Redundant Connection

In this problem, a tree is anundirectedgraph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one a...

weixin_30301183的博客 154

[leetcode]684. Redundant Connection(Python)

[leetcode]684. Redundant Connection题目描述题目理解解题思路并查集Time 题目描述 Category:Medium Tree In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that st...

La_Ragazza的博客 544

leetcode 684. Redundant Connection解法

Example 1: Input: [[1,2], [1,3], [2,3]] Output: [2,3] Explanation: Original tree will be like this: 1 / \ 2 - 3 Example 2: Input: [[1,2], [1,3], [3,1]] Output: [3,1] Explanation: Original t

dong_beijing的博客 1884

【LeetCode】Redundant Connection

In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one addi

sunycccccc的博客 245

LeetCode 684. Redundant Connection 解题报告(python)

684. Redundant Connection Redundant Connection python solution 题目描述 n this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tr...

Orientliu96的博客 430

Redundant Connection——LeetCode进阶路

原题链接https://leetcode.com/problems/redundant-connection/ 题目描述 In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree wit...

Moliay的博客 272

整数划分 -- 思考问题背后的数学原理

今天在 leetcode 做动态规划的题, 做到一道整数划分的题目如下 Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you c...

mbinary 431

[一起来刷leetcode吧][25]--No.542 01 Matrix

这篇文章是程序自动发表的,详情可以见这里 这是leetcode的第542题--01 Matrix &emsp;&emsp;题目 Given a matrix consists of 0 and 1, find the ...

mbinary 346
上一篇: [一起来刷leetcode吧][10]--No.89 Gray Code
下一篇: [一起来刷leetcode吧][12]--No.355 Design Twitter
mbinary
博客等级 码龄9年 39粉丝 77原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址