답안 #895457

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
895457 2023-12-30T01:32:15 Z 12345678 공장들 (JOI14_factories) C++17
0 / 100
34 ms 37464 KB
#include "factories.h"
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int nx=5e5+5, kx=19;
ll lvl[nx], pa[nx][kx], ds[nx], c[nx], sz[nx], dp[nx];
vector<pair<ll, ll>> d[nx];
bool used[nx];

void dfs(int u, int p)
{
    lvl[u]=lvl[p]+1;
    pa[u][0]=p;
    for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
    for (auto [v, w]:d[u]) if (v!=p) ds[v]=ds[u]+w, dfs(v, u);
}

int lca(int u, int v)
{
    if (lvl[u]>lvl[v]) swap(u, v);
    for (int i=kx-1; i>=0; i--) if (lvl[pa[v][i]]>=lvl[u]) v=pa[v][i];
    if (u==v) return v;
    for (int i=kx-1; i>=0; i--) if (pa[u][i]!=pa[v][i]) u=pa[u][i], v=pa[v][i];
    return pa[u][0];
}

ll distance(int u, int v)
{
    return ds[u]+ds[v]-2*ds[lca(u, v)];
}

int dfssz(int u, int p)
{
    sz[u]=1;
    for (auto [v, w]:d[u]) if (v!=p&&!used[v]) sz[u]+=dfssz(v, u);
    return sz[u];
}

int findcentroid(int u, int p, int rtsz)
{
    for (auto [v, w]:d[u]) if (v!=p&&!used[v]&&2*sz[v]>rtsz) return findcentroid(v, u, rtsz);
    return u;
}

void decomposition(int u, int p)
{
    u=findcentroid(u, u, dfssz(u, u));
    used[u]=1;
    c[u]=p;
    for (auto [v, w]:d[u]) if (v!=p&&!used[v]) decomposition(v, u);
}

void update(int u, bool remove)
{
    int rt=u;
    while (u!=-1) dp[u]=remove?LLONG_MAX:(dp[u], distance(u, rt)), u=c[u];
}

ll query(int u)
{
    ll tmp=LLONG_MAX, rt=u;
    while (u!=-1) tmp=(dp[u]!=LLONG_MAX)?min(tmp, distance(u, rt)+dp[u]):tmp, u=c[u];
    return tmp;
}

void Init(int N, int A[], int B[], int D[]) {
    for (int i=0; i<N-1; i++) d[A[i]].push_back({B[i], D[i]}), d[B[i]].push_back({A[i], D[i]});
    for (int i=0; i<N; i++) dp[i]=LLONG_MAX;
    dfs(0, 0);
    decomposition(0, -1);
}

long long Query(int S, int X[], int T, int Y[]) {
    ll res=LLONG_MAX;
    for (int i=0; i<S; i++) update(X[i], 0);
    for (int i=0; i<T; i++) res=min(res, query(Y[i]));
    for (int i=0; i<S; i++) update(X[i], 1);
    return res;
}

Compilation message

factories.cpp: In function 'void update(int, bool)':
factories.cpp:59:47: warning: left operand of comma operator has no effect [-Wunused-value]
   59 |     while (u!=-1) dp[u]=remove?LLONG_MAX:(dp[u], distance(u, rt)), u=c[u];
      |                                           ~~~~^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 34 ms 37464 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 37344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 34 ms 37464 KB Output isn't correct
2 Halted 0 ms 0 KB -