답안 #998722

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
998722 2024-06-14T15:04:28 Z mdn2002 봉쇄 시간 (IOI23_closing) C++17
43 / 100
847 ms 78536 KB
/*
Mayoeba Yabureru
*/
#include "closing.h"
#include<bits/stdc++.h>
using namespace std;

int max_score(int n, int x, int y, long long k, vector<int> U, vector<int> V, vector<int> W) {
    x ++, y ++;
    vector dis(n + 1, vector<long long>(2));
    vector<vector<pair<int, long long>>> gr(n + 1);

    for (int i = 0; i < n - 1; i ++) {
        int u = U[i] + 1, v = V[i] + 1, w = W[i];
        gr[u].push_back({v, w});
        gr[v].push_back({u, w});
    }

    function<void(int, int, int)> dfs = [&] (int v, int p, int wt) {
        for (auto [u, w] : gr[v]) {
            if (u == p) continue;
            dis[u][wt] = dis[v][wt] + w;
            dfs(u, v, wt);
        }
    };
    dfs(x, 0, 0), dfs(y, 0, 1);

    vector<int> vc, path, onpath(n + 1);
    function<void(int, int)> go = [&] (int v, int p) {
        vc.push_back(v);
        if (v == y) path = vc;
        for (auto [u, w] : gr[v]) {
            if (u == p) continue;
            go(u, v);
        }
        vc.pop_back();
    };
    go(x, 0);

    long long minstart = 0, mn = 1e16;
    for (auto mid : path) {
        onpath[mid] = 1;
        if (max(dis[mid][0], dis[mid][1]) < mn) {
            mn = max(dis[mid][0], dis[mid][1]);
            minstart = mid;
        }
    }

    multiset<pair<long long, int>> ms;

    vector<int> did(n + 1);
    int ans = 0;

    function f = [&] {
        multiset<pair<long long, int>> s;
        for (int i = 1; i <= n; i ++) {
            if (did[i]) continue;
            s.insert({dis[i][0], i});
            s.insert({dis[i][1], i});
        }
        int cnt = 0;
        while (s.size()) {
            auto [x, y] = *s.begin();
            s.erase(s.begin());
            if (did[y]) continue;
            if (x > k) break;
            k -= x;
            did[y] = 1;
            cnt ++;
        }
        return cnt;
    };

    long long kk = k;
    ans = f();

    for (int i = 1; i <= n; i ++) did[i] = 0;
    int cnt = 2;
    k = kk - mn;

    for (auto x : path) {
        did[x] = 1;
        if (x == minstart) break;
        cnt ++;
        k -= dis[x][0];
    }

    for (int i = path.size() - 1; i >= 0; i --) {
        int x = path[i];
        did[x] = 1;
        if (x == minstart) break;
        cnt ++;
        k -= dis[x][1];
    }
    if (k < 0) return ans;

    for (int i = 1; i <= n; i ++) {
        if (i == minstart) continue;
        if (onpath[i] == 0) ms.insert({max(dis[i][0], dis[i][1]), i});
        else ms.insert({abs(dis[i][0] - dis[i][1]), i});
    }

    while (ms.size()) {
        vector<int> odid = did;
        long long kk = k;
        ans = max(ans, cnt + f());

        did = odid;
        k = kk;
        auto [x, y] = *ms.begin();
        ms.erase(ms.begin());
        if (x > k) break;
        k -= x;
        did[y] = 1;
        if (onpath[y] == 0) cnt += 2;
        else cnt ++; // this is wrong but in the case where the tree is linear
    }

    return ans;
}
/*
1
7 0 2 10
0 1 2
0 3 3
1 2 4
2 4 2
2 5 5
5 6 3
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 289 ms 69188 KB Output is correct
2 Correct 306 ms 78536 KB Output is correct
3 Correct 108 ms 5712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 440 KB Output is correct
19 Correct 16 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 8 ms 624 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 440 KB Output is correct
19 Correct 16 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 8 ms 624 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 7 ms 348 KB Output is correct
26 Correct 847 ms 1732 KB Output is correct
27 Correct 8 ms 1628 KB Output is correct
28 Correct 9 ms 1604 KB Output is correct
29 Correct 334 ms 1372 KB Output is correct
30 Correct 2 ms 1368 KB Output is correct
31 Correct 2 ms 1628 KB Output is correct
32 Correct 2 ms 1572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Incorrect 0 ms 344 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Incorrect 0 ms 344 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 440 KB Output is correct
20 Correct 16 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 8 ms 624 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Incorrect 0 ms 344 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 440 KB Output is correct
20 Correct 16 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 8 ms 624 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 7 ms 348 KB Output is correct
27 Correct 847 ms 1732 KB Output is correct
28 Correct 8 ms 1628 KB Output is correct
29 Correct 9 ms 1604 KB Output is correct
30 Correct 334 ms 1372 KB Output is correct
31 Correct 2 ms 1368 KB Output is correct
32 Correct 2 ms 1628 KB Output is correct
33 Correct 2 ms 1572 KB Output is correct
34 Incorrect 0 ms 344 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
35 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 440 KB Output is correct
20 Correct 16 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 8 ms 624 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 600 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 7 ms 348 KB Output is correct
27 Correct 847 ms 1732 KB Output is correct
28 Correct 8 ms 1628 KB Output is correct
29 Correct 9 ms 1604 KB Output is correct
30 Correct 334 ms 1372 KB Output is correct
31 Correct 2 ms 1368 KB Output is correct
32 Correct 2 ms 1628 KB Output is correct
33 Correct 2 ms 1572 KB Output is correct
34 Incorrect 0 ms 344 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
35 Halted 0 ms 0 KB -