Submission #954744

#TimeUsernameProblemLanguageResultExecution timeMemory
954744Flan312Valley (BOI19_valley)C++17
100 / 100
168 ms37408 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define eb emplace_back
#define task ""
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
#define fi first
#define se second
#define pii pair <int, int>
#define tii tuple <int, int, int>
using namespace std;
const int nmax = 1e5 + 2;
const ll oo = 1e18;
int n, s, q, e;
int h[nmax], up[nmax][17], in[nmax], out[nmax], cnt = 0;
bool shop[nmax];
vector <pii> adj[nmax];
pii edge[nmax];
ll dist[nmax], dp[nmax];
void dfs(int u)
{
    if (!shop[u]) dp[u] = oo;
    in[u] = ++cnt;
    for (auto &[v, w] : adj[u])
    {
        if (v == up[u][0]) continue;
        h[v] = h[u] + 1;
        dist[v] = dist[u] + w;
        up[v][0] = u;
        for (int j = 1; (1 << j) <= n; ++j)
            up[v][j] = up[up[v][j - 1]][j - 1];
        dfs(v);
        dp[u] = min(dp[u], dp[v] + w);
    }
    out[u] = cnt;
}
ll mi[nmax][17];
void dfs2(int u)
{
    for (auto &[v, w] : adj[u])
    {
        if (v == up[u][0]) continue;
        mi[v][0] = dp[v] - dist[v];
        for (int j = 1; (1 << j) <= n; ++j)
            mi[v][j] = min(mi[v][j - 1], mi[up[v][j - 1]][j - 1]);
        dfs2(v);
    }
}
ll get(int u, int k)
{
    ll res = oo;
    for (int j = 0; (1 << j) <= k; ++j)
        if (k >> j & 1)
        {
            res = min(res, mi[u][j]);
            u = up[u][j];
        }
    return res;
}
bool is_ancestor(int u, int v)
{
    return in[u] <= in[v] && in[v] <= out[u];
}
int main()
{
    if (ifstream(task".inp")) nx
    fast
    cin >> n >> s >> q >> e;
    for (int u, v, w, i = 1; i < n; ++i)
    {
        cin >> u >> v >> w;
        adj[u].eb(v, w);
        adj[v].eb(u, w);
        edge[i] = {u, v};
    }
    for (int c, i = 1; i <= s; ++i)
        cin >> c, shop[c] = 1;
    dfs(e);
    dfs2(e);
    while(q--)
    {
        int id, cur_node;
        cin >> id >> cur_node;
        auto [u, v] = edge[id];
        if (in[u] > in[v]) swap(u, v);
        if (!is_ancestor(v, cur_node))
        {
            cout << "escaped\n";
            continue;
        }
        if (dp[v] == oo) cout << "oo\n";
        else cout << get(cur_node, h[cur_node] - h[v] + 1) + dist[cur_node] << '\n';
    }
}

Compilation message (stderr)

valley.cpp: In function 'int main()':
valley.cpp:7:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 | #define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
valley.cpp:67:31: note: in expansion of macro 'nx'
   67 |     if (ifstream(task".inp")) nx
      |                               ^~
valley.cpp:7:52: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 | #define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
      |                                            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:67:31: note: in expansion of macro 'nx'
   67 |     if (ifstream(task".inp")) nx
      |                               ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...