답안 #360198

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
360198 2021-01-27T17:52:53 Z shahriarkhan 공장들 (JOI14_factories) C++14
100 / 100
3904 ms 193652 KB
#include<bits/stdc++.h>
#include "factories.h"
using namespace std ;

const int mx = 5e5 + 10 ;
const long long INF = 1e18 ;

vector<pair<int,long long> > edges[mx] ;

int n , sub[mx] , par[mx] , depth[mx] , vis[mx] ;

long long ans[mx] , dist[mx][20] ;

stack<int> st ;

void dfs(int x , int p , int d)
{
    sub[x] = 1 ;
    for(auto i : edges[x])
    {
        if(i.first == p) continue ;
        if(vis[i.first]) continue ;
        if(d) dist[i.first][d-1] = dist[x][d-1] + i.second ;
        dfs(i.first,x,d) ;
        sub[x] += sub[i.first] ;
    }
}

int find_centroid(int x , int p , int range)
{
    for(auto i : edges[x])
    {
        if(i.first==p) continue ;
        if(vis[i.first]) continue ;
        if(sub[i.first]>range)
        {
            return find_centroid(i.first,x,range) ;
        }
    }
    return x ;
}

void build(int x , int p , int d)
{
    dfs(x,-1,d) ;
    int c = find_centroid(x,-1,sub[x]/2) ;
    par[c] = p , vis[c] = 1 , depth[c] = d ;
    for(auto i : edges[c])
    {
        if(vis[i.first]) continue ;
        dist[i.first][d] = i.second ;
        build(i.first,c,d+1) ;
    }
    vis[c] = 0 ;
}

void update(int x)
{
    int cur = x ;
    while(cur)
    {
        ans[cur] = min(ans[cur],dist[x][depth[cur]]) ;
        st.push(cur) ;
        cur = par[cur] ;
    }
}

long long query(int x)
{
    long long res = ans[x] ;
    int cur = x ;
    while(cur)
    {
        res = min(res,ans[cur] + dist[x][depth[cur]]) ;
        cur = par[cur] ;
    }
    return res ;
}

void Init(int N , int A[] , int B[] , int D[])
{
    n = N ;
    for(int i = 0 ; i < (N-1) ; ++i)
    {
        edges[A[i] + 1].push_back({B[i]+1,D[i]}) ;
        edges[B[i] + 1].push_back({A[i]+1,D[i]}) ;
    }
    build(1,0,0) ;
    for(int i = 1 ; i <= N ; ++i) ans[i] = INF ;
}

long long Query(int S , int X[] , int T , int Y[])
{
    for(int i = 0 ; i < S ; ++i)
    {
        update(X[i] + 1) ;
    }
    long long res = INF ;
    for(int i = 0 ; i < T ; ++i)
    {
        res = min(res,query(Y[i] + 1)) ;
    }
    while(!st.empty())
    {
        ans[st.top()] = INF ;
        st.pop() ;
    }
    return res ;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 12652 KB Output is correct
2 Correct 361 ms 22252 KB Output is correct
3 Correct 397 ms 22508 KB Output is correct
4 Correct 439 ms 22464 KB Output is correct
5 Correct 431 ms 22888 KB Output is correct
6 Correct 272 ms 22304 KB Output is correct
7 Correct 393 ms 22380 KB Output is correct
8 Correct 399 ms 22380 KB Output is correct
9 Correct 428 ms 22764 KB Output is correct
10 Correct 276 ms 22276 KB Output is correct
11 Correct 394 ms 22380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 12416 KB Output is correct
2 Correct 1841 ms 141676 KB Output is correct
3 Correct 2626 ms 146028 KB Output is correct
4 Correct 819 ms 152776 KB Output is correct
5 Correct 3399 ms 193652 KB Output is correct
6 Correct 2630 ms 164180 KB Output is correct
7 Correct 961 ms 60560 KB Output is correct
8 Correct 505 ms 60508 KB Output is correct
9 Correct 1093 ms 65260 KB Output is correct
10 Correct 952 ms 62060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 12652 KB Output is correct
2 Correct 361 ms 22252 KB Output is correct
3 Correct 397 ms 22508 KB Output is correct
4 Correct 439 ms 22464 KB Output is correct
5 Correct 431 ms 22888 KB Output is correct
6 Correct 272 ms 22304 KB Output is correct
7 Correct 393 ms 22380 KB Output is correct
8 Correct 399 ms 22380 KB Output is correct
9 Correct 428 ms 22764 KB Output is correct
10 Correct 276 ms 22276 KB Output is correct
11 Correct 394 ms 22380 KB Output is correct
12 Correct 10 ms 12416 KB Output is correct
13 Correct 1841 ms 141676 KB Output is correct
14 Correct 2626 ms 146028 KB Output is correct
15 Correct 819 ms 152776 KB Output is correct
16 Correct 3399 ms 193652 KB Output is correct
17 Correct 2630 ms 164180 KB Output is correct
18 Correct 961 ms 60560 KB Output is correct
19 Correct 505 ms 60508 KB Output is correct
20 Correct 1093 ms 65260 KB Output is correct
21 Correct 952 ms 62060 KB Output is correct
22 Correct 2166 ms 150292 KB Output is correct
23 Correct 2236 ms 151420 KB Output is correct
24 Correct 3128 ms 154404 KB Output is correct
25 Correct 3087 ms 156456 KB Output is correct
26 Correct 3142 ms 151128 KB Output is correct
27 Correct 3904 ms 178252 KB Output is correct
28 Correct 988 ms 147272 KB Output is correct
29 Correct 3104 ms 150636 KB Output is correct
30 Correct 3126 ms 149960 KB Output is correct
31 Correct 3135 ms 150840 KB Output is correct
32 Correct 1113 ms 68412 KB Output is correct
33 Correct 496 ms 61148 KB Output is correct
34 Correct 744 ms 58168 KB Output is correct
35 Correct 765 ms 57964 KB Output is correct
36 Correct 907 ms 59244 KB Output is correct
37 Correct 910 ms 59028 KB Output is correct