Submission #525568

# Submission time Handle Problem Language Result Execution time Memory
525568 2022-02-12T01:14:33 Z mjhmjh1104 Mountains and Valleys (CCO20_day1problem3) C++17
5 / 25
828 ms 108396 KB
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

int n, m, dp[20][20971520];
vector<int> tree[200006];
vector<pair<int, int>> adj[200006];

int f(int x, int y) {
    if (dp[x][y] + 1) return dp[x][y];
    if (!y) return dp[x][y] = 0;
    dp[x][y] = (int)1e9;
    for (auto &i: adj[x]) dp[x][y] = min(dp[x][y], i.second + f(i.first, y & ~(1 << i.first)));
    for (auto &i: tree[x]) dp[x][y] = min(dp[x][y], 1 + f(i, y & ~(1 << i)));
    return dp[x][y];
}

int lt[5006][5006], dist[200006], par[200006], depth[200006];
bool cl[200006];

void dfs(int x, int y, int dist = 0, int prev = -1) {
    par[x] = prev;
    lt[y][x] = dist;
    for (auto &i: tree[x]) if (i != prev) {
        depth[i] = depth[x] + 1;
        dfs(i, y, dist + 1, x);
    }
}

int dfs_dist(int x, int prev = -1) {
    int ret = x;
    for (auto &i: tree[x]) if (i != prev) {
        dist[i] = 1 + dist[x];
        int t = dfs_dist(i, x);
        if (dist[t] > dist[ret]) ret = t;
    }
    return ret;
}

int intersect(int x, int y, int z, int w) {
    int ret = 0;
    for (int i = 0; i < n; i++) cl[i] = false;
    if (depth[x] < depth[y]) swap(x, y);
    if (depth[z] < depth[w]) swap(z, w);
    while (depth[x] > depth[y]) {
        if (!cl[x]) ret++, cl[x] = true;
        x = par[x];
    }
    while (x != y) {
        if (!cl[x]) ret++, cl[x] = true;
        if (!cl[y]) ret++, cl[y] = true;
        x = par[x], y = par[y];
    }
    while (depth[z] > depth[w]) {
        if (!cl[z]) ret++, cl[z] = true;
        z = par[z];
    }
    while (z != w) {
        if (!cl[z]) ret++, cl[z] = true;
        if (!cl[w]) ret++, cl[w] = true;
        z = par[z], w = par[w];
    }
    return ret;
}

int main() {
    scanf("%d%d", &n, &m);
    while (m--) {
        int x, y, w;
        scanf("%d%d%d", &x, &y, &w);
        if (w == 1) {
            tree[x].push_back(y);
            tree[y].push_back(x);
        } else {
            adj[x].push_back({ y, w });
            adj[y].push_back({ x, w });
        }
    }
    if (n <= 20) {
        for (int i = 0; i < n; i++) for (int j = 0; j < 1 << n; j++) dp[i][j] = -1;
        int res = (int)1e9;
        for (int i = 0; i < n; i++) res = min(res, f(i, (1 << n) - 1 ^ 1 << i));
        printf("%d", res);
        return 0;
    }
    for (int i = 0; i < n; i++) dfs(i, i);
    int t = dfs_dist(dist[0] = 0);
    dist[t] = 0;
    int zero = 2 * (n - 1) - dist[dfs_dist(t)], one = (int)1e9, two = (int)1e9;
    for (int i = 0; i < n; i++) for (auto &j: adj[i]) if (i < j.first) one = min(one, j.second + 2 * (n - 1) - lt[i][j.first]);
    if (n <= 500) for (int i = 0; i < n; i++) for (auto &j: adj[i]) if (i < j.first) for (int k = 0; k < n; k++) for (auto &l: adj[k]) if (k < l.first) {
        two = min(two, j.second + 2 * (n - 1) - intersect(i, l.first, k, j.first));
        two = min(two, j.second + l.second + 2 * (n - 1) - intersect(i, k, l.first, j.first));
    }
    printf("%d", min({ zero, one, two }));
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:84:66: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   84 |         for (int i = 0; i < n; i++) res = min(res, f(i, (1 << n) - 1 ^ 1 << i));
      |                                                         ~~~~~~~~~^~~
Main.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         scanf("%d%d%d", &x, &y, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 7 ms 9720 KB Output is correct
3 Correct 5 ms 9732 KB Output is correct
4 Correct 5 ms 9736 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 5 ms 9656 KB Output is correct
8 Correct 6 ms 9740 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 5 ms 9620 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9676 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 8 ms 9620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 7 ms 9720 KB Output is correct
3 Correct 5 ms 9732 KB Output is correct
4 Correct 5 ms 9736 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 5 ms 9656 KB Output is correct
8 Correct 6 ms 9740 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 5 ms 9620 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9676 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 8 ms 9620 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 6 ms 9804 KB Output is correct
18 Correct 5 ms 9676 KB Output is correct
19 Correct 5 ms 9932 KB Output is correct
20 Correct 7 ms 11608 KB Output is correct
21 Correct 6 ms 11724 KB Output is correct
22 Correct 176 ms 91840 KB Output is correct
23 Correct 45 ms 48716 KB Output is correct
24 Correct 43 ms 48816 KB Output is correct
25 Correct 49 ms 28176 KB Output is correct
26 Correct 67 ms 28248 KB Output is correct
27 Correct 292 ms 91904 KB Output is correct
28 Correct 20 ms 28236 KB Output is correct
29 Correct 31 ms 28264 KB Output is correct
30 Correct 23 ms 28264 KB Output is correct
31 Correct 28 ms 28268 KB Output is correct
32 Correct 11 ms 11724 KB Output is correct
33 Correct 5 ms 9676 KB Output is correct
34 Correct 14 ms 28236 KB Output is correct
35 Correct 14 ms 28240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 804 ms 108396 KB Output is correct
2 Correct 807 ms 108352 KB Output is correct
3 Correct 772 ms 108184 KB Output is correct
4 Correct 759 ms 108156 KB Output is correct
5 Correct 740 ms 108132 KB Output is correct
6 Correct 272 ms 107968 KB Output is correct
7 Correct 782 ms 108396 KB Output is correct
8 Correct 747 ms 108180 KB Output is correct
9 Correct 764 ms 108312 KB Output is correct
10 Correct 774 ms 108124 KB Output is correct
11 Correct 778 ms 108048 KB Output is correct
12 Correct 699 ms 107896 KB Output is correct
13 Correct 828 ms 108144 KB Output is correct
14 Correct 761 ms 108164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 7 ms 9720 KB Output is correct
3 Correct 5 ms 9732 KB Output is correct
4 Correct 5 ms 9736 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 5 ms 9656 KB Output is correct
8 Correct 6 ms 9740 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 5 ms 9620 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9676 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 8 ms 9620 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 6 ms 9804 KB Output is correct
18 Correct 5 ms 9676 KB Output is correct
19 Correct 5 ms 9932 KB Output is correct
20 Correct 7 ms 11608 KB Output is correct
21 Correct 6 ms 11724 KB Output is correct
22 Correct 176 ms 91840 KB Output is correct
23 Correct 45 ms 48716 KB Output is correct
24 Correct 43 ms 48816 KB Output is correct
25 Correct 49 ms 28176 KB Output is correct
26 Correct 67 ms 28248 KB Output is correct
27 Correct 292 ms 91904 KB Output is correct
28 Correct 20 ms 28236 KB Output is correct
29 Correct 31 ms 28264 KB Output is correct
30 Correct 23 ms 28264 KB Output is correct
31 Correct 28 ms 28268 KB Output is correct
32 Correct 11 ms 11724 KB Output is correct
33 Correct 5 ms 9676 KB Output is correct
34 Correct 14 ms 28236 KB Output is correct
35 Correct 14 ms 28240 KB Output is correct
36 Correct 10 ms 10060 KB Output is correct
37 Correct 12 ms 10140 KB Output is correct
38 Correct 11 ms 10136 KB Output is correct
39 Correct 9 ms 10024 KB Output is correct
40 Correct 9 ms 10060 KB Output is correct
41 Correct 5 ms 10044 KB Output is correct
42 Correct 9 ms 10136 KB Output is correct
43 Correct 8 ms 10060 KB Output is correct
44 Correct 9 ms 10060 KB Output is correct
45 Incorrect 8 ms 10060 KB Output isn't correct
46 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 7 ms 9720 KB Output is correct
3 Correct 5 ms 9732 KB Output is correct
4 Correct 5 ms 9736 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 5 ms 9656 KB Output is correct
8 Correct 6 ms 9740 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 5 ms 9620 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9676 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 8 ms 9620 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 6 ms 9804 KB Output is correct
18 Correct 5 ms 9676 KB Output is correct
19 Correct 5 ms 9932 KB Output is correct
20 Correct 7 ms 11608 KB Output is correct
21 Correct 6 ms 11724 KB Output is correct
22 Correct 176 ms 91840 KB Output is correct
23 Correct 45 ms 48716 KB Output is correct
24 Correct 43 ms 48816 KB Output is correct
25 Correct 49 ms 28176 KB Output is correct
26 Correct 67 ms 28248 KB Output is correct
27 Correct 292 ms 91904 KB Output is correct
28 Correct 20 ms 28236 KB Output is correct
29 Correct 31 ms 28264 KB Output is correct
30 Correct 23 ms 28264 KB Output is correct
31 Correct 28 ms 28268 KB Output is correct
32 Correct 11 ms 11724 KB Output is correct
33 Correct 5 ms 9676 KB Output is correct
34 Correct 14 ms 28236 KB Output is correct
35 Correct 14 ms 28240 KB Output is correct
36 Correct 10 ms 10060 KB Output is correct
37 Correct 12 ms 10140 KB Output is correct
38 Correct 11 ms 10136 KB Output is correct
39 Correct 9 ms 10024 KB Output is correct
40 Correct 9 ms 10060 KB Output is correct
41 Correct 5 ms 10044 KB Output is correct
42 Correct 9 ms 10136 KB Output is correct
43 Correct 8 ms 10060 KB Output is correct
44 Correct 9 ms 10060 KB Output is correct
45 Incorrect 8 ms 10060 KB Output isn't correct
46 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 7 ms 9720 KB Output is correct
3 Correct 5 ms 9732 KB Output is correct
4 Correct 5 ms 9736 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 5 ms 9656 KB Output is correct
8 Correct 6 ms 9740 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 5 ms 9620 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9676 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 8 ms 9620 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 6 ms 9804 KB Output is correct
18 Correct 5 ms 9676 KB Output is correct
19 Correct 5 ms 9932 KB Output is correct
20 Correct 7 ms 11608 KB Output is correct
21 Correct 6 ms 11724 KB Output is correct
22 Correct 176 ms 91840 KB Output is correct
23 Correct 45 ms 48716 KB Output is correct
24 Correct 43 ms 48816 KB Output is correct
25 Correct 49 ms 28176 KB Output is correct
26 Correct 67 ms 28248 KB Output is correct
27 Correct 292 ms 91904 KB Output is correct
28 Correct 20 ms 28236 KB Output is correct
29 Correct 31 ms 28264 KB Output is correct
30 Correct 23 ms 28264 KB Output is correct
31 Correct 28 ms 28268 KB Output is correct
32 Correct 11 ms 11724 KB Output is correct
33 Correct 5 ms 9676 KB Output is correct
34 Correct 14 ms 28236 KB Output is correct
35 Correct 14 ms 28240 KB Output is correct
36 Correct 804 ms 108396 KB Output is correct
37 Correct 807 ms 108352 KB Output is correct
38 Correct 772 ms 108184 KB Output is correct
39 Correct 759 ms 108156 KB Output is correct
40 Correct 740 ms 108132 KB Output is correct
41 Correct 272 ms 107968 KB Output is correct
42 Correct 782 ms 108396 KB Output is correct
43 Correct 747 ms 108180 KB Output is correct
44 Correct 764 ms 108312 KB Output is correct
45 Correct 774 ms 108124 KB Output is correct
46 Correct 778 ms 108048 KB Output is correct
47 Correct 699 ms 107896 KB Output is correct
48 Correct 828 ms 108144 KB Output is correct
49 Correct 761 ms 108164 KB Output is correct
50 Correct 10 ms 10060 KB Output is correct
51 Correct 12 ms 10140 KB Output is correct
52 Correct 11 ms 10136 KB Output is correct
53 Correct 9 ms 10024 KB Output is correct
54 Correct 9 ms 10060 KB Output is correct
55 Correct 5 ms 10044 KB Output is correct
56 Correct 9 ms 10136 KB Output is correct
57 Correct 8 ms 10060 KB Output is correct
58 Correct 9 ms 10060 KB Output is correct
59 Incorrect 8 ms 10060 KB Output isn't correct
60 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 7 ms 9720 KB Output is correct
3 Correct 5 ms 9732 KB Output is correct
4 Correct 5 ms 9736 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 5 ms 9656 KB Output is correct
8 Correct 6 ms 9740 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 5 ms 9620 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9676 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 8 ms 9620 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 6 ms 9804 KB Output is correct
18 Correct 5 ms 9676 KB Output is correct
19 Correct 5 ms 9932 KB Output is correct
20 Correct 7 ms 11608 KB Output is correct
21 Correct 6 ms 11724 KB Output is correct
22 Correct 176 ms 91840 KB Output is correct
23 Correct 45 ms 48716 KB Output is correct
24 Correct 43 ms 48816 KB Output is correct
25 Correct 49 ms 28176 KB Output is correct
26 Correct 67 ms 28248 KB Output is correct
27 Correct 292 ms 91904 KB Output is correct
28 Correct 20 ms 28236 KB Output is correct
29 Correct 31 ms 28264 KB Output is correct
30 Correct 23 ms 28264 KB Output is correct
31 Correct 28 ms 28268 KB Output is correct
32 Correct 11 ms 11724 KB Output is correct
33 Correct 5 ms 9676 KB Output is correct
34 Correct 14 ms 28236 KB Output is correct
35 Correct 14 ms 28240 KB Output is correct
36 Correct 804 ms 108396 KB Output is correct
37 Correct 807 ms 108352 KB Output is correct
38 Correct 772 ms 108184 KB Output is correct
39 Correct 759 ms 108156 KB Output is correct
40 Correct 740 ms 108132 KB Output is correct
41 Correct 272 ms 107968 KB Output is correct
42 Correct 782 ms 108396 KB Output is correct
43 Correct 747 ms 108180 KB Output is correct
44 Correct 764 ms 108312 KB Output is correct
45 Correct 774 ms 108124 KB Output is correct
46 Correct 778 ms 108048 KB Output is correct
47 Correct 699 ms 107896 KB Output is correct
48 Correct 828 ms 108144 KB Output is correct
49 Correct 761 ms 108164 KB Output is correct
50 Correct 10 ms 10060 KB Output is correct
51 Correct 12 ms 10140 KB Output is correct
52 Correct 11 ms 10136 KB Output is correct
53 Correct 9 ms 10024 KB Output is correct
54 Correct 9 ms 10060 KB Output is correct
55 Correct 5 ms 10044 KB Output is correct
56 Correct 9 ms 10136 KB Output is correct
57 Correct 8 ms 10060 KB Output is correct
58 Correct 9 ms 10060 KB Output is correct
59 Incorrect 8 ms 10060 KB Output isn't correct
60 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 7 ms 9720 KB Output is correct
3 Correct 5 ms 9732 KB Output is correct
4 Correct 5 ms 9736 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 5 ms 9656 KB Output is correct
8 Correct 6 ms 9740 KB Output is correct
9 Correct 7 ms 9676 KB Output is correct
10 Correct 5 ms 9620 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9676 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
15 Correct 8 ms 9620 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 6 ms 9804 KB Output is correct
18 Correct 5 ms 9676 KB Output is correct
19 Correct 5 ms 9932 KB Output is correct
20 Correct 7 ms 11608 KB Output is correct
21 Correct 6 ms 11724 KB Output is correct
22 Correct 176 ms 91840 KB Output is correct
23 Correct 45 ms 48716 KB Output is correct
24 Correct 43 ms 48816 KB Output is correct
25 Correct 49 ms 28176 KB Output is correct
26 Correct 67 ms 28248 KB Output is correct
27 Correct 292 ms 91904 KB Output is correct
28 Correct 20 ms 28236 KB Output is correct
29 Correct 31 ms 28264 KB Output is correct
30 Correct 23 ms 28264 KB Output is correct
31 Correct 28 ms 28268 KB Output is correct
32 Correct 11 ms 11724 KB Output is correct
33 Correct 5 ms 9676 KB Output is correct
34 Correct 14 ms 28236 KB Output is correct
35 Correct 14 ms 28240 KB Output is correct
36 Correct 804 ms 108396 KB Output is correct
37 Correct 807 ms 108352 KB Output is correct
38 Correct 772 ms 108184 KB Output is correct
39 Correct 759 ms 108156 KB Output is correct
40 Correct 740 ms 108132 KB Output is correct
41 Correct 272 ms 107968 KB Output is correct
42 Correct 782 ms 108396 KB Output is correct
43 Correct 747 ms 108180 KB Output is correct
44 Correct 764 ms 108312 KB Output is correct
45 Correct 774 ms 108124 KB Output is correct
46 Correct 778 ms 108048 KB Output is correct
47 Correct 699 ms 107896 KB Output is correct
48 Correct 828 ms 108144 KB Output is correct
49 Correct 761 ms 108164 KB Output is correct
50 Correct 10 ms 10060 KB Output is correct
51 Correct 12 ms 10140 KB Output is correct
52 Correct 11 ms 10136 KB Output is correct
53 Correct 9 ms 10024 KB Output is correct
54 Correct 9 ms 10060 KB Output is correct
55 Correct 5 ms 10044 KB Output is correct
56 Correct 9 ms 10136 KB Output is correct
57 Correct 8 ms 10060 KB Output is correct
58 Correct 9 ms 10060 KB Output is correct
59 Incorrect 8 ms 10060 KB Output isn't correct
60 Halted 0 ms 0 KB -