답안 #786748

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
786748 2023-07-18T12:26:25 Z winter0101 공장들 (JOI14_factories) C++14
100 / 100
5306 ms 192712 KB
#include "factories.h"
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct edg{
int v;
long long w;
};
vector<edg>g[500009];
vector<edg>lon[500009];
long long dis[500009];
int dep[500009];
int st[500009][21];
int sub[500009];
int use[500009];
int in[500009];
int ck[500009];
int cnt=0;
bool cmp(int i,int j){
return in[i]<in[j];
}
void dfs(int u,int par){
    in[u]=++cnt;
    for (auto v:g[u]){
        if (v.v==par)continue;
        dis[v.v]=v.w+dis[u];
        dep[v.v]=dep[u]+1;
        st[v.v][0]=u;
        dfs(v.v,u);
    }
}
inline int lca(int u,int v){
if (dep[u]<dep[v])swap(u,v);
int k=dep[u]-dep[v];
for (int i=18;i>=0;i--){
    if (k>>i&1){
        u=st[u][i];
    }
}
if (u==v)return u;
for (int i=18;i>=0;i--){
    if (st[u][i]==0||st[v][i]==0)continue;
    if (st[u][i]!=st[v][i]){
        u=st[u][i];
        v=st[v][i];
    }
}
return st[u][0];
}
inline long long cal(int u,int v){
return dis[u]+dis[v]-2*dis[lca(u,v)];
}
long long cac[500009];//min 1
long long buoi[500009];//min 2
void Init(int n,int a[],int b[],int d[]){
for (int i=0;i<n-1;i++){
    a[i]++;
    b[i]++;
    g[a[i]].push_back({b[i],d[i]});
    g[b[i]].push_back({a[i],d[i]});
}
dfs(1,0);
for (int i=1;i<=18;i++){
    for (int j=1;j<=n;j++){
        cac[j]=buoi[j]=1e17;
        st[j][i]=st[st[j][i-1]][i-1];
    }
}
}
long long ans=1e17;
void redfs(int u){
    if (use[u]==1)cac[u]=0;
    if (use[u]==2)buoi[u]=0;
    for (auto v:lon[u]){
        redfs(v.v);
        cac[u]=min(cac[u],cac[v.v]+v.w);
        buoi[u]=min(buoi[u],buoi[v.v]+v.w);
    }
    ans=min(ans,cac[u]+buoi[u]);
}
long long Query(int s,int x[],int t,int y[]){
vector<int>tp;
for (int i=0;i<s;i++){
    use[x[i]+1]=1;
    ck[x[i]+1]=1;
    tp.push_back(x[i]+1);
}
for (int i=0;i<t;i++){
    use[y[i]+1]=2;
    ck[y[i]+1]=1;
    tp.push_back(y[i]+1);
}
sort(tp.begin(),tp.end(),cmp);
for (int i=1;i<(int)tp.size();i++){
    int u=tp[i],v=tp[i-1];
    if (ck[lca(u,v)]==0){
        ck[lca(u,v)]=1;
        tp.push_back(lca(u,v));
    }
}
sort(tp.begin(),tp.end(),cmp);
vector<int>dit;
for (auto v:tp){
    if (dit.empty()){
        dit.push_back(v);
    }
    else {
        while (!dit.empty()&&lca(dit.back(),v)!=dit.back())dit.pop_back();
        lon[dit.back()].push_back({v,cal(dit.back(),v)});
        dit.push_back(v);
    }
}
ans=1e17;
redfs(dit[0]);
for (auto v:tp){
    ck[v]=0;
    lon[v].clear();
    cac[v]=buoi[v]=1e17;
}
for (int i=0;i<s;i++){
    use[x[i]+1]=0;
}
for (int i=0;i<t;i++){
    use[y[i]+1]=0;
}
return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 24448 KB Output is correct
2 Correct 915 ms 42328 KB Output is correct
3 Correct 1201 ms 42472 KB Output is correct
4 Correct 1012 ms 42404 KB Output is correct
5 Correct 762 ms 42688 KB Output is correct
6 Correct 644 ms 42424 KB Output is correct
7 Correct 1184 ms 42448 KB Output is correct
8 Correct 1008 ms 42512 KB Output is correct
9 Correct 769 ms 42876 KB Output is correct
10 Correct 674 ms 42312 KB Output is correct
11 Correct 1176 ms 42368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 24020 KB Output is correct
2 Correct 1612 ms 141764 KB Output is correct
3 Correct 2768 ms 146312 KB Output is correct
4 Correct 1016 ms 138812 KB Output is correct
5 Correct 2076 ms 187292 KB Output is correct
6 Correct 3026 ms 148488 KB Output is correct
7 Correct 2403 ms 66764 KB Output is correct
8 Correct 700 ms 66112 KB Output is correct
9 Correct 1484 ms 73820 KB Output is correct
10 Correct 2481 ms 68148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 24448 KB Output is correct
2 Correct 915 ms 42328 KB Output is correct
3 Correct 1201 ms 42472 KB Output is correct
4 Correct 1012 ms 42404 KB Output is correct
5 Correct 762 ms 42688 KB Output is correct
6 Correct 644 ms 42424 KB Output is correct
7 Correct 1184 ms 42448 KB Output is correct
8 Correct 1008 ms 42512 KB Output is correct
9 Correct 769 ms 42876 KB Output is correct
10 Correct 674 ms 42312 KB Output is correct
11 Correct 1176 ms 42368 KB Output is correct
12 Correct 13 ms 24020 KB Output is correct
13 Correct 1612 ms 141764 KB Output is correct
14 Correct 2768 ms 146312 KB Output is correct
15 Correct 1016 ms 138812 KB Output is correct
16 Correct 2076 ms 187292 KB Output is correct
17 Correct 3026 ms 148488 KB Output is correct
18 Correct 2403 ms 66764 KB Output is correct
19 Correct 700 ms 66112 KB Output is correct
20 Correct 1484 ms 73820 KB Output is correct
21 Correct 2481 ms 68148 KB Output is correct
22 Correct 3552 ms 157696 KB Output is correct
23 Correct 3213 ms 158920 KB Output is correct
24 Correct 4275 ms 162948 KB Output is correct
25 Correct 4361 ms 165956 KB Output is correct
26 Correct 5168 ms 156464 KB Output is correct
27 Correct 3884 ms 192712 KB Output is correct
28 Correct 2070 ms 153212 KB Output is correct
29 Correct 5260 ms 155180 KB Output is correct
30 Correct 5290 ms 154376 KB Output is correct
31 Correct 5306 ms 155096 KB Output is correct
32 Correct 1515 ms 75336 KB Output is correct
33 Correct 991 ms 68784 KB Output is correct
34 Correct 1575 ms 65448 KB Output is correct
35 Correct 1610 ms 65284 KB Output is correct
36 Correct 2191 ms 66444 KB Output is correct
37 Correct 2270 ms 66212 KB Output is correct