답안 #1103212

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1103212 2024-10-20T14:24:57 Z InvMOD 공장들 (JOI14_factories) C++14
15 / 100
380 ms 130684 KB
#include<bits/stdc++.h>

#define fi first
#define se second

using namespace std;

using ll = long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 1e5+5;
const int lg = 21;
const ll inf = 1e15;

int n, sz[N];
int par_Cen[N], level[N];
ll dist[lg][N], mn_Dist[N];
bool del[N];
vector<pair<int,int>> adj[N];


int get_Size(int x, int p){
    sz[x] = 1;
    for(pair<int,int> e : adj[x])if(e.fi != p && !del[e.fi]){
        sz[x] += get_Size(e.fi, x);
    }
    return sz[x];
}

int Centroid(int x, int p, int trsz){
    for(pair<int,int> e : adj[x])if(e.fi != p && !del[e.fi] && (sz[e.fi]<<1) > trsz){
        return Centroid(e.fi, x, trsz);
    }
    return x;
}

void build_dist(int x, int p, int cur_lev){
    for(pair<int,int> e : adj[x])if(e.fi != p && !del[e.fi]){
        dist[cur_lev][e.fi] = dist[cur_lev][x] + (1ll * e.se);
        build_dist(e.fi, x, cur_lev);
    }
    return;
}

void decompose(int x, int pre_cen){
    x = Centroid(x, -1, get_Size(x, -1));
    del[x] = true;

    level[x] = level[pre_cen] + 1;
    par_Cen[x] = pre_cen;

    dist[level[x]][x] = 0;
    build_dist(x, -1, level[x]);

    for(pair<int,int> e : adj[x]){
        if(!del[e.fi]){
            decompose(e.fi, x);
        }
    }
    return;
}

ll get_Dist(int a, int b){
    int u = a, v = b;
    if(level[u] > level[v]) swap(u, v);
    while(level[v] > level[u]) v = par_Cen[v];

    while(u != v){
        u = par_Cen[u], v = par_Cen[v];
    }
    return dist[level[v]][a] + dist[level[v]][b];
}

void add_Cen(int a){
    int u = a;
    while(level[u]){
        ckmn(mn_Dist[u], get_Dist(a, u));
        u = par_Cen[u];
    }
    return;
}

void rem_Cen(int u){
    while(level[u]){
        mn_Dist[u] = inf;
        u = par_Cen[u];
    }
    return;
}

ll Query_Cen(int a){
    int u = a; ll answer = inf;
    while(level[u]){
        ckmn(answer, get_Dist(a, u) + mn_Dist[u]);
        u = par_Cen[u];
    }
    return answer;
}

ll Query(int S, int X[], int T, int Y[]){
    for(int i = 0; i < S; i++){
        add_Cen(X[i]+1);
    }

    ll answer = inf;
    for(int i = 0; i < T; i++){
        ckmn(answer, Query_Cen(Y[i]+1));
    }

    for(int i = 0; i < S; i++){
        rem_Cen(X[i]+1);
    }
    return answer;
}

void Init(int _N, int A[], int B[], int D[]){
    n = _N;

    for(int i = 0; i < n-1; i++){
        int u = A[i]+1, v = B[i]+1, w = D[i];
        adj[u].push_back(make_pair(v, w));
        adj[v].push_back(make_pair(u, w));
    }

    decompose(1, 0);
    for(int i = 1; i <= n; i++){
        mn_Dist[i] = inf;
    }
    return;
}


/*
#define name "InvMOD"

#ifdef name

int _n, q, A[N], B[N], D[N];
int S, X[N], T, Y[N];

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

    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    cin >> _n >> q;
    for(int i = 0; i < _n-1; i++){
        cin >> A[i] >> B[i] >> D[i];
    }

    Init(_n, A, B, D);

    while(q--){
        cin >> S >> T;
        for(int i = 0; i < S; i++){
            cin >> X[i];
        }
        for(int i = 0; i < T; i++){
            cin >> Y[i];
        }

        cout << Query(S, X, T, Y) <<"\n";
    }

    return 0;
}

#endif // InvMOD
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 29524 KB Output is correct
2 Correct 259 ms 45172 KB Output is correct
3 Correct 336 ms 47096 KB Output is correct
4 Correct 324 ms 47180 KB Output is correct
5 Correct 380 ms 47476 KB Output is correct
6 Correct 153 ms 38900 KB Output is correct
7 Correct 304 ms 47352 KB Output is correct
8 Correct 315 ms 47224 KB Output is correct
9 Correct 358 ms 47436 KB Output is correct
10 Correct 137 ms 38988 KB Output is correct
11 Correct 304 ms 47180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 29268 KB Output is correct
2 Runtime error 342 ms 130684 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 29524 KB Output is correct
2 Correct 259 ms 45172 KB Output is correct
3 Correct 336 ms 47096 KB Output is correct
4 Correct 324 ms 47180 KB Output is correct
5 Correct 380 ms 47476 KB Output is correct
6 Correct 153 ms 38900 KB Output is correct
7 Correct 304 ms 47352 KB Output is correct
8 Correct 315 ms 47224 KB Output is correct
9 Correct 358 ms 47436 KB Output is correct
10 Correct 137 ms 38988 KB Output is correct
11 Correct 304 ms 47180 KB Output is correct
12 Correct 5 ms 29268 KB Output is correct
13 Runtime error 342 ms 130684 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -