답안 #944284

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944284 2024-03-12T13:59:23 Z Nhoksocqt1 봉쇄 시간 (IOI23_closing) C++17
43 / 100
179 ms 48720 KB
#ifndef Nhoksocqt1
    #include "closing.h"
#endif // Nhoksocqt1
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const bool isMultiTest = 1;
const int MAXN = 200005;

struct State {
    ll dis;
    int u, t;

    State(ll _dis = 0, int _u = 0, int _t = 0) : dis(_dis), u(_u), t(_t) {};

    bool operator < (const State &s) const {
        return (dis != s.dis) ? dis > s.dis : u > s.u;
    }
};

vector<ii> adj[MAXN];
ll lastDist[MAXN], dp[MAXN][2], dist[MAXN][2], sumDepth[MAXN], maxSum;
int mask[MAXN], depth[MAXN], P[MAXN][18], numNode, cityX, cityY;
bool ok[MAXN][2];

void preDfs(int u) {
    for (int it = 0; it < sz(adj[u]); ++it) {
        int v(adj[u][it].fi), w(adj[u][it].se);
        if(v != P[u][0]) {
            sumDepth[v] = sumDepth[u] + w;
            depth[v] = depth[u] + 1;
            P[v][0] = u;
            preDfs(v);
        }
    }
}

int lca(int u, int v) {
    if(depth[u] < depth[v])
        swap(u, v);

    for (int i1 = depth[u] - depth[v]; i1 > 0; i1 ^= i1 & -i1) {
        int i = __builtin_ctz(i1);
        u = P[u][i];
    }

    if(u == v)
        return u;

    for (int i = 31 - __builtin_clz(depth[u]); i >= 0; --i) {
        if(P[u][i] != P[v][i])
            u = P[u][i], v = P[v][i];
    }

    return P[u][0];
}

ll distance(int u, int v) {
    return sumDepth[u] + sumDepth[v] - 2LL * sumDepth[lca(u, v)];
}

int max_score(int _N, int _X, int _Y, ll _K, vector<int> eu, vector<int> ev, vector<int> ew) {
    numNode = _N, cityX = _X, cityY = _Y, maxSum = _K;
    for (int i = 0; i < numNode; ++i)
        adj[i].clear();

    for (int i = 0; i + 1 < numNode; ++i) {
        int u(eu[i]), v(ev[i]), w(ew[i]);
        adj[u].push_back(ii(v, w));
        adj[v].push_back(ii(u, w));
    }

    depth[0] = 1, P[0][0] = -1;
    preDfs(0);

    for (int j = 1; (1 << j) <= numNode; ++j) {
        for (int i = 0; i < numNode; ++i) {
            if(P[i][j - 1] != -1) {
                P[i][j] = P[P[i][j - 1]][j - 1];
            } else {
                P[i][j] = -1;
            }
        }
    }

    for (int i = 0; i < numNode; ++i) {
        ll distx = distance(cityX, i);
        ll disty = distance(cityY, i);
        dist[i][0] = distx, dist[i][1] = disty;
        mask[i] = lastDist[i] = 0;
        dp[i][0] = dp[i][1] = 1e18;
        ok[i][0] = ok[i][1] = 0;
    }

    priority_queue<State> heap;
    heap.push({0, cityX, 0});
    heap.push({0, cityY, 1});
    dp[cityX][0] = dp[cityY][1] = 0;
    ok[cityX][0] = ok[cityY][1] = 1;

    ll sumUsed(0);
    int res(0);
    while(sz(heap)) {
        ll dis(heap.top().dis);
        int u(heap.top().u), t(heap.top().t);
        heap.pop();

        if((mask[u] >> t & 1) || dis != dp[u][t])
            continue;

        if(sumUsed + dis > maxSum)
            break;

        ++res;
        sumUsed += dis;
        lastDist[u] += dis;
        mask[u] |= (1 << t);
        if(ok[u][!t] && !(mask[u] >> (!t) & 1) && dp[u][!t] > max(0LL, dist[u][!t] - lastDist[u])) {
            dp[u][!t] = max(0LL, dist[u][!t] - lastDist[u]);
            heap.push(State(dp[u][!t], u, !t));
        }

        for (int it = 0; it < sz(adj[u]); ++it) {
            int v(adj[u][it].fi);
            ok[v][t] = 1;
            if(!(mask[v] >> t & 1) && dp[v][t] > max(0LL, dist[v][t] - lastDist[v])) {
                dp[v][t] = max(0LL, dist[v][t] - lastDist[v]);
                heap.push(State(dp[v][t], v, t));
            }
        }
    }

    return res;
}

#ifdef Nhoksocqt1

int main(void) {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    #define TASK "closing"
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    vector<int> u, v, w;
    ll k;
    int n, x, y;
    cin >> n >> x >> y >> k;

    u.resize(n - 1);
    v.resize(n - 1);
    w.resize(n - 1);
    for (int i = 0; i + 1 < n; ++i)
        cin >> u[i] >> v[i] >> w[i];

    int maxScore = max_score(n, x, y, k, u, v, w);
    cout << maxScore << '\n';

    return 0;
}

#endif // Nhoksocqt1
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 18264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 179 ms 45904 KB Output is correct
2 Correct 168 ms 48720 KB Output is correct
3 Correct 65 ms 20600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18012 KB Output is correct
2 Correct 3 ms 18012 KB Output is correct
3 Correct 3 ms 18084 KB Output is correct
4 Correct 3 ms 18012 KB Output is correct
5 Correct 3 ms 18012 KB Output is correct
6 Correct 4 ms 18436 KB Output is correct
7 Correct 3 ms 18012 KB Output is correct
8 Correct 3 ms 18012 KB Output is correct
9 Correct 3 ms 18012 KB Output is correct
10 Correct 3 ms 18008 KB Output is correct
11 Correct 3 ms 18012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18012 KB Output is correct
2 Correct 3 ms 18012 KB Output is correct
3 Correct 3 ms 18084 KB Output is correct
4 Correct 3 ms 18012 KB Output is correct
5 Correct 3 ms 18012 KB Output is correct
6 Correct 4 ms 18436 KB Output is correct
7 Correct 3 ms 18012 KB Output is correct
8 Correct 3 ms 18012 KB Output is correct
9 Correct 3 ms 18012 KB Output is correct
10 Correct 3 ms 18008 KB Output is correct
11 Correct 3 ms 18012 KB Output is correct
12 Correct 4 ms 18012 KB Output is correct
13 Correct 3 ms 18012 KB Output is correct
14 Correct 3 ms 18012 KB Output is correct
15 Correct 3 ms 18012 KB Output is correct
16 Correct 3 ms 18012 KB Output is correct
17 Correct 3 ms 18012 KB Output is correct
18 Correct 3 ms 18012 KB Output is correct
19 Correct 4 ms 18012 KB Output is correct
20 Correct 4 ms 18212 KB Output is correct
21 Correct 3 ms 18012 KB Output is correct
22 Correct 3 ms 18012 KB Output is correct
23 Correct 3 ms 18080 KB Output is correct
24 Correct 3 ms 18012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18012 KB Output is correct
2 Correct 3 ms 18012 KB Output is correct
3 Correct 3 ms 18084 KB Output is correct
4 Correct 3 ms 18012 KB Output is correct
5 Correct 3 ms 18012 KB Output is correct
6 Correct 4 ms 18436 KB Output is correct
7 Correct 3 ms 18012 KB Output is correct
8 Correct 3 ms 18012 KB Output is correct
9 Correct 3 ms 18012 KB Output is correct
10 Correct 3 ms 18008 KB Output is correct
11 Correct 3 ms 18012 KB Output is correct
12 Correct 4 ms 18012 KB Output is correct
13 Correct 3 ms 18012 KB Output is correct
14 Correct 3 ms 18012 KB Output is correct
15 Correct 3 ms 18012 KB Output is correct
16 Correct 3 ms 18012 KB Output is correct
17 Correct 3 ms 18012 KB Output is correct
18 Correct 3 ms 18012 KB Output is correct
19 Correct 4 ms 18012 KB Output is correct
20 Correct 4 ms 18212 KB Output is correct
21 Correct 3 ms 18012 KB Output is correct
22 Correct 3 ms 18012 KB Output is correct
23 Correct 3 ms 18080 KB Output is correct
24 Correct 3 ms 18012 KB Output is correct
25 Correct 5 ms 18012 KB Output is correct
26 Correct 5 ms 18264 KB Output is correct
27 Correct 4 ms 18356 KB Output is correct
28 Correct 4 ms 18264 KB Output is correct
29 Correct 5 ms 18268 KB Output is correct
30 Correct 4 ms 18268 KB Output is correct
31 Correct 4 ms 18268 KB Output is correct
32 Correct 4 ms 18268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 18264 KB Output is correct
2 Correct 3 ms 18012 KB Output is correct
3 Correct 3 ms 18012 KB Output is correct
4 Correct 3 ms 18084 KB Output is correct
5 Correct 3 ms 18012 KB Output is correct
6 Correct 3 ms 18012 KB Output is correct
7 Incorrect 3 ms 18012 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 18264 KB Output is correct
2 Correct 3 ms 18012 KB Output is correct
3 Correct 3 ms 18012 KB Output is correct
4 Correct 3 ms 18084 KB Output is correct
5 Correct 3 ms 18012 KB Output is correct
6 Correct 3 ms 18012 KB Output is correct
7 Correct 4 ms 18436 KB Output is correct
8 Correct 3 ms 18012 KB Output is correct
9 Correct 3 ms 18012 KB Output is correct
10 Correct 3 ms 18012 KB Output is correct
11 Correct 3 ms 18008 KB Output is correct
12 Correct 3 ms 18012 KB Output is correct
13 Correct 4 ms 18012 KB Output is correct
14 Correct 3 ms 18012 KB Output is correct
15 Correct 3 ms 18012 KB Output is correct
16 Correct 3 ms 18012 KB Output is correct
17 Correct 3 ms 18012 KB Output is correct
18 Correct 3 ms 18012 KB Output is correct
19 Incorrect 3 ms 18012 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 18264 KB Output is correct
2 Correct 3 ms 18012 KB Output is correct
3 Correct 3 ms 18012 KB Output is correct
4 Correct 3 ms 18084 KB Output is correct
5 Correct 3 ms 18012 KB Output is correct
6 Correct 3 ms 18012 KB Output is correct
7 Correct 4 ms 18436 KB Output is correct
8 Correct 3 ms 18012 KB Output is correct
9 Correct 3 ms 18012 KB Output is correct
10 Correct 3 ms 18012 KB Output is correct
11 Correct 3 ms 18008 KB Output is correct
12 Correct 3 ms 18012 KB Output is correct
13 Correct 4 ms 18012 KB Output is correct
14 Correct 3 ms 18012 KB Output is correct
15 Correct 3 ms 18012 KB Output is correct
16 Correct 3 ms 18012 KB Output is correct
17 Correct 3 ms 18012 KB Output is correct
18 Correct 3 ms 18012 KB Output is correct
19 Correct 3 ms 18012 KB Output is correct
20 Correct 4 ms 18012 KB Output is correct
21 Correct 4 ms 18212 KB Output is correct
22 Correct 3 ms 18012 KB Output is correct
23 Correct 3 ms 18012 KB Output is correct
24 Correct 3 ms 18080 KB Output is correct
25 Correct 3 ms 18012 KB Output is correct
26 Incorrect 3 ms 18012 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 18264 KB Output is correct
2 Correct 3 ms 18012 KB Output is correct
3 Correct 3 ms 18012 KB Output is correct
4 Correct 3 ms 18084 KB Output is correct
5 Correct 3 ms 18012 KB Output is correct
6 Correct 3 ms 18012 KB Output is correct
7 Correct 4 ms 18436 KB Output is correct
8 Correct 3 ms 18012 KB Output is correct
9 Correct 3 ms 18012 KB Output is correct
10 Correct 3 ms 18012 KB Output is correct
11 Correct 3 ms 18008 KB Output is correct
12 Correct 3 ms 18012 KB Output is correct
13 Correct 4 ms 18012 KB Output is correct
14 Correct 3 ms 18012 KB Output is correct
15 Correct 3 ms 18012 KB Output is correct
16 Correct 3 ms 18012 KB Output is correct
17 Correct 3 ms 18012 KB Output is correct
18 Correct 3 ms 18012 KB Output is correct
19 Correct 3 ms 18012 KB Output is correct
20 Correct 4 ms 18012 KB Output is correct
21 Correct 4 ms 18212 KB Output is correct
22 Correct 3 ms 18012 KB Output is correct
23 Correct 3 ms 18012 KB Output is correct
24 Correct 3 ms 18080 KB Output is correct
25 Correct 3 ms 18012 KB Output is correct
26 Correct 5 ms 18012 KB Output is correct
27 Correct 5 ms 18264 KB Output is correct
28 Correct 4 ms 18356 KB Output is correct
29 Correct 4 ms 18264 KB Output is correct
30 Correct 5 ms 18268 KB Output is correct
31 Correct 4 ms 18268 KB Output is correct
32 Correct 4 ms 18268 KB Output is correct
33 Correct 4 ms 18268 KB Output is correct
34 Incorrect 3 ms 18012 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
35 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 18264 KB Output is correct
2 Correct 3 ms 18012 KB Output is correct
3 Correct 3 ms 18012 KB Output is correct
4 Correct 3 ms 18084 KB Output is correct
5 Correct 3 ms 18012 KB Output is correct
6 Correct 3 ms 18012 KB Output is correct
7 Correct 4 ms 18436 KB Output is correct
8 Correct 3 ms 18012 KB Output is correct
9 Correct 3 ms 18012 KB Output is correct
10 Correct 3 ms 18012 KB Output is correct
11 Correct 3 ms 18008 KB Output is correct
12 Correct 3 ms 18012 KB Output is correct
13 Correct 4 ms 18012 KB Output is correct
14 Correct 3 ms 18012 KB Output is correct
15 Correct 3 ms 18012 KB Output is correct
16 Correct 3 ms 18012 KB Output is correct
17 Correct 3 ms 18012 KB Output is correct
18 Correct 3 ms 18012 KB Output is correct
19 Correct 3 ms 18012 KB Output is correct
20 Correct 4 ms 18012 KB Output is correct
21 Correct 4 ms 18212 KB Output is correct
22 Correct 3 ms 18012 KB Output is correct
23 Correct 3 ms 18012 KB Output is correct
24 Correct 3 ms 18080 KB Output is correct
25 Correct 3 ms 18012 KB Output is correct
26 Correct 5 ms 18012 KB Output is correct
27 Correct 5 ms 18264 KB Output is correct
28 Correct 4 ms 18356 KB Output is correct
29 Correct 4 ms 18264 KB Output is correct
30 Correct 5 ms 18268 KB Output is correct
31 Correct 4 ms 18268 KB Output is correct
32 Correct 4 ms 18268 KB Output is correct
33 Correct 4 ms 18268 KB Output is correct
34 Incorrect 3 ms 18012 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
35 Halted 0 ms 0 KB -