Submission #839580

# Submission time Handle Problem Language Result Execution time Memory
839580 2023-08-30T09:00:14 Z model_code Truck Driver (IOI23_deliveries) C++17
43 / 100
4500 ms 21748 KB
// time_limit/sol_vp_brute_force_reroot.cpp

#include "deliveries.h"
#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;

vector<vector<pair<int, long long>>> g;
vector<long long> weight, sz;
vector<int> up, height, cnt;
vector<bool> vis;

pair<int, int> dfs0(int x, int p, int d){
    vis[x] = true;
    up[x] = p;
    pair<int, int> res = {d, x};
    for(auto [y, w] : g[x]){
        if(!vis[y]){
            res = max(res, dfs0(y, x, d+1));
        }
    }
    return res;
}

void dfs(int x, int p, int w_ = 0){
    vis[x] = true;
    weight[x] = w_;
    up[x] = p;
    sz[x] = cnt[x];
    height[x] = 0;
    for(auto [y, w] : g[x]){
        if(!vis[y]){
            dfs(y, x, w);
            sz[x] += sz[y];
            height[x] = max(height[x], height[y] + 1);
        }
    }
}

long long weight_sum = 0;
int C;

void update(int x, int c){
    cnt[x] += c;
    while(x != -1){
        sz[x] += c;
        weight_sum += weight[x] * c;
        x = up[x];
    }
}

int get_c(int x, long long m){
    for(auto [y, w] : g[x]){
        if(up[y] == x && sz[y]*2 > m){
            weight_sum += (sz[x] - sz[y]*2) * weight[y];
            long long tmp = sz[x];
            sz[x] -= sz[y];
            sz[y] = tmp;
            up[x] = y;
            up[y] = -1;
            weight[x] = weight[y];
            weight[y] = 0;
            return get_c(y, m);
        }
    }
    C = x;
    return x;
}

void print(){
    for(int i = 0; i < (int)g.size(); i++){
        cout << "i: " << i << " sz: " << sz[i] << " w: " << weight[i] << " up: " << up[i] << " cnt: " << cnt[i] << endl;
    }
}

void init(int N, std::vector<int> U, std::vector<int> V, std::vector<int> T, std::vector<int> W) {
	g.resize(N);
    sz.assign(N, 0);
    up.assign(N, 0);
    height.assign(N, 0);
    weight.assign(N, 0);
    cnt = W;
    cnt[0]++;
    for(int i = 0; i < N-1; i++){
        g[U[i]].emplace_back(V[i], T[i]);
        g[V[i]].emplace_back(U[i], T[i]);
    }

    vis.assign(N, false);
    int tmp = dfs0(0, -1, 0).second;
    vis.assign(N, false);
    auto [d, s] = dfs0(tmp, -1, 0);

    for(int i = 0; i < d/2; i++) s = up[s];

    vis.assign(N, false);
    dfs(s, -1);
    C = s;

    for(int i = 0; i < N; i++){
        weight_sum += sz[i] * weight[i];
    }

    // cout << "C: " << C << endl;
    // cout << "weight sum: " << weight_sum << endl;

    return;
}

long long max_time(int S, int X) {
    if(S == 0) X++;
	update(S, X - cnt[S]);

    get_c(C, sz[C]);

    // cout << "centroid: " << c << endl;
    // print();

    return weight_sum * 2;
}
# Verdict Execution time Memory Grader output
1 Correct 73 ms 7652 KB Output is correct
2 Correct 73 ms 7356 KB Output is correct
3 Correct 76 ms 7676 KB Output is correct
4 Correct 88 ms 7544 KB Output is correct
5 Correct 101 ms 7652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 4 ms 500 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 1 ms 440 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 7652 KB Output is correct
2 Correct 73 ms 7356 KB Output is correct
3 Correct 76 ms 7676 KB Output is correct
4 Correct 88 ms 7544 KB Output is correct
5 Correct 101 ms 7652 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 17 ms 2388 KB Output is correct
9 Correct 254 ms 20300 KB Output is correct
10 Correct 280 ms 20220 KB Output is correct
11 Correct 253 ms 20268 KB Output is correct
12 Correct 145 ms 21588 KB Output is correct
13 Correct 138 ms 21588 KB Output is correct
14 Correct 150 ms 21748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 7652 KB Output is correct
2 Correct 73 ms 7356 KB Output is correct
3 Correct 76 ms 7676 KB Output is correct
4 Correct 88 ms 7544 KB Output is correct
5 Correct 101 ms 7652 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 6 ms 508 KB Output is correct
8 Correct 454 ms 2772 KB Output is correct
9 Execution timed out 4532 ms 20948 KB Time limit exceeded
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 73 ms 7652 KB Output is correct
2 Correct 73 ms 7356 KB Output is correct
3 Correct 76 ms 7676 KB Output is correct
4 Correct 88 ms 7544 KB Output is correct
5 Correct 101 ms 7652 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 4 ms 500 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 3 ms 468 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 17 ms 2388 KB Output is correct
24 Correct 254 ms 20300 KB Output is correct
25 Correct 280 ms 20220 KB Output is correct
26 Correct 253 ms 20268 KB Output is correct
27 Correct 145 ms 21588 KB Output is correct
28 Correct 138 ms 21588 KB Output is correct
29 Correct 150 ms 21748 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 6 ms 508 KB Output is correct
32 Correct 454 ms 2772 KB Output is correct
33 Execution timed out 4532 ms 20948 KB Time limit exceeded
34 Halted 0 ms 0 KB -