CSES - Aalto Competitive Programming 2024 - wk6 - Wed - Results
Submission details
Task:Fragile network
Sender:aalto2024g_002
Submission time:2024-10-09 17:44:30 +0300
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.01 sdetails
#2ACCEPTED0.01 sdetails
#3ACCEPTED0.01 sdetails
#40.01 sdetails
#5ACCEPTED0.01 sdetails
#6ACCEPTED0.15 sdetails
#7ACCEPTED0.10 sdetails
#80.15 sdetails
#90.10 sdetails
#10ACCEPTED0.10 sdetails
#11ACCEPTED0.01 sdetails
#12ACCEPTED0.01 sdetails
#13ACCEPTED0.01 sdetails
#14ACCEPTED0.09 sdetails
#150.01 sdetails
#16ACCEPTED0.01 sdetails
#17ACCEPTED0.01 sdetails
#18ACCEPTED0.01 sdetails
#19ACCEPTED0.01 sdetails
#20ACCEPTED0.01 sdetails
#21ACCEPTED0.01 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:65:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |         for(int j=1; j<leaves[leaf_list[i]].size(); j++)
      |                      ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
input/code.cpp:79:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for(int j=1; j<leaves[leaf_list[num_subtrees-1]].size(); j++)
      |                  ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input/code.cpp: In function 'void Test()':
input/code.cpp:28:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     freopen("temp\\in.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
input/code.cpp: In function 'int main(...

Code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=100005;

vector<int> edges[N];
vector<int> leaves[N];

bool DFS(int father, int u)
{
    bool son_flag = true;
    for(auto v: edges[u])
    {
        if(v == father)
            continue;
        if(DFS(u, v))
        {
            // v is a leave
            leaves[u].push_back(v);
        }
        son_flag = false;
    }
    return son_flag;
}

void Test()
{
    freopen("temp\\in.txt", "r", stdin);
}
int main()
{
    // Test();
    int n;
    cin >> n;
    for(int i=1; i<=n-1; i++)
    {
        int a, b;
        cin >> a >> b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }

    int s;
    for(int i=1; i<=n; i++)
        if(edges[i].size() > 1) {
            s = i;
            break;
        }
    DFS(0, s);

    vector<int> leaf_list, hold_list;
    vector<pair<int, int> > ans;
    for(int i=1; i<=n; i++)
    {
        if(leaves[i].empty())
            continue;
        leaf_list.push_back(i);
    }
    int num_subtrees = leaf_list.size();
    // Make all first node connected, then to the hold_list
    for(int i=0; i<num_subtrees-1; i+=2)
        ans.push_back(make_pair(leaves[leaf_list[i]][0], 
                                leaves[leaf_list[i+1]][0]));
    for(int i=0; i<num_subtrees-1; i++)
        for(int j=1; j<leaves[leaf_list[i]].size(); j++)
            hold_list.push_back(leaves[leaf_list[i]][j]);
    if(num_subtrees % 2)
    {
        // A single sub tree can't form
        int tail = leaf_list[num_subtrees-1];
        if(hold_list.empty())
            ans.push_back(make_pair(s, leaves[tail][0]));
        else
        {
            ans.push_back(make_pair(leaves[tail][0], *(hold_list.end()-1)));
            hold_list.pop_back();
        }
    } 
    for(int j=1; j<leaves[leaf_list[num_subtrees-1]].size(); j++)
        hold_list.push_back(leaves[leaf_list[num_subtrees-1]][j]);

    int num_holdleaves = hold_list.size();
    if(num_holdleaves % 2)
        ans.push_back(make_pair(s, hold_list[num_holdleaves-1]));
    for(int i=0; i<num_holdleaves - 1; i+=2)
        ans.push_back(make_pair(hold_list[i], hold_list[i+1]));

    cout << ans.size() << endl;
    for(auto p: ans)
        cout << p.first << " " << p.second <<endl;

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

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

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

user output
5
1 5
7 8
3 4
10 6
...

Test 2

Verdict: ACCEPTED

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

correct output
1
10 1

user output
1
1 10

Test 3

Verdict: ACCEPTED

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

correct output
3
7 10
8 2
1 9

user output
3
8 10
7 9
1 2

Test 4

Verdict:

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

correct output
3
10 8
6 4
5 9

user output
3
5 10
8 9
6 4

Test 5

Verdict: ACCEPTED

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

correct output
3
8 7
10 9
1 6

user output
3
9 8
10 7
1 6

Test 6

Verdict: ACCEPTED

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

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

user output
50000
1 56967
56618 42321
82550 46223
97282 72319
...
Truncated

Test 7

Verdict: ACCEPTED

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

correct output
1
100000 1

user output
1
1 100000

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
25057
70354 39496
33525 14822
55360 23565
72986 53476
...
Truncated

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
489
1 99
667 718
884 2631
1400 1404
...
Truncated

Test 10

Verdict: ACCEPTED

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

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

user output
51
1 140
346 1093
2983 5134
6092 6887
...
Truncated

Test 11

Verdict: ACCEPTED

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

correct output
2
2 6
4 10

user output
2
2 4
6 10

Test 12

Verdict: ACCEPTED

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

correct output
2
4 7
3 6

user output
2
3 6
4 7

Test 13

Verdict: ACCEPTED

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

correct output
2
3 6
2 5

user output
2
2 5
3 6

Test 14

Verdict: ACCEPTED

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

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

user output
16385
2 33
35 39
41 47
49 53
...
Truncated

Test 15

Verdict:

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

correct output
2
9 11
8 10

user output
2
8 9
10 11

Test 16

Verdict: ACCEPTED

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

correct output
2
5 7
4 6

user output
2
4 6
5 7

Test 17

Verdict: ACCEPTED

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

correct output
2
5 7
4 6

user output
2
4 6
5 7

Test 18

Verdict: ACCEPTED

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

correct output
3
8 7
10 9
1 6

user output
3
9 8
10 7
1 6

Test 19

Verdict: ACCEPTED

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

correct output
2
6 7
3 4

user output
2
3 4
6 7

Test 20

Verdict: ACCEPTED

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

correct output
3
4 7
6 8
1 5

user output
3
4 8
1 7
5 6

Test 21

Verdict: ACCEPTED

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

correct output
3
9 8
6 10
3 7

user output
3
3 8
6 10
7 9