Submission details
Task:Connect cities
Sender:discape
Submission time:2025-09-11 02:43:00 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.07 sdetails
#20.07 sdetails
#30.07 sdetails
#40.07 sdetails
#50.07 sdetails
#60.68 sdetails
#70.68 sdetails
#80.68 sdetails
#90.68 sdetails
#100.68 sdetails
#110.07 sdetails
#120.07 sdetails

Code

import sys

dbg = 0


# strict int
def sint(x):
    if isinstance(x, str):
        return int(x)
    if x.is_integer():
        return int(x)
    raise ValueError


def ep(*args, **kwargs):
    if dbg:
        print(*args, file=sys.stderr, **kwargs)


def p(**kwargs):
    ep(", ".join([f"{k} = {v}" for (k, v) in kwargs.items()]))


# n = sint(input())
# p_n = [sint(h) for i, h in enumerate(input().split(" "))]
# p(n=n, p_n=p_n)
[n, m] = [int(x) for x in input().split(" ")]
p(n=n, m=m)
roads = set([frozenset([sint(x) for x in input().split(" ")]) for i in range(m)])
p(roads=roads)


# we can build a list of islands and then connect the islands together one by one
# the number of required roads is always islands-1
#
# we can loop through all the... roads? cities? both?
islands = set[frozenset]([frozenset([x + 1]) for x in range(n)])
for r in roads:
    [c1, c2] = r
    ild_w_c1 = next((x for x in islands if c1 in x))
    ild_w_c2 = next((x for x in islands if c2 in x))
    # if neither of the cities are in the islands set
    # if ild_w_c1 is None and ild_w_c2 is None:
    #     ep("adding new island", [c1, c2])
    #     islands.add(frozenset([c1, c2]))
    if ild_w_c1 is ild_w_c2:
        continue
    # elif ild_w_c1 is None:
    #     assert ild_w_c2 is not None
    #     islands.remove(ild_w_c2)
    #     ep("adding city", c1, "to island", ild_w_c2)
    #     islands.add(ild_w_c2.union([c1]))
    # elif ild_w_c2 is None:
    #     assert ild_w_c1 is not None
    #     islands.remove(ild_w_c1)
    #     ep("adding city", c2, "to island", ild_w_c1)
    #     islands.add(ild_w_c1.union([c2]))
    else:
        islands.remove(ild_w_c1)
        islands.remove(ild_w_c2)
        ep("connecting", ild_w_c1, "with", ild_w_c2)
        islands.add(ild_w_c1.union(ild_w_c2))
p(islands=islands)

hub = islands.pop()
hub_city = next(iter(hub))
new_roads = set()
for i in islands:
    gate_city = next(iter(i))
    new_roads.add((hub_city, gate_city))

print(len(new_roads))
for n in new_roads:
    print(*n)

# new_roads = set()
# for c1 in range(n):
#     for c2 in range(n):
#         if c1 == c2:
#             continue
#         road = frozenset([c1 + 1, c2 + 1])
#         p(road=road)
#         if road not in roads:
#             new_roads.add(road)
# print(len(new_roads) + len(roads))
# for r in new_roads:
#     print(*r)

Test details

Test 1

Verdict:

input
10 10
2 5
5 6
1 4
6 8
...

correct output
2
1 2
2 7

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 37, in <module>
    island...

Test 2

Verdict:

input
10 10
3 9
6 8
9 10
7 8
...

correct output
2
1 4
4 5

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 37, in <module>
    island...

Test 3

Verdict:

input
10 10
7 9
1 7
1 3
3 4
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 37, in <module>
    island...

Test 4

Verdict:

input
10 10
4 8
5 9
4 9
2 7
...

correct output
1
1 3

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 37, in <module>
    island...

Test 5

Verdict:

input
10 10
4 9
2 4
7 10
1 8
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 37, in <module>
    island...

Test 6

Verdict:

input
100000 200000
7233 22146
94937 96203
6133 10731
98737 99193
...

correct output
4785
1 2
2 3
3 4
4 5
...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 37, in <module>
    island...

Test 7

Verdict:

input
100000 200000
92950 93575
24401 88897
41796 99364
47106 50330
...

correct output
4868
1 2
2 7
7 9
9 15
...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 37, in <module>
    island...

Test 8

Verdict:

input
100000 200000
15637 76736
79169 98809
4382 86557
73383 77029
...

correct output
4683
1 9
9 20
20 27
27 28
...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 37, in <module>
    island...

Test 9

Verdict:

input
100000 200000
47932 66981
86401 99942
4353 27841
60492 67345
...

correct output
4807
1 6
6 7
7 11
11 12
...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 37, in <module>
    island...

Test 10

Verdict:

input
100000 200000
6554 44548
76413 98555
5447 59589
70166 74434
...

correct output
4786
1 2
2 18
18 21
21 27
...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 37, in <module>
    island...

Test 11

Verdict:

input
100000 1
1 2

correct output
99998
1 3
3 4
4 5
5 6
...

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 37, in <module>
    island...

Test 12

Verdict:

input
10 9
2 5
5 6
1 4
6 8
...

correct output
2
1 2
2 7

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 37, in <module>
    island...