답안 #840635

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
840635 2023-08-31T14:45:45 Z arbuzick 봉쇄 시간 (IOI23_closing) C++17
51 / 100
1000 ms 666256 KB
#include <bits/stdc++.h>

using namespace std;

constexpr int maxn = 2e5 + 5;

constexpr long long inf = (long long)(1e18) + 7;

vector<pair<int, int>> g[maxn];

long long dist_x[maxn], dist_y[maxn];

int prv[maxn];

vector<int> path;

void dfs(int v, long long* dist) {
    for (auto [u, c] : g[v]) {
        if (u != prv[v]) {
            prv[u] = v;
            dist[u] = dist[v] + c;
            dfs(u, dist);
        }
    }
}

void calc_dist(int x, int y) {
    prv[x] = x;
    dfs(x, dist_x);
    prv[y] = y;
    dfs(y, dist_y);
    int nw = x;
    while (nw != y) {
        path.push_back(nw);
        nw = prv[nw];
    }
    path.push_back(y);
}

bool used[maxn];

constexpr int maxn2 = 3005;

long long dp[maxn2][maxn2 * 2][4];

long long res[maxn2 * 2], pr_res[maxn2 * 2], suff_res[maxn2][maxn2 * 2];

int sz[maxn];

void calc_sz(int v, int prv) {
    sz[v] = 1;
    for (auto [u, c] : g[v]) {
        if (!used[u] && u != prv) {
            calc_sz(u, v);
            sz[v] += sz[u];
        }
    }
}

void calc_dp(int v, int prv) {
    int mx = 2;
    for (auto [u, c] : g[v]) {
        if (!used[u] && u != prv) {
            calc_dp(u, v);
            for (int mask = 0; mask < 4; ++mask) {
                for (int cnt_nw = mx; cnt_nw >= 0; --cnt_nw) {
                    if (dp[v][cnt_nw][mask] == inf) {
                        continue;
                    }
                    for (int cnt_add = 0; cnt_add <= sz[u] * 2 && cnt_nw + cnt_add <= sz[v] * 2; ++cnt_add) {
                        dp[v][cnt_nw + cnt_add][mask] = min(dp[v][cnt_nw + cnt_add][mask], dp[v][cnt_nw][mask] + dp[u][cnt_add][mask]);
                    }
                }
            }
            mx += sz[u] * 2;
        }
    }
    for (int cnt = 0; cnt <= sz[v] * 2; ++cnt) {
        dp[v][cnt][1] = min(dp[v][cnt][1], dp[v][cnt][0]);
        dp[v][cnt][2] = min(dp[v][cnt][2], dp[v][cnt][0]);
        dp[v][cnt][3] = min(dp[v][cnt][3], min(dp[v][cnt][1], dp[v][cnt][2]));
    }
}

int max_score(int n, int x, int y, long long k, vector<int> u,
              vector<int> v, vector<int> w) {
    path.clear();
    for (int i = 0; i < n; ++i) {
        g[i].clear();
        dist_x[i] = dist_y[i] = 0;
        used[i] = false;
    }
    for (int i = 0; i < n - 1; ++i) {
        g[u[i]].emplace_back(v[i], w[i]);
        g[v[i]].emplace_back(u[i], w[i]);
    }
    calc_dist(x, y);
    int ans = 0;
    long long k_old = k;
    multiset<long long> ms;
    for (int i = 0; i < n; ++i) {
        ms.insert(dist_x[i]);
        ms.insert(dist_y[i]);
    }
    while (!ms.empty() && *ms.begin() <= k) {
        k -= *ms.begin();
        ms.erase(ms.begin());
        ans++;
    }
    if (n > maxn2) {
        return ans;
    }
    for (int i = 0; i < n; ++i) {
        for (int cnt = 0; cnt < maxn2 * 2; ++cnt) {
            for (int mask = 0; mask < 4; ++mask) {
                dp[i][cnt][mask] = inf;
            }
        }
        dp[i][0][0] = 0;
        dp[i][1][1] = dist_x[i];
        dp[i][1][2] = dist_y[i];
        dp[i][2][3] = max(dist_x[i], dist_y[i]);
    }
    k = k_old;
    for (auto v : path) {
        used[v] = true;
    }
    for (auto v : path) {
        calc_sz(v, v);
        for (int cnt = 0; cnt <= sz[v] * 2; ++cnt) {
            for (int mask = 0; mask < 4; ++mask) {
                dp[v][cnt][mask] = inf;
            }
        }
        dp[v][0][0] = dp[v][0][1] = dp[v][0][2] = dp[v][0][3] = 0;
        calc_dp(v, v);
        // cout << "!" << ' ' << v << ' ' << dp[v][2][0] << ' ' << dp[v][2][1] << ' ' << dp[v][2][2] << ' ' << dp[v][2][3] << endl;
    }
    pr_res[0] = 0;
    for (int i = 1; i <= n * 2; ++i) {
        pr_res[i] = inf;
    }
    suff_res[path.size()][0] = 0;
    for (int i = 1; i <= n * 2; ++i) {
        suff_res[path.size()][i] = inf;
    }
    for (int i = (int)path.size() - 1; i >= 0; --i) {
        for (int cnt_nw = n * 2; cnt_nw >= 0; --cnt_nw) {
            suff_res[i][cnt_nw] = suff_res[i + 1][cnt_nw];
            if (suff_res[i + 1][cnt_nw] == inf) {
                continue;
            }
            for (int cnt_add = 0; cnt_add <= sz[path[i]] * 2 && cnt_nw + cnt_add <= n * 2; ++cnt_add) {
                suff_res[i][cnt_nw + cnt_add] = min(suff_res[i][cnt_nw + cnt_add], suff_res[i + 1][cnt_nw] + dp[path[i]][cnt_add][2]);
            }
        }
    }
    for (int l = 0; l < (int)path.size(); ++l) {
        for (int i = 0; i <= n * 2; ++i) {
            res[i] = pr_res[i];
        }
        for (int r = l + 1; r <= (int)path.size(); ++r) {
            int add_ans = 0;
            long long add_k = 0;
            for (int i = 0; i < l; ++i) {
                add_ans++;
                add_k += dist_x[path[i]];
            }
            for (int i = l; i < r; ++i) {
                add_ans += 2;
                add_k += max(dist_x[path[i]], dist_y[path[i]]);
                if (i == r - 1) {
                    for (int cnt_nw = n * 2; cnt_nw >= 0; --cnt_nw) {
                        if (res[cnt_nw] == inf) {
                            continue;
                        }
                        for (int cnt_add = 0; cnt_add <= sz[path[i]] * 2 && cnt_nw + cnt_add <= n * 2; ++cnt_add) {
                            res[cnt_nw + cnt_add] = min(res[cnt_nw + cnt_add], res[cnt_nw] + dp[path[i]][cnt_add][3]);
                        }
                    }
                }
            }
            for (int i = r; i < (int)path.size(); ++i) {
                add_ans++;
                add_k += dist_y[path[i]];
            }
            while (true) {
                bool change = false;
                if (add_k > k) {
                    break;
                }
                if (add_ans > ans) {
                    ans = add_ans;
                }
                for (int cnt = 0; cnt <= n * 2; ++cnt) {
                    while (ans < n * 2) {
                        if (res[cnt] + add_k + suff_res[r][ans + 1 - cnt - add_ans] <= k) {
                            change = true;
                            ans++;
                        } else {
                            break;
                        }
                    }
                }
                if (!change) {
                    break;
                }
            }
        }
        for (int cnt_nw = n * 2; cnt_nw >= 0; --cnt_nw) {
            if (pr_res[cnt_nw] == inf) {
                continue;
            }
            for (int cnt_add = 0; cnt_add <= sz[path[l]] * 2 && cnt_nw + cnt_add <= n * 2; ++cnt_add) {
                pr_res[cnt_nw + cnt_add] = min(pr_res[cnt_nw + cnt_add], pr_res[cnt_nw] + dp[path[l]][cnt_add][1]);
            }
        }
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 313 ms 45616 KB Output is correct
2 Correct 348 ms 51508 KB Output is correct
3 Execution timed out 1065 ms 209564 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5588 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 4 ms 8788 KB Output is correct
4 Correct 5 ms 8788 KB Output is correct
5 Correct 5 ms 8660 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 6 ms 13828 KB Output is correct
8 Correct 6 ms 13780 KB Output is correct
9 Correct 6 ms 13652 KB Output is correct
10 Correct 6 ms 14676 KB Output is correct
11 Correct 8 ms 14676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5588 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 4 ms 8788 KB Output is correct
4 Correct 5 ms 8788 KB Output is correct
5 Correct 5 ms 8660 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 6 ms 13828 KB Output is correct
8 Correct 6 ms 13780 KB Output is correct
9 Correct 6 ms 13652 KB Output is correct
10 Correct 6 ms 14676 KB Output is correct
11 Correct 8 ms 14676 KB Output is correct
12 Correct 13 ms 23892 KB Output is correct
13 Correct 10 ms 23636 KB Output is correct
14 Correct 14 ms 22484 KB Output is correct
15 Correct 10 ms 22484 KB Output is correct
16 Correct 14 ms 24404 KB Output is correct
17 Correct 11 ms 24452 KB Output is correct
18 Correct 10 ms 8788 KB Output is correct
19 Correct 37 ms 99216 KB Output is correct
20 Correct 41 ms 91156 KB Output is correct
21 Correct 235 ms 98952 KB Output is correct
22 Correct 79 ms 101472 KB Output is correct
23 Correct 264 ms 105220 KB Output is correct
24 Correct 214 ms 105164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5588 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 4 ms 8788 KB Output is correct
4 Correct 5 ms 8788 KB Output is correct
5 Correct 5 ms 8660 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 6 ms 13828 KB Output is correct
8 Correct 6 ms 13780 KB Output is correct
9 Correct 6 ms 13652 KB Output is correct
10 Correct 6 ms 14676 KB Output is correct
11 Correct 8 ms 14676 KB Output is correct
12 Correct 13 ms 23892 KB Output is correct
13 Correct 10 ms 23636 KB Output is correct
14 Correct 14 ms 22484 KB Output is correct
15 Correct 10 ms 22484 KB Output is correct
16 Correct 14 ms 24404 KB Output is correct
17 Correct 11 ms 24452 KB Output is correct
18 Correct 10 ms 8788 KB Output is correct
19 Correct 37 ms 99216 KB Output is correct
20 Correct 41 ms 91156 KB Output is correct
21 Correct 235 ms 98952 KB Output is correct
22 Correct 79 ms 101472 KB Output is correct
23 Correct 264 ms 105220 KB Output is correct
24 Correct 214 ms 105164 KB Output is correct
25 Correct 51 ms 14728 KB Output is correct
26 Correct 330 ms 570672 KB Output is correct
27 Correct 250 ms 530512 KB Output is correct
28 Execution timed out 1087 ms 666256 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6380 KB Output is correct
2 Correct 3 ms 5588 KB Output is correct
3 Correct 5 ms 8788 KB Output is correct
4 Correct 4 ms 8788 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 5 ms 8660 KB Output is correct
7 Correct 4 ms 5716 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 3 ms 6484 KB Output is correct
10 Correct 4 ms 8528 KB Output is correct
11 Correct 3 ms 8532 KB Output is correct
12 Correct 3 ms 7892 KB Output is correct
13 Correct 4 ms 8788 KB Output is correct
14 Correct 3 ms 8532 KB Output is correct
15 Correct 5 ms 8404 KB Output is correct
16 Correct 5 ms 8448 KB Output is correct
17 Correct 4 ms 8404 KB Output is correct
18 Correct 4 ms 8660 KB Output is correct
19 Correct 5 ms 8788 KB Output is correct
20 Correct 5 ms 8404 KB Output is correct
21 Correct 4 ms 8404 KB Output is correct
22 Correct 5 ms 8788 KB Output is correct
23 Correct 4 ms 8788 KB Output is correct
24 Correct 4 ms 8404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6380 KB Output is correct
2 Correct 3 ms 5588 KB Output is correct
3 Correct 5 ms 8788 KB Output is correct
4 Correct 4 ms 8788 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 5 ms 8660 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 6 ms 13828 KB Output is correct
9 Correct 6 ms 13780 KB Output is correct
10 Correct 6 ms 13652 KB Output is correct
11 Correct 6 ms 14676 KB Output is correct
12 Correct 8 ms 14676 KB Output is correct
13 Correct 13 ms 23892 KB Output is correct
14 Correct 10 ms 23636 KB Output is correct
15 Correct 14 ms 22484 KB Output is correct
16 Correct 10 ms 22484 KB Output is correct
17 Correct 14 ms 24404 KB Output is correct
18 Correct 11 ms 24452 KB Output is correct
19 Correct 4 ms 5716 KB Output is correct
20 Correct 3 ms 5716 KB Output is correct
21 Correct 3 ms 6484 KB Output is correct
22 Correct 4 ms 8528 KB Output is correct
23 Correct 3 ms 8532 KB Output is correct
24 Correct 3 ms 7892 KB Output is correct
25 Correct 4 ms 8788 KB Output is correct
26 Correct 3 ms 8532 KB Output is correct
27 Correct 5 ms 8404 KB Output is correct
28 Correct 5 ms 8448 KB Output is correct
29 Correct 4 ms 8404 KB Output is correct
30 Correct 4 ms 8660 KB Output is correct
31 Correct 5 ms 8788 KB Output is correct
32 Correct 5 ms 8404 KB Output is correct
33 Correct 4 ms 8404 KB Output is correct
34 Correct 5 ms 8788 KB Output is correct
35 Correct 4 ms 8788 KB Output is correct
36 Correct 4 ms 8404 KB Output is correct
37 Correct 5 ms 8656 KB Output is correct
38 Correct 7 ms 13312 KB Output is correct
39 Correct 11 ms 23892 KB Output is correct
40 Correct 11 ms 23124 KB Output is correct
41 Correct 12 ms 23892 KB Output is correct
42 Correct 8 ms 23124 KB Output is correct
43 Correct 10 ms 22996 KB Output is correct
44 Correct 10 ms 23252 KB Output is correct
45 Correct 13 ms 24056 KB Output is correct
46 Correct 10 ms 23508 KB Output is correct
47 Correct 12 ms 23892 KB Output is correct
48 Correct 11 ms 21204 KB Output is correct
49 Correct 7 ms 15572 KB Output is correct
50 Correct 5 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6380 KB Output is correct
2 Correct 3 ms 5588 KB Output is correct
3 Correct 5 ms 8788 KB Output is correct
4 Correct 4 ms 8788 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 5 ms 8660 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 6 ms 13828 KB Output is correct
9 Correct 6 ms 13780 KB Output is correct
10 Correct 6 ms 13652 KB Output is correct
11 Correct 6 ms 14676 KB Output is correct
12 Correct 8 ms 14676 KB Output is correct
13 Correct 13 ms 23892 KB Output is correct
14 Correct 10 ms 23636 KB Output is correct
15 Correct 14 ms 22484 KB Output is correct
16 Correct 10 ms 22484 KB Output is correct
17 Correct 14 ms 24404 KB Output is correct
18 Correct 11 ms 24452 KB Output is correct
19 Correct 10 ms 8788 KB Output is correct
20 Correct 37 ms 99216 KB Output is correct
21 Correct 41 ms 91156 KB Output is correct
22 Correct 235 ms 98952 KB Output is correct
23 Correct 79 ms 101472 KB Output is correct
24 Correct 264 ms 105220 KB Output is correct
25 Correct 214 ms 105164 KB Output is correct
26 Correct 4 ms 5716 KB Output is correct
27 Correct 3 ms 5716 KB Output is correct
28 Correct 3 ms 6484 KB Output is correct
29 Correct 4 ms 8528 KB Output is correct
30 Correct 3 ms 8532 KB Output is correct
31 Correct 3 ms 7892 KB Output is correct
32 Correct 4 ms 8788 KB Output is correct
33 Correct 3 ms 8532 KB Output is correct
34 Correct 5 ms 8404 KB Output is correct
35 Correct 5 ms 8448 KB Output is correct
36 Correct 4 ms 8404 KB Output is correct
37 Correct 4 ms 8660 KB Output is correct
38 Correct 5 ms 8788 KB Output is correct
39 Correct 5 ms 8404 KB Output is correct
40 Correct 4 ms 8404 KB Output is correct
41 Correct 5 ms 8788 KB Output is correct
42 Correct 4 ms 8788 KB Output is correct
43 Correct 4 ms 8404 KB Output is correct
44 Correct 5 ms 8656 KB Output is correct
45 Correct 7 ms 13312 KB Output is correct
46 Correct 11 ms 23892 KB Output is correct
47 Correct 11 ms 23124 KB Output is correct
48 Correct 12 ms 23892 KB Output is correct
49 Correct 8 ms 23124 KB Output is correct
50 Correct 10 ms 22996 KB Output is correct
51 Correct 10 ms 23252 KB Output is correct
52 Correct 13 ms 24056 KB Output is correct
53 Correct 10 ms 23508 KB Output is correct
54 Correct 12 ms 23892 KB Output is correct
55 Correct 11 ms 21204 KB Output is correct
56 Correct 7 ms 15572 KB Output is correct
57 Correct 5 ms 9684 KB Output is correct
58 Correct 8 ms 10708 KB Output is correct
59 Correct 18 ms 22740 KB Output is correct
60 Correct 23 ms 24276 KB Output is correct
61 Correct 26 ms 33492 KB Output is correct
62 Correct 20 ms 33492 KB Output is correct
63 Correct 42 ms 99212 KB Output is correct
64 Correct 40 ms 92884 KB Output is correct
65 Correct 59 ms 90828 KB Output is correct
66 Correct 44 ms 93396 KB Output is correct
67 Correct 46 ms 98508 KB Output is correct
68 Correct 128 ms 95252 KB Output is correct
69 Correct 325 ms 103112 KB Output is correct
70 Correct 76 ms 96652 KB Output is correct
71 Correct 70 ms 96312 KB Output is correct
72 Correct 40 ms 96344 KB Output is correct
73 Correct 31 ms 78548 KB Output is correct
74 Correct 40 ms 82624 KB Output is correct
75 Correct 20 ms 23892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6380 KB Output is correct
2 Correct 3 ms 5588 KB Output is correct
3 Correct 5 ms 8788 KB Output is correct
4 Correct 4 ms 8788 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 5 ms 8660 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 6 ms 13828 KB Output is correct
9 Correct 6 ms 13780 KB Output is correct
10 Correct 6 ms 13652 KB Output is correct
11 Correct 6 ms 14676 KB Output is correct
12 Correct 8 ms 14676 KB Output is correct
13 Correct 13 ms 23892 KB Output is correct
14 Correct 10 ms 23636 KB Output is correct
15 Correct 14 ms 22484 KB Output is correct
16 Correct 10 ms 22484 KB Output is correct
17 Correct 14 ms 24404 KB Output is correct
18 Correct 11 ms 24452 KB Output is correct
19 Correct 10 ms 8788 KB Output is correct
20 Correct 37 ms 99216 KB Output is correct
21 Correct 41 ms 91156 KB Output is correct
22 Correct 235 ms 98952 KB Output is correct
23 Correct 79 ms 101472 KB Output is correct
24 Correct 264 ms 105220 KB Output is correct
25 Correct 214 ms 105164 KB Output is correct
26 Correct 51 ms 14728 KB Output is correct
27 Correct 330 ms 570672 KB Output is correct
28 Correct 250 ms 530512 KB Output is correct
29 Execution timed out 1087 ms 666256 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6380 KB Output is correct
2 Correct 3 ms 5588 KB Output is correct
3 Correct 5 ms 8788 KB Output is correct
4 Correct 4 ms 8788 KB Output is correct
5 Correct 5 ms 8788 KB Output is correct
6 Correct 5 ms 8660 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 6 ms 13828 KB Output is correct
9 Correct 6 ms 13780 KB Output is correct
10 Correct 6 ms 13652 KB Output is correct
11 Correct 6 ms 14676 KB Output is correct
12 Correct 8 ms 14676 KB Output is correct
13 Correct 13 ms 23892 KB Output is correct
14 Correct 10 ms 23636 KB Output is correct
15 Correct 14 ms 22484 KB Output is correct
16 Correct 10 ms 22484 KB Output is correct
17 Correct 14 ms 24404 KB Output is correct
18 Correct 11 ms 24452 KB Output is correct
19 Correct 10 ms 8788 KB Output is correct
20 Correct 37 ms 99216 KB Output is correct
21 Correct 41 ms 91156 KB Output is correct
22 Correct 235 ms 98952 KB Output is correct
23 Correct 79 ms 101472 KB Output is correct
24 Correct 264 ms 105220 KB Output is correct
25 Correct 214 ms 105164 KB Output is correct
26 Correct 51 ms 14728 KB Output is correct
27 Correct 330 ms 570672 KB Output is correct
28 Correct 250 ms 530512 KB Output is correct
29 Execution timed out 1087 ms 666256 KB Time limit exceeded
30 Halted 0 ms 0 KB -