답안 #310426

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
310426 2020-10-06T21:59:17 Z mohamedsobhi777 공장들 (JOI14_factories) C++14
100 / 100
5225 ms 170308 KB
#include "factories.h"
#include<bits/stdc++.h>
//#include "grader.cpp"

const int MX = 5e5 + 7 ; 
using namespace std ; 

vector<pair<int,int> > adj[MX];
vector<int> adx[MX];
int st[MX], en[MX] , t ; 
int up[MX][20] ; 
int col[MX] ; 
long long pref[MX] ; 
int _n; 

void dfs(int x, int p ){
        st[x] = ++ t ; 
        up[x][0] = p ; 
        for(int i = 1; i < 20 ; ++ i)
                up[x][i] = up[ up[x][i-1]][i-1] ; 
        for(auto u : adj[x]){
                if(u.first == p)continue ;
                pref[u.first] = pref[x] + u.second ;  
                dfs(u.first , x) ; 
        }
        en[x] = ++ t; 
}

inline bool upper(int u , int v){return st[u] <= st[v] && en[v] <= en[u] ;}
inline bool cmp(int u ,int v){return st[u] < st[v] ;}

int lca(int u ,int v){
        if(upper(u , v))return u ; 
        if(upper(v , u))return v ; 
        for(int i = 19 ; ~ i; -- i){
                if(!upper(up[u][i] , v))
                        u = up[u][i] ; 
        }
        return up[u][0] ; 
}

void edge(int u , int v){
        adx[u].push_back(v) ; 
        adx[v].push_back(u) ; 
}

long long nrst[MX], nerst[MX] ; 

long long dfz(int x, int p){
        long long ret = 1e18 ; 
        if( (col[x]&2) )ret = 0 ; 

        for(auto u : adx[x]){
                if(u == p)continue ; 
                ret = min(ret , pref[u] - pref[x] + dfz(u , x)) ; 
        }

        return nrst[x] = ret ; 
}

long long dfz2(int x, int p){
        long long ret = 1e18 ; 
        if( (col[x]&1) )ret = 0 ; 

        for(auto u : adx[x]){
                if(u == p)continue ; 
                ret = min(ret , pref[u] - pref[x] + dfz2(u , x)) ; 
        }

        return nerst[x] = ret ; 
}

long long answer = 0 ; 

long long solve(int x, int p, long long ner = 1e18 ){
        long long ret = ( (col[x]&1) == 1 ? nrst[x] : 1e18)  ; 

        for(auto u : adx[x]){
                if(u == p)continue; 
                long long add = 1e18 ; 
                for(auto j : adx[x]){
                        if(j == p || j == u)continue ; 
                        add = min(add , nrst[j] + pref[j] - pref[x]) ; 
                }
                ret = min(ret , solve(u , x, min(ner,add )  + pref[u] - pref[x] ) ) ; 

        }
        if((col[x]&1) == 1){
                answer = min(answer, min(ret , ner)) ; 
        }
        return ret;
}

long long build(vector<int> &vec){
        answer = 1e18 ; 
        vec.push_back(0) ; 
        int sz = (int) vec.size() ; 
        sort(vec.begin() , vec.end() , cmp) ;
        for(int i = 1; i < sz; ++ i){
                vec.push_back(lca(vec[i-1] , vec[i])) ; 
        }
        sort(vec.begin() , vec.end(), cmp) ; 
        vec.erase(unique(vec.begin() , vec.end()) , vec.end()) ;
        vector<int> aux = {vec[0]} ; 

        for(int i = 1; i < (int) vec.size() ; ++ i){
                int j = vec[i] ; 
                while(aux.size() > 1 && !upper(aux.back() ,j) ){
                        edge( aux[aux.size() - 2] , aux.back() ) ; 
                        aux.pop_back() ;
                }
                aux.push_back(j) ; 
        }

        while(aux.size() > 1){
                edge(aux[aux.size() - 2] , aux.back()) ; 
                aux.pop_back() ; 
        }

        dfz(0 , 0) ; 
        dfz2(0 , 0) ; 
        for(auto u : vec)
                answer = min(answer , nrst[u] + nerst[u]) ; 

        for(auto u : vec){
                col[u] = 0 ; 
                adx[u].clear() ; 
                nrst[u] = nerst[u] = 1e18 ; 
        }
        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] , v = B[i] , w = D[i] ; 
                adj[u].push_back({v , w}) ; 
                adj[v].push_back({u , w}) ; 
        }
        fill(nrst , nrst + N , 1e18) ; 
        fill(nerst , nerst + N , 1e18) ; 
        dfs(0 , 0) ; 
}

long long Query(int S, int X[], int T, int Y[]) {
        vector<int> vec ; 
        for(int i = 0 ;i < S ; ++ i){
                col[X[i]]|=1;
                vec.push_back(X[i]) ; 
        }
        for(int i = 0 ;i < T ; ++ i){
                vec.push_back(Y[i]) ; 
                col[Y[i]]|=2;
        }
        build(vec) ; 
        return answer ; 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 24312 KB Output is correct
2 Correct 1271 ms 32888 KB Output is correct
3 Correct 1270 ms 32888 KB Output is correct
4 Correct 1261 ms 33144 KB Output is correct
5 Correct 1030 ms 33400 KB Output is correct
6 Correct 918 ms 33016 KB Output is correct
7 Correct 1210 ms 33016 KB Output is correct
8 Correct 1238 ms 33144 KB Output is correct
9 Correct 1024 ms 33108 KB Output is correct
10 Correct 928 ms 33016 KB Output is correct
11 Correct 1205 ms 32992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 24096 KB Output is correct
2 Correct 2078 ms 126992 KB Output is correct
3 Correct 2932 ms 127096 KB Output is correct
4 Correct 1583 ms 127984 KB Output is correct
5 Correct 1968 ms 144376 KB Output is correct
6 Correct 3096 ms 129072 KB Output is correct
7 Correct 2576 ms 52976 KB Output is correct
8 Correct 1468 ms 54124 KB Output is correct
9 Correct 1319 ms 55928 KB Output is correct
10 Correct 2575 ms 54460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 24312 KB Output is correct
2 Correct 1271 ms 32888 KB Output is correct
3 Correct 1270 ms 32888 KB Output is correct
4 Correct 1261 ms 33144 KB Output is correct
5 Correct 1030 ms 33400 KB Output is correct
6 Correct 918 ms 33016 KB Output is correct
7 Correct 1210 ms 33016 KB Output is correct
8 Correct 1238 ms 33144 KB Output is correct
9 Correct 1024 ms 33108 KB Output is correct
10 Correct 928 ms 33016 KB Output is correct
11 Correct 1205 ms 32992 KB Output is correct
12 Correct 19 ms 24096 KB Output is correct
13 Correct 2078 ms 126992 KB Output is correct
14 Correct 2932 ms 127096 KB Output is correct
15 Correct 1583 ms 127984 KB Output is correct
16 Correct 1968 ms 144376 KB Output is correct
17 Correct 3096 ms 129072 KB Output is correct
18 Correct 2576 ms 52976 KB Output is correct
19 Correct 1468 ms 54124 KB Output is correct
20 Correct 1319 ms 55928 KB Output is correct
21 Correct 2575 ms 54460 KB Output is correct
22 Correct 4268 ms 154800 KB Output is correct
23 Correct 3816 ms 156416 KB Output is correct
24 Correct 4720 ms 139336 KB Output is correct
25 Correct 4723 ms 158464 KB Output is correct
26 Correct 5149 ms 154444 KB Output is correct
27 Correct 3788 ms 170308 KB Output is correct
28 Correct 2692 ms 158220 KB Output is correct
29 Correct 5225 ms 154468 KB Output is correct
30 Correct 5172 ms 153976 KB Output is correct
31 Correct 5124 ms 154492 KB Output is correct
32 Correct 2052 ms 73536 KB Output is correct
33 Correct 1670 ms 70584 KB Output is correct
34 Correct 2384 ms 65404 KB Output is correct
35 Correct 2275 ms 64856 KB Output is correct
36 Correct 2651 ms 65312 KB Output is correct
37 Correct 2667 ms 65432 KB Output is correct