# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1136380 | Marko2604 | 공장들 (JOI14_factories) | C++20 | 0 ms | 0 KiB |
#include <stdio.h>
#include <stdlib.h>
#define MAX_N 500000
#define MAX_Q 100000
#define MAX_SUM_ST 1000000
#define MAX_VALUE 1000000000
static int N, Q;
static int A[MAX_N], B[MAX_N], D[MAX_N];
static int S[MAX_N];
static int T[MAX_N];
static int X[MAX_SUM_ST];
static int Y[MAX_SUM_ST];
static int Qx[MAX_N];
static int Qy[MAX_N];
//#include "factories.h"
#include<bits/stdc++.h>
#define ll long long
#define MAXN 500002
using namespace std;
vector<vector<pair<int,ll>>>Tr(MAXN);
bool used[MAXN]={};
int subtree[MAXN];
vector<vector<pair<int,ll>>>centroids(MAXN);
void subtreedfs(int node, int p=-1)
{
subtree[node]=1;
for(auto i:Tr[node]) if(!used[i.first] && i.first!=p)
{
subtreedfs(i.first,node);
subtree[node]+=subtree[i.first];
}
}
int getCentroid(int node, int subsz, int p=-1)
{
for(auto i:Tr[node]) if(i.first!=p && !used[i.first] && subtree[i.first]*2>subsz) return getCentroid(i.first,subsz,node);
return node;
}
void dfscen(int node, int cn, int p=-1, ll dist=0)
{
centroids[node].push_back({cn, dist});
for(auto i:Tr[node]) if(i.first!=p && !used[i.first]) dfscen(i.first,cn,node, dist+i.second);
}
void cenDec(int node)
{
subtreedfs(node);
int c=getCentroid(node,subtree[node]);
dfscen(c,c);
used[c]=true;
for(auto i:Tr[c]) if(!used[i.first]) cenDec(i.first);
}
unordered_map<int,ll>df;
void Init(int N, int A[], int B[], int D[]) {
for(int i=0;i<N-1;i++)
{
Tr[A[i]].emplace_back(B[i],D[i]);
Tr[B[i]].emplace_back(A[i],D[i]);
}
cenDec(0);
}
long long Query(int S, int X[], int T, int Y[]) {
for(int i=0;i<S;i++)
{
for(auto j:centroids[X[i]]) df[j.first]=LLONG_MAX/4;
}
for(int i=0;i<T;i++)
{
for(auto j:centroids[Y[i]]) df[j.first]=LLONG_MAX/4;
}
for(int i=0;i<T;i++)
{
for(auto j:centroids[Y[i]]) df[j.first]=min(df[j.first],j.second);
}
ll ans=LLONG_MAX/4;
for(int i=0;i<S;i++)
{
for(auto j:centroids[X[i]]) ans=min(ans,df[j.first]+j.second);
}
return ans;
}
int main() {
int i, j, k;
int STop, TTop;
if (2 != scanf("%d%d", &N, &Q)) {
fprintf(stdout, "error: cannot read N and Q.\n");
exit(1);
}
if (!(2 <= N && N <= MAX_N)) {
fprintf(stdout, "error: N is out of bounds.\n");
exit(1);
}
if (!(1 <= Q && Q <= MAX_Q)) {
fprintf(stdout, "error: Q is out of bounds.\n");
exit(1);
}
for (i = 0; i < N - 1; ++i) {
if (1 != scanf("%d", &A[i])) {
fprintf(stdout, "error: cannot read A[%d].\n", i);
exit(1);
}
if (!(0 <= A[i] && A[i] <= N - 1)) {
fprintf(stdout, "error: A[%d] is out of bounds.\n", i);
exit(1);
}
if (1 != scanf("%d", &B[i])) {
fprintf(stdout, "error: cannot read B[%d].\n", i);
exit(1);
}
if (!(0 <= B[i] && B[i] <= N - 1)) {
fprintf(stdout, "error: B[%d] is out of bounds.\n", i);
exit(1);
}
if (A[i] == B[i]) {
fprintf(stdout, "error: B[%d] is equal to A[%d].\n", i, i);
exit(1);
}
if (1 != scanf("%d", &D[i])) {
fprintf(stdout, "error: cannot read D[%d].\n", i);
exit(1);
}
if (!(1 <= D[i] && D[i] <= MAX_VALUE)) {
fprintf(stdout, "error: D[%d] is out of bounds.\n", i);
exit(1);
}
}
STop = 0;
TTop = 0;
for (j = 0; j < Q; ++j) {
if (2 != scanf("%d%d", &S[j], &T[j])) {
fprintf(stdout, "error: cannot read L[%d] and R[%d].\n", j, j);
exit(1);
}
if(STop + S[j] > MAX_SUM_ST) {
fprintf(stdout, "error: S[0] + S[1] + ... + S[%d] is out of bounds.\n", j);
exit(1);
}
if(TTop + T[j] > MAX_SUM_ST) {
fprintf(stdout, "error: T[0] + T[1] + ... + T[%d] is out of bounds.\n", j);
exit(1);
}
for (k = 0; k < S[j]; ++k, ++STop) {
if (1 != scanf("%d", &X[STop])) {
fprintf(stdout, "error: cannot read X[%d][%d].\n", j, k);
exit(1);
}
if (!(0 <= X[STop] && X[STop] <= N - 1)) {
fprintf(stdout, "error: cannot read X[%d][%d].\n", j, k);
exit(1);
}
}
for (k = 0; k < T[j]; ++k, ++TTop) {
if (1 != scanf("%d", &Y[TTop])) {
fprintf(stdout, "error: cannot read Y[%d][%d].\n", j, k);
exit(1);
}
if (!(0 <= Y[TTop] && Y[TTop] <= N - 1)) {
fprintf(stdout, "error: cannot read Y[%d][%d].\n", j, k);
exit(1);
}
}
}
STop = 0;
TTop = 0;
Init(N, A, B, D);
for (j = 0; j < Q; ++j) {
for (k = 0; k < S[j]; k++) {
Qx[k] = X[STop++];
}
for (k = 0; k < T[j]; k++) {
Qy[k] = Y[TTop++];
}
printf("%lld\n", Query(S[j], Qx, T[j], Qy));
}
return 0;
}