Submission #410924

# Submission time Handle Problem Language Result Execution time Memory
410924 2021-05-23T22:36:17 Z AlperenT Factories (JOI14_factories) C++17
100 / 100
5666 ms 343816 KB
#include <bits/stdc++.h>
#include "factories.h"

using namespace std;

const long long N = 5e5 + 5, INF = 1e18 + 5;

long long n, cnt[N], mina[N], ans;
bool blocked[N];

struct Edge{
    long long b, w;
};

vector<Edge> tree[N];
vector<Edge> parent[N];

void subtreecnt(int v, int p){
    cnt[v] = 1;

    for(auto e : tree[v]){
        if(e.b != p && !blocked[e.b]){
            subtreecnt(e.b, v);
            cnt[v] += cnt[e.b];
        }
    }
}

int findcentroid(int v, int p, int root){
    for(auto e : tree[v]){
        if(e.b != p && !blocked[e.b] && cnt[e.b] > cnt[root] / 2){
            return findcentroid(e.b, v, root);
        }
    }
    return v;
}

void findpaths(int v, int p, int centroid, long long curw){
    parent[v].push_back({centroid, curw});

    for(auto e : tree[v]){
        if(e.b != p && !blocked[e.b]) findpaths(e.b, v, centroid, curw + e.w);
    }
}

void decomp(int v){
    subtreecnt(v, -1);

    int centroid = findcentroid(v, -1, v);

    findpaths(centroid, -1, centroid, 0);

    blocked[centroid] = true;

    for(auto e : tree[centroid]) if(!blocked[e.b]) decomp(e.b);
}

void Init(int n, int a[], int b[], int d[]){
    for(int i = 0; i < n - 1; i++){
        tree[a[i]].push_back({b[i], d[i]});
        tree[b[i]].push_back({a[i], d[i]});
    }

    decomp(0);

    for(int i = 0; i < n; i++) reverse(parent[i].begin(), parent[i].end());

    fill(mina, mina + N, INF);
}

long long Query(int s, int x[], int t, int y[]){
    ans = INF;

    for(int i = 0; i < s; i++){
        for(auto e : parent[x[i]]){
            mina[e.b] = min(mina[e.b], e.w);
        }
    }

    for(int i = 0; i < t; i++){
        for(auto e : parent[y[i]]){
            ans = min(ans, mina[e.b] + e.w);
        }
    }

    for(int i = 0; i < s; i++){
        for(auto e : parent[x[i]]){
            mina[e.b] = INF;
        }
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 25 ms 28288 KB Output is correct
2 Correct 378 ms 38204 KB Output is correct
3 Correct 369 ms 38688 KB Output is correct
4 Correct 395 ms 38604 KB Output is correct
5 Correct 402 ms 39000 KB Output is correct
6 Correct 345 ms 37672 KB Output is correct
7 Correct 372 ms 38636 KB Output is correct
8 Correct 378 ms 38684 KB Output is correct
9 Correct 402 ms 39032 KB Output is correct
10 Correct 278 ms 37716 KB Output is correct
11 Correct 374 ms 38580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 27908 KB Output is correct
2 Correct 2926 ms 197772 KB Output is correct
3 Correct 4223 ms 268456 KB Output is correct
4 Correct 1084 ms 91920 KB Output is correct
5 Correct 5075 ms 343112 KB Output is correct
6 Correct 4125 ms 268948 KB Output is correct
7 Correct 1248 ms 73532 KB Output is correct
8 Correct 616 ms 50600 KB Output is correct
9 Correct 1436 ms 87416 KB Output is correct
10 Correct 1250 ms 74888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 28288 KB Output is correct
2 Correct 378 ms 38204 KB Output is correct
3 Correct 369 ms 38688 KB Output is correct
4 Correct 395 ms 38604 KB Output is correct
5 Correct 402 ms 39000 KB Output is correct
6 Correct 345 ms 37672 KB Output is correct
7 Correct 372 ms 38636 KB Output is correct
8 Correct 378 ms 38684 KB Output is correct
9 Correct 402 ms 39032 KB Output is correct
10 Correct 278 ms 37716 KB Output is correct
11 Correct 374 ms 38580 KB Output is correct
12 Correct 19 ms 27908 KB Output is correct
13 Correct 2926 ms 197772 KB Output is correct
14 Correct 4223 ms 268456 KB Output is correct
15 Correct 1084 ms 91920 KB Output is correct
16 Correct 5075 ms 343112 KB Output is correct
17 Correct 4125 ms 268948 KB Output is correct
18 Correct 1248 ms 73532 KB Output is correct
19 Correct 616 ms 50600 KB Output is correct
20 Correct 1436 ms 87416 KB Output is correct
21 Correct 1250 ms 74888 KB Output is correct
22 Correct 3395 ms 197364 KB Output is correct
23 Correct 3434 ms 201976 KB Output is correct
24 Correct 5045 ms 269672 KB Output is correct
25 Correct 5188 ms 273524 KB Output is correct
26 Correct 4740 ms 270716 KB Output is correct
27 Correct 5666 ms 343816 KB Output is correct
28 Correct 1467 ms 95732 KB Output is correct
29 Correct 4711 ms 269800 KB Output is correct
30 Correct 4689 ms 270084 KB Output is correct
31 Correct 4718 ms 270100 KB Output is correct
32 Correct 1508 ms 87764 KB Output is correct
33 Correct 582 ms 50636 KB Output is correct
34 Correct 1034 ms 66028 KB Output is correct
35 Correct 1038 ms 66828 KB Output is correct
36 Correct 1351 ms 72076 KB Output is correct
37 Correct 1323 ms 71912 KB Output is correct