简体   繁体   English

关于连通性的图论难题?

[英]Graph theory puzzle about connectivity?

You have a graph of n nodes (numbered 1-n) and m edges.您有一个包含n 个节点(编号为 1-n)和m 条边的图。 You remove all the nodes one by one, and at each step you check if the graph is fully connected or not (by printing "connected" or not).您一个一个地删除所有节点,并在每一步检查图形是否完全连接(通过打印“连接”与否)。 The order of the nodes to be removed is given.给出了要删除的节点的顺序。

For example,例如,

n = 4 and m = 3 The edges given are: 1 - 2, 2 - 3, 3 - 4. The removal order is: 3, 4, 1, 2 n = 4 and m = 3 给出的边为:1 - 2, 2 - 3, 3 - 4. 去除顺序为:3, 4, 1, 2

The nodes are numbered 1-n, so the nodes in this case are 1, 2, 3, 4.节点编号为 1-n,因此本例中的节点为 1、2、3、4。

Initially, the graph is connected, so you print out:最初,图形是连接的,因此您打印出:

Connected连接的

You first remove node 3. Now the graph is disconnected because node 4 is alone.您首先删除节点 3。现在图形断开连接,因为节点 4 是单独的。

Disconnected断开连接

Then you remove node 4. Now the graph is composed of only nodes 1 and 2, which are connected.然后删除节点 4。现在该图仅由连接的节点 1 和 2 组成。

Connected连接的

Then you remove node 1. The graph is still considered connected;然后删除节点 1。该图仍被认为是连通的; there is only one node.只有一个节点。

Connected连接的

Then you remove node 2. There is nothing left.然后删除节点 2。什么都没有留下。

Sample code would be helpful, preferably java or c++.示例代码会有所帮助,最好是 java 或 c++。 I tried using BFS and DFS, but they were too slow.我尝试使用 BFS 和 DFS,但它们太慢了。 What is the most efficient way to do this?执行此操作的最有效方法是什么?

Try working in reverse order.尝试以相反的顺序工作。

Add the edges one by one and use a disjoint set data structure to find when the set becomes connected.一条一条地添加边并使用不相交的集合数据结构来查找集合何时连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM