Submission #1333850

#TimeUsernameProblemLanguageResultExecution timeMemory
1333850adscodingValley (BOI19_valley)C++20
0 / 100
3097 ms69000 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __print_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        ++s;
        cerr << "  ,  ";
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = { 0 , ( __print_one(s, args) , 0 )... };
    (void)dummy;
    cerr << "  ]\n\n";
}

template<class X>
bool maximize(X &a, X b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

template<class X>
bool minimize(X &a, X b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

// --------------------------------------------------------------------------------------------

const int maxn = 1e5 + 3;
int n, S, q, E;
vector<pii> g[maxn];
pii Eds[maxn];
bitset<maxn> has;

// --------------------------------------------------------------------------------------------



namespace AC
{
    int tin[maxn], tout[maxn], tdfs, hv[maxn], sz[maxn], par[maxn], nx[maxn][20], head[maxn];
    ll dp_down[maxn], dp_up[maxn], vals[maxn], D[maxn];
    vector<ll> PRE[maxn], SUFF[maxn];
    vector<pii> NODES[maxn];
    ll Nhay[maxn][20], NhayUp[maxn][20];

    void dfs_down(int u, int p)
    {
        tin[u] = ++tdfs;
        sz[u] = 1;

        if (has[u])
            dp_down[u] = 0;

        for (const auto &e : g[u])
        {
            if (e.fi == p) continue;
            par[e.fi] = u;
            vals[e.fi] = e.se;
            D[e.fi] = D[u] + e.se;
            dfs_down(e.fi, u);
            sz[u] += sz[e.fi];
            if (sz[e.fi] > sz[hv[u]])
                hv[u] = e.fi;

            NODES[u].push_back(e);
            minimize(dp_down[u], dp_down[e.fi] + e.se);
        }

        PRE[u].assign(int(NODES[u].size()) + 3, ll(1e18));
        SUFF[u].assign(int(NODES[u].size()) + 3, ll(1e18));

        FOR(id, 0, int(NODES[u].size()) - 1)
        {
            auto &e = NODES[u][id];
            PRE[u][id] = dp_down[e.fi] + e.se;
            if (id)
                minimize(PRE[u][id], PRE[u][id - 1]);
        }

        FORD(id, int(NODES[u].size()) - 1, 0)
        {
            auto &e = NODES[u][id];
            SUFF[u][id] = dp_down[e.fi] + e.se;
            if (id < int(NODES[u].size()) - 1)
                minimize(SUFF[u][id], SUFF[u][id + 1]);
        }

        tout[u] = tdfs;
    }

    void dfs_up(int u, int p)
    {
        FOR(id, 0, int(NODES[u].size()) - 1)
        {
            auto &e = NODES[u][id];
            ll cur = has[u] ? 0 : ll(1e18);
            if (id) minimize(cur, PRE[u][id - 1]);
            minimize(cur, SUFF[u][id + 1]);
            minimize(cur, dp_up[u] + vals[u]);
        }

        for (const auto &e : NODES[u])
            dfs_up(e.fi, u);
    }

    int root = 1;

    void hld(int u)
    {
        head[u] = root;
        tin[u] = ++tdfs;

        if (hv[u])
        {
            hld(hv[u]);
            for (const auto &e : NODES[u])
            {
                root = e.fi;
                hld(e.fi);
            }
        }

        tout[u] = tdfs;
    }

    bool onPath(int u, int v, int k)
    {
        while (head[u] != head[v])
        {
            if (tin[u] < tin[v]) swap(u, v);
            if (tin[k] >= tin[head[u]] && tin[k] <= tin[u]) return true;
            u = par[head[u]];
        }
        if (tin[u] > tin[v]) swap(u, v);
        return tin[k] >= tin[u] && tin[k] <= tin[v];
    }

    void solve()
    {
        vals[1] = ll(1e18);
        FOR(i, 1, n)
        {
            dp_down[i] = ll(1e18);
            dp_up[i] = ll(1e18);
        }

        dfs_down(1, -1);
        dfs_up(1, -1);
        hld(1);

        FOR(i, 1, n)
        {
            nx[i][0] = par[i];
            Nhay[i][0] = -D[i] + dp_down[i];
            NhayUp[i][0] = D[par[i]] + dp_up[i];
        }

        FOR(j, 1, 19)
        {
            FOR(i, 1, n)
            {
                nx[i][j] = nx[nx[i][j - 1]][j - 1];
                Nhay[i][j] = min(Nhay[i][j - 1], Nhay[nx[i][j - 1]][j - 1]);
                NhayUp[i][j] = min(NhayUp[i][j - 1], NhayUp[nx[i][j - 1]][j - 1]);
            }
        }

        while (q--)
        {
            int Canh, Dinh; cin >> Canh >> Dinh;
            int u = Eds[Canh].fi, v = Eds[Canh].se;
            if (tin[u] > tin[v]) swap(u, v);
            if (!onPath(Dinh, E, u) || !onPath(Dinh, E, v))
            {
                cout << "escaped\n";
                continue;
            }

            ll res = 1e18;
            ll them = 0;
            if (!(tin[Dinh] <= tin[u] && tout[Dinh] >= tin[u]))
            {
                int org = Dinh;
                for (int i = 19; i >= 0; --i)
                {
                    if (nx[Dinh][i] == 0) continue;
                    int bruh = nx[Dinh][i];
                    if (tin[bruh] <= tin[u] && tout[bruh] >= tin[u]) continue;
                    minimize(res, D[org] + Nhay[Dinh][i]);
                    them += D[Dinh] - D[bruh];
                    Dinh = nx[Dinh][i];
                }
                minimize(res, D[org] + Nhay[Dinh][0]);
                them += vals[Dinh];
                Dinh = par[Dinh];
            }

            if (!(tin[Dinh] >= tin[v] && tin[Dinh] <= tout[v]))
            {
                int org = v;
                FORD(i, 19, 0)
                {
                    if (nx[v][i] == 0) continue;
                    int bruh = nx[u][i];
                    if (tin[Dinh] >= tin[bruh] && tin[Dinh] <= tout[bruh]) continue;
                    minimize(res, them + NhayUp[v][i] - D[Dinh]);
                }
                minimize(res, them + NhayUp[v][0] - D[Dinh]);
                v = par[v];
                them += D[u] - D[Dinh];
                Dinh = u;
                v = org;

                FOR(id, 0, int(NODES[u].size()) - 1)
                {
                    auto &e = NODES[u][id];
                    if (e.fi == v)
                    {
                        ll cur = has[u] ? 0 : ll(1e18);
                        if (id) minimize(cur, PRE[u][id - 1]);
                        minimize(cur, SUFF[u][id + 1]);
                        minimize(res, cur + them);
                        break;
                    }
                }
            }

            cout << res << '\n';
        }
    }
}

void solve()
{
    cin >> n >> S >> q >> E;
    FOR(i, 2, n)
    {
        int u, v, w; cin >> u >> v >> w;
        if (u > v) swap(u, v);
        g[u].push_back({v, w});
        g[v].push_back({u, w});
        Eds[i - 1] = {u, v};
    }

    FOR(i, 1, S)
    {
        int x; cin >> x;
        has[x] = 1;
    }

    AC :: solve();
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    solve();
    return 0;
}

Compilation message (stderr)

valley.cpp: In function 'int main()':
valley.cpp:305:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  305 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:306:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  306 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...