답안 #525818

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
525818 2022-02-13T00:26:38 Z mjhmjh1104 Mountains and Valleys (CCO20_day1problem3) C++17
5 / 25
3189 ms 108692 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 par[200006], sz[200006];
bool cl[200006];
 
int dfs(int x, int prev = -1) {
    par[x] = prev;
    sz[x] = 1;
    for (auto &i: tree[x]) if (i != prev) {
        sz[x] = max(sz[x], dfs(i, x) + 1);
    }
    return sz[x];
}

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

int dist[200006];
 
int dfs_dist(int x, int prev = -1) {
    vector<int> v;
    int ret = 0;
    dist[x] = 0;
    for (auto &i: tree[x]) if (i != prev) {
        v.push_back(dfs_dist(i, x) + 1);
        ret = max(ret, v.back());
        dist[x] = max(dist[x], dist[i]);
    }
    sort(v.rbegin(), v.rend());
    if (!v.empty()) dist[x] = max(dist[x], v[0]);
    if ((int)v.size() > 1) dist[x] = max(dist[x], v[0] + v[1]);
    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_lt(i, i);
    dfs_dist(0);
    int zero = 2 * (n - 1) - dist[0], one = (int)1e9;
    for (int i = 0; i < n; i++) for (auto &j: adj[i]) if (i < j.first) {
        int curr = j.second + 2 * (n - 1) - lt[i][j.first] - 1;
        dfs(i);
        int x = j.first, pv = -1;
        vector<pair<int, int>> v;
        int y = -1, T = 0;
        dfs_dist(i);
        while (x != -1) {
            for (auto &k: tree[x]) {
                if (k != pv && k != par[x]) {
                    if (sz[k]) v.push_back({ sz[k], T });
                    y = max(y, dist[k] - 1);
                }
                T++;
            }
            pv = x;
            x = par[x];
        }
        int ans = 0;
        for (int i = 0; i < (int)v.size(); i++) {
            if (i) ans -= v[i].second - v[i - 1].second;
            y = max(y, ans + v[i].first - 1);
            ans = max(ans, v[i].first);
        }
        curr -= y;
        one = min(one, curr);
    }
    printf("%d", min(zero, one));
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:72:66: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   72 |         for (int i = 0; i < n; i++) res = min(res, f(i, (1 << n) - 1 ^ 1 << i));
      |                                                         ~~~~~~~~~^~~
Main.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         scanf("%d%d%d", &x, &y, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 4 ms 9676 KB Output is correct
6 Correct 4 ms 9732 KB Output is correct
7 Correct 5 ms 9676 KB Output is correct
8 Correct 5 ms 9676 KB Output is correct
9 Correct 4 ms 9676 KB Output is correct
10 Correct 4 ms 9672 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 4 ms 9676 KB Output is correct
15 Correct 4 ms 9612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 4 ms 9676 KB Output is correct
6 Correct 4 ms 9732 KB Output is correct
7 Correct 5 ms 9676 KB Output is correct
8 Correct 5 ms 9676 KB Output is correct
9 Correct 4 ms 9676 KB Output is correct
10 Correct 4 ms 9672 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 4 ms 9676 KB Output is correct
15 Correct 4 ms 9612 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 4 ms 9676 KB Output is correct
19 Correct 5 ms 9932 KB Output is correct
20 Correct 5 ms 11724 KB Output is correct
21 Correct 5 ms 11724 KB Output is correct
22 Correct 185 ms 91832 KB Output is correct
23 Correct 43 ms 48812 KB Output is correct
24 Correct 38 ms 48716 KB Output is correct
25 Correct 32 ms 28244 KB Output is correct
26 Correct 54 ms 28236 KB Output is correct
27 Correct 277 ms 91908 KB Output is correct
28 Correct 18 ms 28236 KB Output is correct
29 Correct 35 ms 28180 KB Output is correct
30 Correct 21 ms 28208 KB Output is correct
31 Correct 37 ms 28232 KB Output is correct
32 Correct 11 ms 11724 KB Output is correct
33 Correct 4 ms 9692 KB Output is correct
34 Correct 12 ms 28236 KB Output is correct
35 Correct 15 ms 28236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2991 ms 108636 KB Output is correct
2 Correct 3070 ms 108692 KB Output is correct
3 Correct 2952 ms 108484 KB Output is correct
4 Correct 2820 ms 108276 KB Output is correct
5 Correct 2833 ms 108300 KB Output is correct
6 Correct 1117 ms 108176 KB Output is correct
7 Correct 2958 ms 108684 KB Output is correct
8 Correct 3189 ms 108400 KB Output is correct
9 Correct 3016 ms 108484 KB Output is correct
10 Correct 2709 ms 108160 KB Output is correct
11 Correct 2718 ms 108244 KB Output is correct
12 Correct 2622 ms 108280 KB Output is correct
13 Correct 2730 ms 108316 KB Output is correct
14 Correct 2726 ms 108380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 4 ms 9676 KB Output is correct
6 Correct 4 ms 9732 KB Output is correct
7 Correct 5 ms 9676 KB Output is correct
8 Correct 5 ms 9676 KB Output is correct
9 Correct 4 ms 9676 KB Output is correct
10 Correct 4 ms 9672 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 4 ms 9676 KB Output is correct
15 Correct 4 ms 9612 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 4 ms 9676 KB Output is correct
19 Correct 5 ms 9932 KB Output is correct
20 Correct 5 ms 11724 KB Output is correct
21 Correct 5 ms 11724 KB Output is correct
22 Correct 185 ms 91832 KB Output is correct
23 Correct 43 ms 48812 KB Output is correct
24 Correct 38 ms 48716 KB Output is correct
25 Correct 32 ms 28244 KB Output is correct
26 Correct 54 ms 28236 KB Output is correct
27 Correct 277 ms 91908 KB Output is correct
28 Correct 18 ms 28236 KB Output is correct
29 Correct 35 ms 28180 KB Output is correct
30 Correct 21 ms 28208 KB Output is correct
31 Correct 37 ms 28232 KB Output is correct
32 Correct 11 ms 11724 KB Output is correct
33 Correct 4 ms 9692 KB Output is correct
34 Correct 12 ms 28236 KB Output is correct
35 Correct 15 ms 28236 KB Output is correct
36 Correct 6 ms 10060 KB Output is correct
37 Correct 6 ms 10140 KB Output is correct
38 Correct 6 ms 10060 KB Output is correct
39 Incorrect 6 ms 10060 KB Output isn't correct
40 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 4 ms 9676 KB Output is correct
6 Correct 4 ms 9732 KB Output is correct
7 Correct 5 ms 9676 KB Output is correct
8 Correct 5 ms 9676 KB Output is correct
9 Correct 4 ms 9676 KB Output is correct
10 Correct 4 ms 9672 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 4 ms 9676 KB Output is correct
15 Correct 4 ms 9612 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 4 ms 9676 KB Output is correct
19 Correct 5 ms 9932 KB Output is correct
20 Correct 5 ms 11724 KB Output is correct
21 Correct 5 ms 11724 KB Output is correct
22 Correct 185 ms 91832 KB Output is correct
23 Correct 43 ms 48812 KB Output is correct
24 Correct 38 ms 48716 KB Output is correct
25 Correct 32 ms 28244 KB Output is correct
26 Correct 54 ms 28236 KB Output is correct
27 Correct 277 ms 91908 KB Output is correct
28 Correct 18 ms 28236 KB Output is correct
29 Correct 35 ms 28180 KB Output is correct
30 Correct 21 ms 28208 KB Output is correct
31 Correct 37 ms 28232 KB Output is correct
32 Correct 11 ms 11724 KB Output is correct
33 Correct 4 ms 9692 KB Output is correct
34 Correct 12 ms 28236 KB Output is correct
35 Correct 15 ms 28236 KB Output is correct
36 Correct 6 ms 10060 KB Output is correct
37 Correct 6 ms 10140 KB Output is correct
38 Correct 6 ms 10060 KB Output is correct
39 Incorrect 6 ms 10060 KB Output isn't correct
40 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 4 ms 9676 KB Output is correct
6 Correct 4 ms 9732 KB Output is correct
7 Correct 5 ms 9676 KB Output is correct
8 Correct 5 ms 9676 KB Output is correct
9 Correct 4 ms 9676 KB Output is correct
10 Correct 4 ms 9672 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 4 ms 9676 KB Output is correct
15 Correct 4 ms 9612 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 4 ms 9676 KB Output is correct
19 Correct 5 ms 9932 KB Output is correct
20 Correct 5 ms 11724 KB Output is correct
21 Correct 5 ms 11724 KB Output is correct
22 Correct 185 ms 91832 KB Output is correct
23 Correct 43 ms 48812 KB Output is correct
24 Correct 38 ms 48716 KB Output is correct
25 Correct 32 ms 28244 KB Output is correct
26 Correct 54 ms 28236 KB Output is correct
27 Correct 277 ms 91908 KB Output is correct
28 Correct 18 ms 28236 KB Output is correct
29 Correct 35 ms 28180 KB Output is correct
30 Correct 21 ms 28208 KB Output is correct
31 Correct 37 ms 28232 KB Output is correct
32 Correct 11 ms 11724 KB Output is correct
33 Correct 4 ms 9692 KB Output is correct
34 Correct 12 ms 28236 KB Output is correct
35 Correct 15 ms 28236 KB Output is correct
36 Correct 2991 ms 108636 KB Output is correct
37 Correct 3070 ms 108692 KB Output is correct
38 Correct 2952 ms 108484 KB Output is correct
39 Correct 2820 ms 108276 KB Output is correct
40 Correct 2833 ms 108300 KB Output is correct
41 Correct 1117 ms 108176 KB Output is correct
42 Correct 2958 ms 108684 KB Output is correct
43 Correct 3189 ms 108400 KB Output is correct
44 Correct 3016 ms 108484 KB Output is correct
45 Correct 2709 ms 108160 KB Output is correct
46 Correct 2718 ms 108244 KB Output is correct
47 Correct 2622 ms 108280 KB Output is correct
48 Correct 2730 ms 108316 KB Output is correct
49 Correct 2726 ms 108380 KB Output is correct
50 Correct 6 ms 10060 KB Output is correct
51 Correct 6 ms 10140 KB Output is correct
52 Correct 6 ms 10060 KB Output is correct
53 Incorrect 6 ms 10060 KB Output isn't correct
54 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 4 ms 9676 KB Output is correct
6 Correct 4 ms 9732 KB Output is correct
7 Correct 5 ms 9676 KB Output is correct
8 Correct 5 ms 9676 KB Output is correct
9 Correct 4 ms 9676 KB Output is correct
10 Correct 4 ms 9672 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 4 ms 9676 KB Output is correct
15 Correct 4 ms 9612 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 4 ms 9676 KB Output is correct
19 Correct 5 ms 9932 KB Output is correct
20 Correct 5 ms 11724 KB Output is correct
21 Correct 5 ms 11724 KB Output is correct
22 Correct 185 ms 91832 KB Output is correct
23 Correct 43 ms 48812 KB Output is correct
24 Correct 38 ms 48716 KB Output is correct
25 Correct 32 ms 28244 KB Output is correct
26 Correct 54 ms 28236 KB Output is correct
27 Correct 277 ms 91908 KB Output is correct
28 Correct 18 ms 28236 KB Output is correct
29 Correct 35 ms 28180 KB Output is correct
30 Correct 21 ms 28208 KB Output is correct
31 Correct 37 ms 28232 KB Output is correct
32 Correct 11 ms 11724 KB Output is correct
33 Correct 4 ms 9692 KB Output is correct
34 Correct 12 ms 28236 KB Output is correct
35 Correct 15 ms 28236 KB Output is correct
36 Correct 2991 ms 108636 KB Output is correct
37 Correct 3070 ms 108692 KB Output is correct
38 Correct 2952 ms 108484 KB Output is correct
39 Correct 2820 ms 108276 KB Output is correct
40 Correct 2833 ms 108300 KB Output is correct
41 Correct 1117 ms 108176 KB Output is correct
42 Correct 2958 ms 108684 KB Output is correct
43 Correct 3189 ms 108400 KB Output is correct
44 Correct 3016 ms 108484 KB Output is correct
45 Correct 2709 ms 108160 KB Output is correct
46 Correct 2718 ms 108244 KB Output is correct
47 Correct 2622 ms 108280 KB Output is correct
48 Correct 2730 ms 108316 KB Output is correct
49 Correct 2726 ms 108380 KB Output is correct
50 Correct 6 ms 10060 KB Output is correct
51 Correct 6 ms 10140 KB Output is correct
52 Correct 6 ms 10060 KB Output is correct
53 Incorrect 6 ms 10060 KB Output isn't correct
54 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 4 ms 9676 KB Output is correct
6 Correct 4 ms 9732 KB Output is correct
7 Correct 5 ms 9676 KB Output is correct
8 Correct 5 ms 9676 KB Output is correct
9 Correct 4 ms 9676 KB Output is correct
10 Correct 4 ms 9672 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 4 ms 9676 KB Output is correct
15 Correct 4 ms 9612 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 4 ms 9676 KB Output is correct
19 Correct 5 ms 9932 KB Output is correct
20 Correct 5 ms 11724 KB Output is correct
21 Correct 5 ms 11724 KB Output is correct
22 Correct 185 ms 91832 KB Output is correct
23 Correct 43 ms 48812 KB Output is correct
24 Correct 38 ms 48716 KB Output is correct
25 Correct 32 ms 28244 KB Output is correct
26 Correct 54 ms 28236 KB Output is correct
27 Correct 277 ms 91908 KB Output is correct
28 Correct 18 ms 28236 KB Output is correct
29 Correct 35 ms 28180 KB Output is correct
30 Correct 21 ms 28208 KB Output is correct
31 Correct 37 ms 28232 KB Output is correct
32 Correct 11 ms 11724 KB Output is correct
33 Correct 4 ms 9692 KB Output is correct
34 Correct 12 ms 28236 KB Output is correct
35 Correct 15 ms 28236 KB Output is correct
36 Correct 2991 ms 108636 KB Output is correct
37 Correct 3070 ms 108692 KB Output is correct
38 Correct 2952 ms 108484 KB Output is correct
39 Correct 2820 ms 108276 KB Output is correct
40 Correct 2833 ms 108300 KB Output is correct
41 Correct 1117 ms 108176 KB Output is correct
42 Correct 2958 ms 108684 KB Output is correct
43 Correct 3189 ms 108400 KB Output is correct
44 Correct 3016 ms 108484 KB Output is correct
45 Correct 2709 ms 108160 KB Output is correct
46 Correct 2718 ms 108244 KB Output is correct
47 Correct 2622 ms 108280 KB Output is correct
48 Correct 2730 ms 108316 KB Output is correct
49 Correct 2726 ms 108380 KB Output is correct
50 Correct 6 ms 10060 KB Output is correct
51 Correct 6 ms 10140 KB Output is correct
52 Correct 6 ms 10060 KB Output is correct
53 Incorrect 6 ms 10060 KB Output isn't correct
54 Halted 0 ms 0 KB -