Submission #577205

# Submission time Handle Problem Language Result Execution time Memory
577205 2022-06-14T09:22:02 Z eecs Mountains and Valleys (CCO20_day1problem3) C++17
11 / 25
7000 ms 197012 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 5010;
int n, m, d[maxn][maxn], par[maxn][maxn];
vector<int> G[maxn];

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m;
    assert(n <= 5000 && m <= 10000);
    vector<array<int, 3>> E;
    while (m--) {
        int u, v, w;
        cin >> u >> v >> w;
        if (w == 1) G[u].push_back(v), G[v].push_back(u);
        else if (w < (n + 1) / 2) E.push_back({u, v, w});
    }
    int diam = 0;
    for (int i = 0; i < n; i++) {
        auto dfs = [&](auto self, int u, int fa) -> void {
            diam = max(diam, d[i][u]), par[i][u] = fa;
            for (int v : G[u]) if (v ^ fa) {
                d[i][v] = d[i][u] + 1, self(self, v, u);
            }
        };
        dfs(dfs, i, -1);
    }
    int ans = 2 * (n - 1) - diam;
    for (auto &e : E) {
        int u = e[0], v = e[1], mn = INT_MAX;
        for (int i = 0; i < n; i++) if (i ^ u) {
            for (int j = 0; j < n; j++) if (j ^ v) {
                int cnt = 0;
                for (int k = 0; k < n; k++) {
                    if (d[u][k] + d[k][i] == d[u][i] && d[v][k] + d[k][j] == d[v][j]) cnt++;
                }
                mn = min(mn, 2 * (n + cnt - 2) - d[u][i] - d[v][j]);
            }
        }
        ans = min(ans, mn + e[2]);
    }
    cout << ans << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 448 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 448 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 448 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 0 ms 468 KB Output is correct
19 Correct 1 ms 544 KB Output is correct
20 Correct 1 ms 572 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 576 KB Output is correct
26 Correct 1 ms 568 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 572 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 444 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 823 ms 196908 KB Output is correct
2 Correct 866 ms 197012 KB Output is correct
3 Correct 779 ms 196860 KB Output is correct
4 Correct 795 ms 196764 KB Output is correct
5 Correct 785 ms 196768 KB Output is correct
6 Correct 396 ms 196764 KB Output is correct
7 Correct 799 ms 196804 KB Output is correct
8 Correct 848 ms 196788 KB Output is correct
9 Correct 815 ms 197004 KB Output is correct
10 Correct 780 ms 196776 KB Output is correct
11 Correct 795 ms 196776 KB Output is correct
12 Correct 778 ms 196444 KB Output is correct
13 Correct 817 ms 196620 KB Output is correct
14 Correct 780 ms 196736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 448 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 448 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 0 ms 468 KB Output is correct
19 Correct 1 ms 544 KB Output is correct
20 Correct 1 ms 572 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 576 KB Output is correct
26 Correct 1 ms 568 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 572 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 444 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 122 ms 1316 KB Output is correct
37 Correct 96 ms 1236 KB Output is correct
38 Correct 127 ms 1312 KB Output is correct
39 Correct 134 ms 1300 KB Output is correct
40 Correct 132 ms 1300 KB Output is correct
41 Correct 52 ms 1316 KB Output is correct
42 Correct 111 ms 1236 KB Output is correct
43 Correct 134 ms 1304 KB Output is correct
44 Correct 117 ms 1300 KB Output is correct
45 Correct 128 ms 1276 KB Output is correct
46 Correct 145 ms 1236 KB Output is correct
47 Correct 111 ms 1236 KB Output is correct
48 Correct 20 ms 1304 KB Output is correct
49 Correct 23 ms 1312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 448 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 448 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 0 ms 468 KB Output is correct
19 Correct 1 ms 544 KB Output is correct
20 Correct 1 ms 572 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 576 KB Output is correct
26 Correct 1 ms 568 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 572 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 444 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 122 ms 1316 KB Output is correct
37 Correct 96 ms 1236 KB Output is correct
38 Correct 127 ms 1312 KB Output is correct
39 Correct 134 ms 1300 KB Output is correct
40 Correct 132 ms 1300 KB Output is correct
41 Correct 52 ms 1316 KB Output is correct
42 Correct 111 ms 1236 KB Output is correct
43 Correct 134 ms 1304 KB Output is correct
44 Correct 117 ms 1300 KB Output is correct
45 Correct 128 ms 1276 KB Output is correct
46 Correct 145 ms 1236 KB Output is correct
47 Correct 111 ms 1236 KB Output is correct
48 Correct 20 ms 1304 KB Output is correct
49 Correct 23 ms 1312 KB Output is correct
50 Execution timed out 7018 ms 6356 KB Time limit exceeded
51 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 448 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 448 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 0 ms 468 KB Output is correct
19 Correct 1 ms 544 KB Output is correct
20 Correct 1 ms 572 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 576 KB Output is correct
26 Correct 1 ms 568 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 572 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 444 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 823 ms 196908 KB Output is correct
37 Correct 866 ms 197012 KB Output is correct
38 Correct 779 ms 196860 KB Output is correct
39 Correct 795 ms 196764 KB Output is correct
40 Correct 785 ms 196768 KB Output is correct
41 Correct 396 ms 196764 KB Output is correct
42 Correct 799 ms 196804 KB Output is correct
43 Correct 848 ms 196788 KB Output is correct
44 Correct 815 ms 197004 KB Output is correct
45 Correct 780 ms 196776 KB Output is correct
46 Correct 795 ms 196776 KB Output is correct
47 Correct 778 ms 196444 KB Output is correct
48 Correct 817 ms 196620 KB Output is correct
49 Correct 780 ms 196736 KB Output is correct
50 Correct 122 ms 1316 KB Output is correct
51 Correct 96 ms 1236 KB Output is correct
52 Correct 127 ms 1312 KB Output is correct
53 Correct 134 ms 1300 KB Output is correct
54 Correct 132 ms 1300 KB Output is correct
55 Correct 52 ms 1316 KB Output is correct
56 Correct 111 ms 1236 KB Output is correct
57 Correct 134 ms 1304 KB Output is correct
58 Correct 117 ms 1300 KB Output is correct
59 Correct 128 ms 1276 KB Output is correct
60 Correct 145 ms 1236 KB Output is correct
61 Correct 111 ms 1236 KB Output is correct
62 Correct 20 ms 1304 KB Output is correct
63 Correct 23 ms 1312 KB Output is correct
64 Execution timed out 7018 ms 6356 KB Time limit exceeded
65 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 448 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 448 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 0 ms 468 KB Output is correct
19 Correct 1 ms 544 KB Output is correct
20 Correct 1 ms 572 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 576 KB Output is correct
26 Correct 1 ms 568 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 572 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 444 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 823 ms 196908 KB Output is correct
37 Correct 866 ms 197012 KB Output is correct
38 Correct 779 ms 196860 KB Output is correct
39 Correct 795 ms 196764 KB Output is correct
40 Correct 785 ms 196768 KB Output is correct
41 Correct 396 ms 196764 KB Output is correct
42 Correct 799 ms 196804 KB Output is correct
43 Correct 848 ms 196788 KB Output is correct
44 Correct 815 ms 197004 KB Output is correct
45 Correct 780 ms 196776 KB Output is correct
46 Correct 795 ms 196776 KB Output is correct
47 Correct 778 ms 196444 KB Output is correct
48 Correct 817 ms 196620 KB Output is correct
49 Correct 780 ms 196736 KB Output is correct
50 Correct 122 ms 1316 KB Output is correct
51 Correct 96 ms 1236 KB Output is correct
52 Correct 127 ms 1312 KB Output is correct
53 Correct 134 ms 1300 KB Output is correct
54 Correct 132 ms 1300 KB Output is correct
55 Correct 52 ms 1316 KB Output is correct
56 Correct 111 ms 1236 KB Output is correct
57 Correct 134 ms 1304 KB Output is correct
58 Correct 117 ms 1300 KB Output is correct
59 Correct 128 ms 1276 KB Output is correct
60 Correct 145 ms 1236 KB Output is correct
61 Correct 111 ms 1236 KB Output is correct
62 Correct 20 ms 1304 KB Output is correct
63 Correct 23 ms 1312 KB Output is correct
64 Execution timed out 7018 ms 6356 KB Time limit exceeded
65 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 448 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 448 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 0 ms 468 KB Output is correct
19 Correct 1 ms 544 KB Output is correct
20 Correct 1 ms 572 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 576 KB Output is correct
26 Correct 1 ms 568 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 572 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 444 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 823 ms 196908 KB Output is correct
37 Correct 866 ms 197012 KB Output is correct
38 Correct 779 ms 196860 KB Output is correct
39 Correct 795 ms 196764 KB Output is correct
40 Correct 785 ms 196768 KB Output is correct
41 Correct 396 ms 196764 KB Output is correct
42 Correct 799 ms 196804 KB Output is correct
43 Correct 848 ms 196788 KB Output is correct
44 Correct 815 ms 197004 KB Output is correct
45 Correct 780 ms 196776 KB Output is correct
46 Correct 795 ms 196776 KB Output is correct
47 Correct 778 ms 196444 KB Output is correct
48 Correct 817 ms 196620 KB Output is correct
49 Correct 780 ms 196736 KB Output is correct
50 Correct 122 ms 1316 KB Output is correct
51 Correct 96 ms 1236 KB Output is correct
52 Correct 127 ms 1312 KB Output is correct
53 Correct 134 ms 1300 KB Output is correct
54 Correct 132 ms 1300 KB Output is correct
55 Correct 52 ms 1316 KB Output is correct
56 Correct 111 ms 1236 KB Output is correct
57 Correct 134 ms 1304 KB Output is correct
58 Correct 117 ms 1300 KB Output is correct
59 Correct 128 ms 1276 KB Output is correct
60 Correct 145 ms 1236 KB Output is correct
61 Correct 111 ms 1236 KB Output is correct
62 Correct 20 ms 1304 KB Output is correct
63 Correct 23 ms 1312 KB Output is correct
64 Execution timed out 7018 ms 6356 KB Time limit exceeded
65 Halted 0 ms 0 KB -