## Disjoint Set Test

View as PDF

Points: 10
Time limit: 1.4s
Memory limit: 256M

Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Xyene is doing a contest. He comes across the following problem:

You have a weighted graph of vertices labelled from 1 to and edges labelled from 1 to . Each edge has a unique positive weight — specifically, an edge that shows up earlier in the input has a strictly less weight than an edge that shows up later in the input. The graph will not have multiple edges or self loops. From this information, determine the edges in the minimum spanning tree, or that one does not exist. The minimum spanning tree of this graph is guaranteed to be unique if it exists.

Xyene knows that one fast solution uses Kruskal's algorithm with a Disjoint Set data structure. He practices that data structure every day, but still somehow manages to get it wrong. Will you show him a working example?

Xyene recalls that Kruskal's algorithm goes through all the edges one by one sorted by nondecreasing edge weight. An edge will be added to the minimum spanning tree if the two vertices it connects were not connected by any path so far.

#### Input Specification

The first line has and .

The -th line has and , the two vertices that the -th edge connects.

#### Output Specification

If no minimum spanning tree exists, output Disconnected Graph.

Otherwise, output lines: the numbers of the edges that are in the minimum spanning tree in any order.

#### Sample Input 1

4 4
1 2
1 3
2 3
3 4

#### Sample Output 1

1
2
4

#### Sample Input 2

3 1
1 2

#### Sample Output 2

Disconnected Graph

• commented on Dec. 1, 2019, 1:35 p.m.

Prim's algorithm works here too. Fast input is recommended though.

• commented on Aug. 14, 2019, 1:47 p.m.

This comment is hidden due to too much negative feedback. Click here to view it.

• commented on Aug. 14, 2019, 1:47 p.m.

This comment is hidden due to too much negative feedback. Click here to view it.

• commented on July 28, 2020, 3:42 p.m.
• commented on June 8, 2018, 6:34 p.m. edited

This comment is hidden due to too much negative feedback. Click here to view it.

• commented on June 23, 2019, 11:08 p.m.

Well to make logarithmic time TLE would require massive amount of input. So there really isn't a good way of penalizing people for using link-cut trees.

• commented on June 24, 2019, 2:48 p.m. edited

Some simple disjoint set implementations (i.e. Only using union-by-rank optimization) take time per operation

edit: Google Code Jam's problem preparation guide also heavily discourages against trying to differentiate between and in problems

• commented on June 8, 2018, 7:40 p.m.

Well that's certainly one way to solve it, but not the only way (or the intended way).