You are given an undirected graph consisting of nn vertices and mm edges. Your task is to find the number of connected components which are cycles.
Here are some definitions of graph theory.
An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex aa is connected with a vertex bb, a vertex bb is also connected with a vertex aa). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.
Two vertices uu and vv belong to the same connected component if and only if there is at least one path along edges connecting uu and

给定一个无向图,任务是找到构成环的连通分量的数量。图由顶点和边组成,边是双向的,不允许自环。连通分量是存在路径连接的所有顶点的集合。一个连通分量是环当且仅当其顶点可以重新排列,使得每个顶点与下一个顶点通过边相连,并且所有边都是不同的。题目提供了一个包含66个连通分量,其中2个是环的例子。解决方案涉及计算每个顶点的度,只有度为2的顶点才可能属于环。通过并查集来检查是否有环,若父节点相同,则环的数量加一。
最低0.47元/天 解锁文章
206

被折叠的 条评论
为什么被折叠?



