답안 #249838

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
249838 2020-07-16T02:20:30 Z AM1648 공장들 (JOI14_factories) C++17
컴파일 오류
0 ms 0 KB
/* be name khoda */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;

#define forifrom(i, s, n) for (ll i = (s); i < (n); ++i)
#define fori(i, n) forifrom(i, 0, n)
#define forirto(i, e, s) for (ll i = (e) - 1; i >= s; --i)
#define forir(i, n) forirto(i, n, 0)
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back

typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
typedef vector<ll> vi;

const ll BIG = 1446803456761533460LL;

const ll maxn = 500010;
const ll maxa = 2000010;
const ll maxnlg = 19;

ll n, nn, m, k, Q;
vpii g[maxn];
ll M[maxa], K[maxa];

ll H[maxn], D[maxn];
ll pars[maxnlg][maxn];
ll st[maxn], ft[maxn];

vpii subt[maxn];
ll ver[maxa];
ll lt[maxn];

bool bomb[maxn];
ll down[maxn], up[maxn];

ll tim = 0;
void dfs_lca(ll x, ll par) {
    st[x] = tim++;
    pars[0][x] = par;
    fori (i, maxnlg - 1) {
        pars[i + 1][x] = pars[i][pars[i][x]];
    }
    for (auto y : g[x]) {
        ll v = y.ff;
        if (v == par) continue;
        D[v] = D[x] + y.ss;
        H[v] = H[x] + 1;
        dfs_lca(v, x);
    }
    ft[x] = tim;
}

inline bool a_subt_b(ll a, ll b) {
    return st[b] <= st[a] && ft[a] <= ft[b];
}

ll LCA(ll a, ll b) {
    if (st[a] > st[b]) swap(a, b);
    if (ft[b] <= ft[a]) return a;
    forir (i, maxnlg) {
        if (ft[pars[i][a]]>ft[b]) {
            a = pars[i][a];
        }
    }
    return pars[0][a];
}

bool st_cmp(ll a, ll b) {
    return st[a] < st[b];
}

void make_tree() {
    fori (i, m) {
        ver[i] = M[i];
    }
    fori (i, k) {
        ver[m + i] = K[i];
    }
    nn = m + k;
    sort(ver, ver + nn, st_cmp);
    ll nnn = nn - 1;
    fori (i, nnn) {
        ver[nn++] = LCA(ver[i], ver[i + 1]);
    }
    sort(ver, ver + nn, st_cmp);
    nn = unique(ver, ver + nn) - ver;
    forifrom (i, 1, nn) {
        lt[i] = i - 1;
        while (ft[ver[i]] > ft[ver[lt[i]]]) {
            lt[i] = lt[lt[i]];
        }
        subt[ver[lt[i]]].pb({ver[i], D[ver[i]] - D[ver[lt[i]]]});
    }
}

void dfs_down(ll x, ll par) {
    if (bomb[x]) down[x] = 0;
    else down[x] = BIG;
    for (auto y : subt[x]) {
        ll v = y.ff;
        dfs_down(v, x);
        down[x] = min(down[x], down[v] + y.ss);
    }
}

void dfs_up(ll x, ll par) {
    if (bomb[x]) up[x] = 0;
    ll mn = up[x];
    for (auto y : subt[x]) {
        ll v = y.ff;
        if (down[v] + y.ss < mn) {
            mn = down[v] + y.ss;
        }
    }
    for (auto y : subt[x]) {
        ll v = y.ff;
        up[v] = mn + y.ss;
        dfs_up(v, x);
    }
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> Q;
    fori (i, n - 1) {
        ll a, b, w;
        cin >> a >> b >> w;
        g[a].pb({b, w});
        g[b].pb({a, w});
    }

    dfs_lca(0, 0);

    fori (q, Q) {
        cin >> m >> k;
        fori (i, m) {
            cin >> M[i];
        }
        fori (i, k) {
            cin >> K[i];
        }

        make_tree();
        ll rot = ver[0];
        
        fori (i, m) {
            bomb[M[i]] = 1;
        }

        dfs_down(rot, rot);
        up[rot] = BIG;
        dfs_up(rot, rot);
        ll ans = BIG;
        fori (i, k) {
            ans = min(ans, min(down[K[i]], up[K[i]]));
        }
        cout << ans << '\n';

        fori (i, m) {
            bomb[M[i]] = 0;
        }
        fori (i, nn) {
            subt[ver[i]].clear();
        }
    }
    
}

/*
7 3
0 1 4
1 2 4
2 3 5
2 4 6
4 5 5
1 6 3
2 2
0 6
3 4
3 2
0 1 3
4 6
1 1
2
5
*/

Compilation message

/tmp/cce3rodC.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccdhOljv.o:factories.cpp:(.text.startup+0x0): first defined here
/tmp/cce3rodC.o: In function `main':
grader.cpp:(.text.startup+0x36d): undefined reference to `Init(int, int*, int*, int*)'
grader.cpp:(.text.startup+0x3ed): undefined reference to `Query(int, int*, int, int*)'
collect2: error: ld returned 1 exit status