Submission #882293

# Submission time Handle Problem Language Result Execution time Memory
882293 2023-12-03T03:14:00 Z noompty Factories (JOI14_factories) C++17
100 / 100
3695 ms 395432 KB
#include <iostream>
#include <vector>
#include <tuple>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <cmath>
#include <array>
#include <random>
#include <string>
#include <cassert>
#include <climits>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include "factories.h"
using namespace std;
 
#define ll long long
#define f first
#define s second
 
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
 
template<typename A> void __print(const A &x);
template<typename A, typename B> void __print(const pair<A, B> &p);
template<typename... A> void __print(const tuple<A...> &t);
template<typename T> void __print(stack<T> s);
template<typename T> void __print(queue<T> q);
template<typename T, typename... U> void __print(priority_queue<T, U...> q);
 
template<typename A> void __print(const A &x) {
    bool first = true;
    cerr << '{';
    for (const auto &i : x) {
        cerr << (first ? "" : ","), __print(i);
        first = false;
    }
    cerr << '}';
}
 
template<typename A, typename B> void __print(const pair<A, B> &p) {
    cerr << '(';
    __print(p.f);
    cerr << ',';
    __print(p.s);
    cerr << ')';
}
 
template<typename... A> void __print(const tuple<A...> &t) {
    bool first = true;
    cerr << '(';
    apply([&first] (const auto &...args) { ((cerr << (first ? "" : ","), __print(args), first = false), ...); }, t);
    cerr << ')';
}
 
template<typename T> void __print(stack<T> s) {
    vector<T> debugVector;
    while (!s.empty()) {
        T t = s.top();
        debugVector.push_back(t);
        s.pop();
    }
    reverse(debugVector.begin(), debugVector.end());
    __print(debugVector);
}
 
template<typename T> void __print(queue<T> q) {
    vector<T> debugVector;
    while (!q.empty()) {
        T t = q.front();
        debugVector.push_back(t);
        q.pop();
    }
    __print(debugVector);
}
 
template<typename T, typename... U> void __print(priority_queue<T, U...> q) {
    vector<T> debugVector;
    while (!q.empty()) {
        T t = q.top();
        debugVector.push_back(t);
        q.pop();
    }
    __print(debugVector);
}
 
void _print() { cerr << "]\n"; }
 
template <typename Head, typename... Tail> void _print(const Head &H, const Tail &...T) {
    __print(H);
    if (sizeof...(T)) cerr << ", ";
    _print(T...);
}
 
#ifdef DEBUG
	#define D(...) cerr << "Line: " << __LINE__ << " [" << #__VA_ARGS__ << "] = ["; _print(__VA_ARGS__)
#else
	#define D(...)
#endif

int n;
ll sz[500005], ans[500005];
bool used[500005];
vector<pair<ll, ll>> adj[500005], anc[500005];

int dfssz(int curr, int par) {
    sz[curr] = 1;
    for (auto [i, d] : adj[curr]) {
        if (i == par || used[i]) continue;
        sz[curr] += dfssz(i, curr);
    }
    return sz[curr];
}

int dfscen(int curr, int par, int size) {
    for (auto [i, d] : adj[curr]) {
        if (i != par && sz[i] > size / 2 && !used[i]) {
            return dfscen(i, curr, size);
        }
    }
    return curr;
}

void getdist(int curr, int cen, int par, ll dist) {
    for (auto [i, d] : adj[curr]) {
        if (i == par || used[i]) continue;
        getdist(i, cen, curr, dist + d);
    }
    anc[curr].push_back({cen, dist});
}

void decomp(int curr) {
    int cen = dfscen(curr, -1, dfssz(curr, -1));
    for (auto [i, d] : adj[cen]) {
        if (used[i]) continue;
        getdist(i, cen, cen, d);
    }
    used[cen] = true;
    for (auto [i, d] : adj[cen]) {
        if (used[i]) continue;
        decomp(i);
    }
}

void Init(int N, int A[], int B[], int D[]) {
    n = N;
    for (int i = 0; i < n - 1; i++) {
        adj[A[i]].push_back({B[i], D[i]});
        adj[B[i]].push_back({A[i], D[i]});
        ans[i] = 1e18;
    }
    ans[n - 1] = 1e18;
    decomp(0);
    for (int i = 0; i < n; i++) {
        anc[i].push_back({i, 0});
    }
}

ll Query(int S, int X[], int T, int Y[]) {
    vector<int> v;
    for (int i = 0; i < S; i++) {
        for (auto [i, d] : anc[X[i]]) {
            v.push_back(i);
            ans[i] = min(ans[i], d);
        }
    }
    ll ret = 1e18;
    for (int i = 0; i < T; i++) {
        for (auto [i, d] : anc[Y[i]]) {
            ret = min(ret, d + ans[i]);
        }
    }
    for (int i : v) {
        ans[i] = 1e18;
    }
    return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 45936 KB Output is correct
2 Correct 207 ms 55712 KB Output is correct
3 Correct 237 ms 56292 KB Output is correct
4 Correct 223 ms 56704 KB Output is correct
5 Correct 227 ms 56828 KB Output is correct
6 Correct 168 ms 55232 KB Output is correct
7 Correct 220 ms 56144 KB Output is correct
8 Correct 222 ms 56364 KB Output is correct
9 Correct 225 ms 56828 KB Output is correct
10 Correct 169 ms 55304 KB Output is correct
11 Correct 222 ms 56280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 45660 KB Output is correct
2 Correct 1890 ms 222736 KB Output is correct
3 Correct 2729 ms 315104 KB Output is correct
4 Correct 676 ms 125232 KB Output is correct
5 Correct 3485 ms 382680 KB Output is correct
6 Correct 2637 ms 315980 KB Output is correct
7 Correct 744 ms 97828 KB Output is correct
8 Correct 275 ms 74940 KB Output is correct
9 Correct 854 ms 113360 KB Output is correct
10 Correct 752 ms 98360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 45936 KB Output is correct
2 Correct 207 ms 55712 KB Output is correct
3 Correct 237 ms 56292 KB Output is correct
4 Correct 223 ms 56704 KB Output is correct
5 Correct 227 ms 56828 KB Output is correct
6 Correct 168 ms 55232 KB Output is correct
7 Correct 220 ms 56144 KB Output is correct
8 Correct 222 ms 56364 KB Output is correct
9 Correct 225 ms 56828 KB Output is correct
10 Correct 169 ms 55304 KB Output is correct
11 Correct 222 ms 56280 KB Output is correct
12 Correct 10 ms 45660 KB Output is correct
13 Correct 1890 ms 222736 KB Output is correct
14 Correct 2729 ms 315104 KB Output is correct
15 Correct 676 ms 125232 KB Output is correct
16 Correct 3485 ms 382680 KB Output is correct
17 Correct 2637 ms 315980 KB Output is correct
18 Correct 744 ms 97828 KB Output is correct
19 Correct 275 ms 74940 KB Output is correct
20 Correct 854 ms 113360 KB Output is correct
21 Correct 752 ms 98360 KB Output is correct
22 Correct 2001 ms 239032 KB Output is correct
23 Correct 2195 ms 244612 KB Output is correct
24 Correct 3179 ms 328628 KB Output is correct
25 Correct 3150 ms 328612 KB Output is correct
26 Correct 2990 ms 321372 KB Output is correct
27 Correct 3695 ms 395432 KB Output is correct
28 Correct 766 ms 132652 KB Output is correct
29 Correct 2877 ms 320944 KB Output is correct
30 Correct 3064 ms 321180 KB Output is correct
31 Correct 2978 ms 321452 KB Output is correct
32 Correct 938 ms 120040 KB Output is correct
33 Correct 262 ms 76024 KB Output is correct
34 Correct 539 ms 92496 KB Output is correct
35 Correct 577 ms 93252 KB Output is correct
36 Correct 686 ms 97068 KB Output is correct
37 Correct 740 ms 97288 KB Output is correct