Submission details
Task:Fragile network
Sender:aalto26fw_007
Submission time:2026-10-07 17:49:31 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#10.88 sdetails
#2--details
#3--details
#4--details
#5--details
#60.75 sdetails
#7--details
#80.93 sdetails
#9--details
#10--details
#11--details
#12--details
#13--details
#14--details
#15--details
#16--details
#17--details
#18--details
#19--details
#20--details
#21--details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:36:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for (int i = 0; i < leafs.size() / 2; ++i) {
      |                     ~~^~~~~~~~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>

int main() {
    int n;
    std::cin >> n;

    std::vector<std::vector<int>> edges(n);
    for (int i = 1; i < n; ++i) {
        int a, b;
        std::cin >> a >> b;
        edges[a-1].push_back(b-1);
        edges[b-1].push_back(a-1);
    }

    std::vector<int> leafs;

    std::stack<int> stack;
    stack.push(0);

    while(!stack.empty()) {
        int node = stack.top();
        stack.pop();

        if (edges[node].size() == 1) leafs.push_back(node);

        for (auto new_node : edges[node]) {
            if (node == new_node) continue;
            stack.push(new_node);
        }
    }

    std::cout << (leafs.size()+1) / 2 << std::endl;
    for (int i = 0; i < leafs.size() / 2; ++i) {
        std::cout << leafs[i] << " " << leafs[leafs.size()-1-i] << std::endl;
    }
    if (leafs.size() % 2 == 1) std::cout << leafs[0] << leafs[leafs.size()/2];
}

Test details

Test 1

Verdict:

input
10
1 5
1 7
1 8
1 3
...

correct output
5
5 2
7 9
8 6
3 10
...

user output
(empty)

Test 2

Verdict:

input
10
4 5
3 4
2 3
9 10
...

correct output
1
10 1

user output
(empty)

Test 3

Verdict:

input
10
1 8
1 3
3 5
5 7
...

correct output
3
7 10
8 2
1 9

user output
(empty)

Test 4

Verdict:

input
10
1 5
3 7
2 10
3 8
...

correct output
3
10 8
6 4
5 9

user output
(empty)

Test 5

Verdict:

input
10
4 8
3 4
4 6
2 3
...

correct output
3
8 7
10 9
1 6

user output
(empty)

Test 6

Verdict:

input
100000
1 56967
1 56618
1 42321
1 82550
...

correct output
50000
56967 16911
56618 39942
42321 99902
82550 2538
...

user output
(empty)

Test 7

Verdict:

input
100000
92297 92298
23511 23512
68057 68058
65434 65435
...

correct output
1
100000 1

user output
(empty)

Test 8

Verdict:

input
100000
17747 97512
10397 12053
679 6975
4013 14565
...

correct output
25057
92881 76094
20353 87429
16069 96487
71186 52809
...

user output
(empty)

Test 9

Verdict:

input
100000
72941 72942
11232 11233
73464 73465
30042 30043
...

correct output
489
16423 85168
20707 94190
36505 54940
96411 44067
...

user output
(empty)

Test 10

Verdict:

input
100000
31451 31452
7473 7474
24056 24057
85181 85182
...

correct output
51
25638 2983
87594 87371
92001 50610
46744 100000
...

user output
(empty)

Test 11

Verdict:

input
10
1 2
1 3
3 4
3 5
...

correct output
2
2 6
4 10

user output
(empty)

Test 12

Verdict:

input
7
1 2
2 3
2 4
1 5
...

correct output
2
4 7
3 6

user output
(empty)

Test 13

Verdict:

input
6
1 2
1 3
1 4
4 5
...

correct output
2
3 6
2 5

user output
(empty)

Test 14

Verdict:

input
65538
1 2
1 3
1 4
3 5
...

correct output
16385
34 36
40 42
35 41
48 50
...

user output
(empty)

Test 15

Verdict:

input
11
1 2
1 3
2 4
2 5
...

correct output
2
9 11
8 10

user output
(empty)

Test 16

Verdict:

input
7
1 2
1 3
2 4
2 5
...

correct output
2
5 7
4 6

user output
(empty)

Test 17

Verdict:

input
7
1 2
1 3
2 4
2 5
...

correct output
2
5 7
4 6

user output
(empty)

Test 18

Verdict:

input
10
8 4
3 4
4 6
2 3
...

correct output
3
8 7
10 9
1 6

user output
(empty)

Test 19

Verdict:

input
7
1 2
1 5
2 3
2 6
...

correct output
2
6 7
3 4

user output
(empty)

Test 20

Verdict:

input
8
1 2
1 3
2 4
2 5
...

correct output
3
4 7
6 8
1 5

user output
(empty)

Test 21

Verdict:

input
10
2 1
3 1
4 2
5 4
...

correct output
3
9 8
6 10
3 7

user output
(empty)