답안 #1108992

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1108992 2024-11-05T18:33:14 Z doducanh 공장들 (JOI14_factories) C++14
0 / 100
14 ms 47440 KB
#include<bits/stdc++.h>
#include "factories.h"
using namespace std;
#define ll long long
#define fi first
#define se second
#define ii pair<int,ll>
const ll maxn=5e5+7;
const ll inf=1e15+7;
vector<pair<int,ll>>adj[maxn];
vector<int>sz(maxn,0);
vector<bool>used(maxn,0);
vector<ll>minn(maxn,inf);
vector<pair<int,ll>>ancestors[maxn];
int dfs(int u,int par)
{
    sz[u]=1;
    for(ii pp:adj[u]){
        int v=pp.fi;
        if(v==par||used[v])continue;
        sz[u]+=dfs(v,u);
    }
    return sz[u];
}
void dfs2(int u,int par,int cen,ll depth)
{
    ancestors[u].push_back({cen,depth});
    for(ii pp:adj[u]){
        int v=pp.fi;
        ll w=pp.se;
        if(v==par||used[v])continue;
        dfs2(v,u,cen,depth+w);
    }
}
int getcen(int u,int par,int treesize)
{
    for(ii pp:adj[u]){
        int v=pp.fi;
        if(v==par||used[v])continue;
        if((sz[v]<<1)>treesize)return getcen(v,u,treesize);
    }
    return u;
}
void buildcen(int u,int par)
{
    int C=getcen(u,-1,dfs(u,-1));
    dfs2(C,-1,C,0);
    used[C]=1;
    for(ii pp:adj[u]){
        int v=pp.fi;
        if(used[v])continue;
        buildcen(v,C);
    }
}
void Init(int N,int A[],int B[],int D[])
{
    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]});
    }
    buildcen(0,-1);
}
long long Query(int S, int X[], int T, int Y[])
{
    for(int i=0;i<T;i++){
        for(pair<int,long long> y:ancestors[Y[i]]){
            minn[y.fi]=inf;
        }
    }
    for(int i=0;i<S;i++){
        for(pair<int,ll> y:ancestors[X[i]]){
            minn[y.fi]=min(minn[y.fi],y.se);
        }
    }
    ll ans=inf;
    for(int i=0;i<T;i++){
        for(pair<int,ll> y:ancestors[Y[i]]){
            ans=min(ans,minn[y.fi]+y.se);
        }
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 47440 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 47440 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 47440 KB Output isn't correct
2 Halted 0 ms 0 KB -