CSES - Aalto Competitive Programming 2024 - wk7 - Mon - Results
Submission details
Task:Road blockade
Sender:Farah
Submission time:2024-10-21 17:45:05 +0300
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#80.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#160.00 sdetails
#17ACCEPTED0.00 sdetails
#18ACCEPTED0.00 sdetails
#19ACCEPTED0.00 sdetails
#20ACCEPTED0.00 sdetails
#21ACCEPTED0.00 sdetails
#220.00 sdetails
#230.00 sdetails
#24ACCEPTED0.00 sdetails
#250.00 sdetails
#260.00 sdetails
#27ACCEPTED0.00 sdetails
#28ACCEPTED0.00 sdetails
#29ACCEPTED0.00 sdetails
#300.00 sdetails
#310.00 sdetails
#32ACCEPTED0.00 sdetails
#33ACCEPTED0.00 sdetails
#340.00 sdetails
#35ACCEPTED0.00 sdetails
#360.00 sdetails
#37ACCEPTED0.00 sdetails
#38ACCEPTED0.00 sdetails
#390.00 sdetails
#400.00 sdetails
#410.00 sdetails
#42ACCEPTED0.00 sdetails
#43ACCEPTED0.00 sdetails
#440.00 sdetails
#450.00 sdetails
#46ACCEPTED0.00 sdetails
#470.00 sdetails
#48ACCEPTED0.00 sdetails
#490.00 sdetails
#500.00 sdetails
#510.00 sdetails
#52ACCEPTED0.00 sdetails
#53ACCEPTED0.00 sdetails
#540.00 sdetails
#55ACCEPTED0.00 sdetails
#560.00 sdetails
#570.00 sdetails
#58ACCEPTED0.00 sdetails
#590.00 sdetails
#600.10 sdetails
#610.09 sdetails
#62ACCEPTED0.12 sdetails
#63ACCEPTED0.12 sdetails
#640.09 sdetails
#650.10 sdetails
#660.09 sdetails
#670.09 sdetails
#68ACCEPTED0.14 sdetails
#690.09 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:100:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  100 |         for(auto &[v, bridge_idx] : bridge_tree[u]){
      |                   ^

Code

#include <bits/stdc++.h>
using namespace std;


struct Edge {
    int to;
    int index;
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    int n, m;
    cin >> n >> m;
    

    vector<vector<Edge>> adj(n+1, vector<Edge>());

    vector<pair<int, int>> edges(m+1); 
    
    for(int i=1; i<=m; ++i){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(Edge{b, i});
        adj[b].push_back(Edge{a, i});
        edges[i] = {a, b};
    }
    
    vector<int> tin(n+1, -1), low(n+1, -1);
    int timer = 0;
    vector<bool> is_bridge(m+1, false);
   
    function<void(int, int)> dfs = [&](int u, int parent_edge) {
        tin[u] = low[u] = ++timer;
        for(auto &edge: adj[u]){
            int v = edge.to;
            int idx = edge.index;
            if(idx == parent_edge) continue;
            if(tin[v] == -1){
                dfs(v, idx);
                low[u] = min(low[u], low[v]);
                if(low[v] > tin[u]){
                    is_bridge[idx] = true;
                }
            }
            else{
                low[u] = min(low[u], tin[v]);
            }
        }
    };
   
    dfs(1, -1);
    
    vector<int> component(n+1, -1);
    int current_component = 0;

    function<void(int)> assign_component = [&](int u) {
        component[u] = current_component;
        for(auto &edge: adj[u]){
            int v = edge.to;
            int idx = edge.index;
            if(component[v] == -1 && !is_bridge[idx]){
                assign_component(v);
            }
        }
    };
    
    for(int u=1; u<=n; ++u){
        if(component[u] == -1){
            assign_component(u);
            current_component++;
        }
    }

    int bridge_tree_size = current_component;
    vector<vector<pair<int, int>>> bridge_tree(bridge_tree_size, vector<pair<int, int>>());
    vector<int> bridge_roads;
    
    for(int i=1; i<=m; ++i){
        if(is_bridge[i]){
            int a = edges[i].first;
            int b = edges[i].second;
            int comp_a = component[a];
            int comp_b = component[b];
            bridge_tree[comp_a].emplace_back(comp_b, i);
            bridge_tree[comp_b].emplace_back(comp_a, i);
        }
    }
    
    int comp1 = component[1];
    int compn = component[n];

    queue<int> q;
    vector<int> parent_bridge(bridge_tree_size, -1); 
    q.push(comp1);
    
    while(!q.empty()){
        int u = q.front(); q.pop();
        for(auto &[v, bridge_idx] : bridge_tree[u]){
            if(parent_bridge[v] == -1 && v != comp1){
                parent_bridge[v] = bridge_idx;
                q.push(v);
                if(v == compn){
                    break;
                }
            }
        }
    }

    vector<int> S;
    if(comp1 != compn){
        int current = compn;
        while(current != comp1){
            int bridge_idx = parent_bridge[current];
            if(bridge_idx == -1){
                break; 
            }
            S.push_back(bridge_idx);
    
            int a = edges[bridge_idx].first;
            int b = edges[bridge_idx].second;
            int other_comp = (component[a] == current) ? component[b] : component[a];
            current = other_comp;
        }
    }
    

    if(S.empty()){
        cout << "0\n";
        return 0;
    }
    
    if(S.size() ==1){
        cout << S[0] << "\n";
    }
    else{
        bool all_connected_to_n = true;
        int min_road_not_connected = INT32_MAX;
        for(auto &road_idx: S){
            int a = edges[road_idx].first;
            int b = edges[road_idx].second;
            if(a != n && b != n){
                all_connected_to_n = false;
                min_road_not_connected = min(min_road_not_connected, road_idx);
            }
        }
        if(all_connected_to_n){
            cout << "0\n";
        }
        else{
            cout << min_road_not_connected << "\n";
        }
    }
}

Test details

Test 1

Verdict: ACCEPTED

input
2 1
1 2

correct output
1

user output
1

Test 2

Verdict: ACCEPTED

input
3 2
1 2
2 3

correct output
1

user output
1

Test 3

Verdict: ACCEPTED

input
3 2
2 3
1 2

correct output
2

user output
2

Test 4

Verdict: ACCEPTED

input
3 3
1 2
1 3
2 3

correct output
0

user output
0

Test 5

Verdict: ACCEPTED

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

correct output
0

user output
0

Test 6

Verdict: ACCEPTED

input
4 4
1 3
2 3
3 4
1 2

correct output
3

user output
3

Test 7

Verdict: ACCEPTED

input
4 3
1 3
2 4
2 3

correct output
1

user output
1

Test 8

Verdict:

input
4 3
2 3
2 4
1 3

correct output
3

user output
1

Test 9

Verdict: ACCEPTED

input
4 4
3 4
2 3
1 3
1 2

correct output
1

user output
1

Test 10

Verdict: ACCEPTED

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

correct output
0

user output
0

Test 11

Verdict: ACCEPTED

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

correct output
0

user output
0

Test 12

Verdict: ACCEPTED

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

correct output
3

user output
3

Test 13

Verdict: ACCEPTED

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

correct output
0

user output
0

Test 14

Verdict: ACCEPTED

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

correct output
0

user output
0

Test 15

Verdict: ACCEPTED

input
5 4
1 3
2 3
2 4
4 5

correct output
1

user output
1

Test 16

Verdict:

input
5 4
2 4
3 5
2 3
1 4

correct output
4

user output
1

Test 17

Verdict: ACCEPTED

input
5 9
4 5
1 2
3 4
1 5
...

correct output
0

user output
0

Test 18

Verdict: ACCEPTED

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

correct output
0

user output
0

Test 19

Verdict: ACCEPTED

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

correct output
0

user output
0

Test 20

Verdict: ACCEPTED

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

correct output
9

user output
9

Test 21

Verdict: ACCEPTED

input
10 43
7 10
1 2
1 3
2 3
...

correct output
0

user output
0

Test 22

Verdict:

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

correct output
4

user output
3

Test 23

Verdict:

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

correct output
8

user output
1

Test 24

Verdict: ACCEPTED

input
10 12
7 9
3 8
3 5
4 5
...

correct output
0

user output
0

Test 25

Verdict:

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

correct output
9

user output
1

Test 26

Verdict:

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

correct output
3

user output
1

Test 27

Verdict: ACCEPTED

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

correct output
0

user output
0

Test 28

Verdict: ACCEPTED

input
10 44
2 6
1 2
1 6
4 7
...

correct output
0

user output
0

Test 29

Verdict: ACCEPTED

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

correct output
0

user output
0

Test 30

Verdict:

input
100 107
44 59
24 76
35 79
14 57
...

correct output
107

user output
9

Test 31

Verdict:

input
100 107
27 79
43 89
83 96
34 75
...

correct output
42

user output
1

Test 32

Verdict: ACCEPTED

input
100 143
13 14
20 59
25 95
38 84
...

correct output
129

user output
129

Test 33

Verdict: ACCEPTED

input
100 155
29 92
3 63
16 18
9 28
...

correct output
36

user output
36

Test 34

Verdict:

input
100 105
74 85
6 49
7 81
45 55
...

correct output
50

user output
2

Test 35

Verdict: ACCEPTED

input
100 121
8 74
14 50
44 55
12 93
...

correct output
0

user output
0

Test 36

Verdict:

input
100 102
54 97
45 82
30 78
40 44
...

correct output
35

user output
2

Test 37

Verdict: ACCEPTED

input
100 106
57 85
3 58
14 93
31 61
...

correct output
8

user output
8

Test 38

Verdict: ACCEPTED

input
100 188
3 58
53 63
8 19
97 100
...

correct output
0

user output
0

Test 39

Verdict:

input
100 100
16 94
48 96
4 37
71 85
...

correct output
41

user output
2

Test 40

Verdict:

input
200 207
111 188
80 121
115 170
157 175
...

correct output
199

user output
146

Test 41

Verdict:

input
200 207
99 117
139 143
151 174
136 184
...

correct output
190

user output
3

Test 42

Verdict: ACCEPTED

input
200 287
36 180
125 138
33 147
38 188
...

correct output
77

user output
77

Test 43

Verdict: ACCEPTED

input
200 310
162 195
21 199
95 180
29 127
...

correct output
30

user output
30

Test 44

Verdict:

input
200 205
140 187
127 185
100 196
55 144
...

correct output
150

user output
5

Test 45

Verdict:

input
200 243
8 85
73 137
52 72
51 157
...

correct output
53

user output
15

Test 46

Verdict: ACCEPTED

input
200 202
45 72
5 96
1 31
35 187
...

correct output
3

user output
3

Test 47

Verdict:

input
200 214
81 116
113 164
154 199
17 153
...

correct output
45

user output
4

Test 48

Verdict: ACCEPTED

input
200 375
68 93
93 101
87 109
23 56
...

correct output
0

user output
0

Test 49

Verdict:

input
200 201
64 124
145 164
103 127
126 186
...

correct output
137

user output
16

Test 50

Verdict:

input
1000 1007
61 967
252 530
105 953
490 826
...

correct output
914

user output
8

Test 51

Verdict:

input
1000 1007
489 976
53 720
478 732
104 111
...

correct output
248

user output
5

Test 52

Verdict: ACCEPTED

input
1000 1435
520 551
702 787
76 719
619 993
...

correct output
1114

user output
1114

Test 53

Verdict: ACCEPTED

input
1000 1550
206 315
476 801
287 713
456 677
...

correct output
1059

user output
1059

Test 54

Verdict:

input
1000 1005
304 771
84 930
17 988
282 364
...

correct output
714

user output
25

Test 55

Verdict: ACCEPTED

input
1000 1221
254 729
301 692
201 239
530 550
...

correct output
406

user output
406

Test 56

Verdict:

input
1000 1002
147 586
502 522
194 372
176 451
...

correct output
117

user output
1

Test 57

Verdict:

input
1000 1075
470 600
129 251
654 850
567 661
...

correct output
642

user output
14

Test 58

Verdict: ACCEPTED

input
1000 1874
668 731
316 335
532 731
523 839
...

correct output
0

user output
0

Test 59

Verdict:

input
1000 1009
674 739
96 435
360 670
130 773
...

correct output
768

user output
1

Test 60

Verdict:

input
100000 100007
44087 69715
31775 43251
18923 35874
36912 75163
...

correct output
923

user output
11

Test 61

Verdict:

input
100000 100007
19253 34700
66338 83144
22343 27898
4199 35494
...

correct output
9842

user output
57

Test 62

Verdict: ACCEPTED

input
100000 143600
14018 46297
31183 86763
34508 87502
54400 64922
...

correct output
0

user output
0

Test 63

Verdict: ACCEPTED

input
100000 155080
7038 29509
13361 87647
1940 92912
7785 12866
...

correct output
0

user output
0

Test 64

Verdict:

input
100000 100005
33360 89287
42922 67061
77709 78585
48488 51346
...

correct output
523

user output
4

Test 65

Verdict:

input
100000 122199
24155 82039
27201 64021
37961 66619
7615 11277
...

correct output
61740

user output
13465

Test 66

Verdict:

input
100000 100002
13135 96454
53655 77933
52027 71763
40622 57175
...

correct output
60082

user output
27

Test 67

Verdict:

input
100000 107630
30498 57903
5372 70239
51740 59293
41709 92770
...

correct output
102920

user output
17311

Test 68

Verdict: ACCEPTED

input
100000 187345
5859 71722
58010 74508
66929 91927
15925 53657
...

correct output
129660

user output
129660

Test 69

Verdict:

input
100000 101036
13541 29328
50426 75396
46089 77950
42830 56873
...

correct output
80466

user output
2384