CSES - Aalto Competitive Programming 2024 - wk2 - Homework - Results
Submission details
Task:Connect cities
Sender:MallocManfred
Submission time:2024-09-05 23:12:46 +0300
Language:C++ (C++11)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.20 sdetails
#7ACCEPTED0.20 sdetails
#8ACCEPTED0.20 sdetails
#9ACCEPTED0.20 sdetails
#10ACCEPTED0.20 sdetails
#11ACCEPTED0.17 sdetails
#12ACCEPTED0.00 sdetails

Code

#include <iostream>
#include <vector>

std::vector<std::vector<int>> connected_components;

void dfs(int current_node, std::vector<int> (&adj)[], bool (&visited)[]) {
    if (visited[current_node]) return;

    visited[current_node] = true;
    connected_components.back().push_back(current_node);
    for (auto node : adj[current_node]) {
        dfs(node, adj, visited);
    }
}

// find all connected components, then connect them with each other
int main() {
    
    int cities, roads;
    std::cin >> cities >> roads;

    std::vector<int> adj[cities];
    bool visited[cities] = {false};


    for (int i = 0; i < roads; i++) {
        int a, b;
        std::cin >> a >> b;

        a--, b--; // index of roads starts at 1
        adj[a].push_back(b);
        adj[b].push_back(a);

    }

    // std::cout << "----------------" << std::endl;

    // start dfs to find all connected components
    for (int i = 0; i < cities; i++) {
        
        if (!visited[i]) {
            connected_components.push_back(std::vector<int>());
            dfs(i, adj, visited);
        }
    }

    
    // print adjacency list
    // for (int i = 0; i < cities; i++) {
    //     for (int j = 0; j < adj[i].size(); j++) {
    //         std::cout << i+1 << ": " << adj[i][j]+1 << " ";
    //     }
    //     std::cout << std::endl;
    // }

    // print connected components
    // for (const auto& component : connected_components) {
    //     for (const auto& node : component) {
    //         std::cout << node << " ";
    //     }
    //     std::cout << std::endl;
    // }

    // std::cout << "----------------" << std::endl;

    // get road between connected components
    int num_connected_components = connected_components.size();
    if (num_connected_components > 1) {
        std::cout << num_connected_components - 1 << std::endl; // number of needed roads
        // get needed roads
        for (int component = 0; component < num_connected_components - 1; component++) {
            // int last_city = connected_components[component].size() - 1;
            std::cout << connected_components[component][0] + 1 << " " <<  connected_components[component + 1][0] + 1 << std::endl; // correct index again, cities start with index 1
        }
    } else {
        std::cout << "0" << std::endl;
    }

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

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

correct output
2
1 2
2 7

user output
2
1 2
2 7

Test 2

Verdict: ACCEPTED

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

correct output
2
1 4
4 5

user output
2
1 4
4 5

Test 3

Verdict: ACCEPTED

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

correct output
0

user output
0

Test 4

Verdict: ACCEPTED

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

correct output
1
1 3

user output
1
1 3

Test 5

Verdict: ACCEPTED

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

correct output
0

user output
0

Test 6

Verdict: ACCEPTED

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

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

user output
4785
1 2
2 3
3 4
4 5
...
Truncated

Test 7

Verdict: ACCEPTED

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

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

user output
4868
1 2
2 7
7 9
9 15
...
Truncated

Test 8

Verdict: ACCEPTED

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

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

user output
4683
1 9
9 20
20 27
27 28
...
Truncated

Test 9

Verdict: ACCEPTED

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

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

user output
4807
1 6
6 7
7 11
11 12
...
Truncated

Test 10

Verdict: ACCEPTED

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

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

user output
4786
1 2
2 18
18 21
21 27
...
Truncated

Test 11

Verdict: ACCEPTED

input
100000 1
1 2

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

user output
99998
1 3
3 4
4 5
5 6
...
Truncated

Test 12

Verdict: ACCEPTED

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

correct output
2
1 2
2 7

user output
2
1 2
2 7