Task: | Road blockade |
Sender: | bubu2006 |
Submission time: | 2024-10-21 17:06:49 +0300 |
Language: | C++ (C++20) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | ACCEPTED | 0.00 s | details |
#7 | ACCEPTED | 0.00 s | details |
#8 | ACCEPTED | 0.00 s | details |
#9 | ACCEPTED | 0.00 s | details |
#10 | ACCEPTED | 0.00 s | details |
#11 | ACCEPTED | 0.00 s | details |
#12 | ACCEPTED | 0.01 s | details |
#13 | ACCEPTED | 0.00 s | details |
#14 | ACCEPTED | 0.00 s | details |
#15 | ACCEPTED | 0.00 s | details |
#16 | ACCEPTED | 0.00 s | details |
#17 | ACCEPTED | 0.00 s | details |
#18 | ACCEPTED | 0.00 s | details |
#19 | ACCEPTED | 0.00 s | details |
#20 | ACCEPTED | 0.00 s | details |
#21 | ACCEPTED | 0.00 s | details |
#22 | ACCEPTED | 0.00 s | details |
#23 | ACCEPTED | 0.00 s | details |
#24 | ACCEPTED | 0.00 s | details |
#25 | ACCEPTED | 0.00 s | details |
#26 | ACCEPTED | 0.00 s | details |
#27 | ACCEPTED | 0.00 s | details |
#28 | ACCEPTED | 0.00 s | details |
#29 | ACCEPTED | 0.00 s | details |
#30 | ACCEPTED | 0.00 s | details |
#31 | ACCEPTED | 0.00 s | details |
#32 | ACCEPTED | 0.00 s | details |
#33 | ACCEPTED | 0.00 s | details |
#34 | ACCEPTED | 0.00 s | details |
#35 | ACCEPTED | 0.00 s | details |
#36 | ACCEPTED | 0.00 s | details |
#37 | ACCEPTED | 0.00 s | details |
#38 | ACCEPTED | 0.00 s | details |
#39 | ACCEPTED | 0.00 s | details |
#40 | ACCEPTED | 0.00 s | details |
#41 | ACCEPTED | 0.00 s | details |
#42 | ACCEPTED | 0.01 s | details |
#43 | ACCEPTED | 0.00 s | details |
#44 | ACCEPTED | 0.00 s | details |
#45 | ACCEPTED | 0.00 s | details |
#46 | ACCEPTED | 0.00 s | details |
#47 | ACCEPTED | 0.00 s | details |
#48 | ACCEPTED | 0.00 s | details |
#49 | ACCEPTED | 0.00 s | details |
#50 | ACCEPTED | 0.00 s | details |
#51 | ACCEPTED | 0.00 s | details |
#52 | ACCEPTED | 0.00 s | details |
#53 | ACCEPTED | 0.01 s | details |
#54 | ACCEPTED | 0.00 s | details |
#55 | ACCEPTED | 0.00 s | details |
#56 | ACCEPTED | 0.00 s | details |
#57 | ACCEPTED | 0.00 s | details |
#58 | ACCEPTED | 0.01 s | details |
#59 | ACCEPTED | 0.00 s | details |
#60 | ACCEPTED | 0.14 s | details |
#61 | ACCEPTED | 0.14 s | details |
#62 | ACCEPTED | 0.25 s | details |
#63 | ACCEPTED | 0.20 s | details |
#64 | ACCEPTED | 0.14 s | details |
#65 | ACCEPTED | 0.16 s | details |
#66 | ACCEPTED | 0.13 s | details |
#67 | ACCEPTED | 0.14 s | details |
#68 | ACCEPTED | 0.21 s | details |
#69 | ACCEPTED | 0.12 s | details |
Compiler report
input/code.cpp: In function 'int main()': input/code.cpp:131:8: warning: unused variable 'tmp' [-Wunused-variable] 131 | ll tmp = dinic.calc(0, n - 1); | ^~~
Code
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; /** * Author: chilli * Date: 2019-04-26 * License: CC0 * Source: https://cp-algorithms.com/graph/dinic.html * Description: Flow algorithm with complexity $O(VE\log U)$ where $U = \max |\text{cap}|$. * $O(\min(E^{1/2}, V^{2/3})E)$ if $U = 1$; $O(\sqrt{V}E)$ for bipartite matching. * Status: Tested on SPOJ FASTFLOW and SPOJ MATCHING, stress-tested */ struct Dinic { struct Edge { int to, rev; ll c, oc; ll flow() { return max(oc - c, 0LL); } // if you need flows }; vi lvl, ptr, q; vector<vector<Edge>> adj; Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {} void addEdge(int a, int b, ll c, ll rcap = 0) { adj[a].push_back({b, sz(adj[b]), c, c}); adj[b].push_back({a, sz(adj[a]) - 1, rcap, rcap}); } ll dfs(int v, int t, ll f) { if (v == t || !f) return f; for (int& i = ptr[v]; i < sz(adj[v]); i++) { Edge& e = adj[v][i]; if (lvl[e.to] == lvl[v] + 1) if (ll p = dfs(e.to, t, min(f, e.c))) { e.c -= p, adj[e.to][e.rev].c += p; return p; } } return 0; } ll calc(int s, int t) { ll flow = 0; q[0] = s; rep(L,0,31) do { // 'int L=30' maybe faster for random data lvl = ptr = vi(sz(q)); int qi = 0, qe = lvl[s] = 1; while (qi < qe && !lvl[t]) { int v = q[qi++]; for (Edge e : adj[v]) if (!lvl[e.to] && e.c >> (30 - L)) q[qe++] = e.to, lvl[e.to] = lvl[v] + 1; } while (ll p = dfs(s, t, LLONG_MAX)) flow += p; } while (lvl[t]); return flow; } bool leftOfMinCut(int a) { return lvl[a] != 0; } }; vector<pair<int, int>> edges; int cnt, idx; void IS_BRIDGE(int v,int to, int id) { cnt++; idx = id; } int n; // number of nodes vector<vector<pair<int, int>>> adj; // adjacency list of graph vector<bool> visited; vector<int> tin, low; int timer; void dfs(int v, int p = -1) { visited[v] = true; tin[v] = low[v] = timer++; bool parent_skipped = false; for (auto [to, id] : adj[v]) { if (to == p && !parent_skipped) { parent_skipped = true; continue; } if (visited[to]) { low[v] = min(low[v], tin[to]); } else { dfs(to, v); low[v] = min(low[v], low[to]); if (low[to] > tin[v]) IS_BRIDGE(v, to, id); } } } void find_bridges() { timer = 0; visited.assign(n, false); tin.assign(n, -1); low.assign(n, -1); for (int i = 0; i < n; ++i) { if (!visited[i]) dfs(i); } } signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); // RTE if input wrong datatype int n, m; cin >> n >> m; adj.resize(n); Dinic dinic(n); vector<pair<int, int>> edges; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; adj[u].push_back({v, i}); adj[v].push_back({u, i}); dinic.addEdge(u, v, 1); dinic.addEdge(v, u, 1); edges.push_back({u, v}); } ll tmp = dinic.calc(0, n - 1); int cnt = 0, idx = 0; for (int i = 0; i < m; i++) { auto [u, v] = edges[i]; if (dinic.leftOfMinCut(u) != dinic.leftOfMinCut(v)) { cnt++; idx = i + 1; } } if (cnt == 1) { cout << idx << '\n'; } else { cout << 0 << '\n'; } // cout << cnt << ' ' << idx << '\n'; // find_bridges(); // if (cnt >= 1) { // cout << idx + 1<< '\n'; // } else { // cout << 0 << '\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: ACCEPTED
input |
---|
4 3 2 3 2 4 1 3 |
correct output |
---|
3 |
user output |
---|
3 |
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: ACCEPTED
input |
---|
5 4 2 4 3 5 2 3 1 4 |
correct output |
---|
4 |
user output |
---|
4 |
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: ACCEPTED
input |
---|
10 11 2 5 2 3 6 7 1 2 ... |
correct output |
---|
4 |
user output |
---|
4 |
Test 23
Verdict: ACCEPTED
input |
---|
10 10 4 9 5 9 7 8 6 8 ... |
correct output |
---|
8 |
user output |
---|
8 |
Test 24
Verdict: ACCEPTED
input |
---|
10 12 7 9 3 8 3 5 4 5 ... |
correct output |
---|
0 |
user output |
---|
0 |
Test 25
Verdict: ACCEPTED
input |
---|
10 9 2 4 8 10 6 7 2 3 ... |
correct output |
---|
9 |
user output |
---|
9 |
Test 26
Verdict: ACCEPTED
input |
---|
10 9 3 5 5 7 1 6 4 6 ... |
correct output |
---|
3 |
user output |
---|
3 |
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: ACCEPTED
input |
---|
100 107 44 59 24 76 35 79 14 57 ... |
correct output |
---|
107 |
user output |
---|
107 |
Test 31
Verdict: ACCEPTED
input |
---|
100 107 27 79 43 89 83 96 34 75 ... |
correct output |
---|
42 |
user output |
---|
42 |
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: ACCEPTED
input |
---|
100 105 74 85 6 49 7 81 45 55 ... |
correct output |
---|
50 |
user output |
---|
50 |
Test 35
Verdict: ACCEPTED
input |
---|
100 121 8 74 14 50 44 55 12 93 ... |
correct output |
---|
0 |
user output |
---|
0 |
Test 36
Verdict: ACCEPTED
input |
---|
100 102 54 97 45 82 30 78 40 44 ... |
correct output |
---|
35 |
user output |
---|
35 |
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: ACCEPTED
input |
---|
100 100 16 94 48 96 4 37 71 85 ... |
correct output |
---|
41 |
user output |
---|
41 |
Test 40
Verdict: ACCEPTED
input |
---|
200 207 111 188 80 121 115 170 157 175 ... |
correct output |
---|
199 |
user output |
---|
199 |
Test 41
Verdict: ACCEPTED
input |
---|
200 207 99 117 139 143 151 174 136 184 ... |
correct output |
---|
190 |
user output |
---|
190 |
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: ACCEPTED
input |
---|
200 205 140 187 127 185 100 196 55 144 ... |
correct output |
---|
150 |
user output |
---|
150 |
Test 45
Verdict: ACCEPTED
input |
---|
200 243 8 85 73 137 52 72 51 157 ... |
correct output |
---|
53 |
user output |
---|
53 |
Test 46
Verdict: ACCEPTED
input |
---|
200 202 45 72 5 96 1 31 35 187 ... |
correct output |
---|
3 |
user output |
---|
3 |
Test 47
Verdict: ACCEPTED
input |
---|
200 214 81 116 113 164 154 199 17 153 ... |
correct output |
---|
45 |
user output |
---|
45 |
Test 48
Verdict: ACCEPTED
input |
---|
200 375 68 93 93 101 87 109 23 56 ... |
correct output |
---|
0 |
user output |
---|
0 |
Test 49
Verdict: ACCEPTED
input |
---|
200 201 64 124 145 164 103 127 126 186 ... |
correct output |
---|
137 |
user output |
---|
137 |
Test 50
Verdict: ACCEPTED
input |
---|
1000 1007 61 967 252 530 105 953 490 826 ... |
correct output |
---|
914 |
user output |
---|
914 |
Test 51
Verdict: ACCEPTED
input |
---|
1000 1007 489 976 53 720 478 732 104 111 ... |
correct output |
---|
248 |
user output |
---|
248 |
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: ACCEPTED
input |
---|
1000 1005 304 771 84 930 17 988 282 364 ... |
correct output |
---|
714 |
user output |
---|
714 |
Test 55
Verdict: ACCEPTED
input |
---|
1000 1221 254 729 301 692 201 239 530 550 ... |
correct output |
---|
406 |
user output |
---|
406 |
Test 56
Verdict: ACCEPTED
input |
---|
1000 1002 147 586 502 522 194 372 176 451 ... |
correct output |
---|
117 |
user output |
---|
117 |
Test 57
Verdict: ACCEPTED
input |
---|
1000 1075 470 600 129 251 654 850 567 661 ... |
correct output |
---|
642 |
user output |
---|
642 |
Test 58
Verdict: ACCEPTED
input |
---|
1000 1874 668 731 316 335 532 731 523 839 ... |
correct output |
---|
0 |
user output |
---|
0 |
Test 59
Verdict: ACCEPTED
input |
---|
1000 1009 674 739 96 435 360 670 130 773 ... |
correct output |
---|
768 |
user output |
---|
768 |
Test 60
Verdict: ACCEPTED
input |
---|
100000 100007 44087 69715 31775 43251 18923 35874 36912 75163 ... |
correct output |
---|
923 |
user output |
---|
923 |
Test 61
Verdict: ACCEPTED
input |
---|
100000 100007 19253 34700 66338 83144 22343 27898 4199 35494 ... |
correct output |
---|
9842 |
user output |
---|
9842 |
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: ACCEPTED
input |
---|
100000 100005 33360 89287 42922 67061 77709 78585 48488 51346 ... |
correct output |
---|
523 |
user output |
---|
523 |
Test 65
Verdict: ACCEPTED
input |
---|
100000 122199 24155 82039 27201 64021 37961 66619 7615 11277 ... |
correct output |
---|
61740 |
user output |
---|
61740 |
Test 66
Verdict: ACCEPTED
input |
---|
100000 100002 13135 96454 53655 77933 52027 71763 40622 57175 ... |
correct output |
---|
60082 |
user output |
---|
60082 |
Test 67
Verdict: ACCEPTED
input |
---|
100000 107630 30498 57903 5372 70239 51740 59293 41709 92770 ... |
correct output |
---|
102920 |
user output |
---|
102920 |
Test 68
Verdict: ACCEPTED
input |
---|
100000 187345 5859 71722 58010 74508 66929 91927 15925 53657 ... |
correct output |
---|
129660 |
user output |
---|
129660 |
Test 69
Verdict: ACCEPTED
input |
---|
100000 101036 13541 29328 50426 75396 46089 77950 42830 56873 ... |
correct output |
---|
80466 |
user output |
---|
80466 |