Submission #1097760

#TimeUsernameProblemLanguageResultExecution timeMemory
1097760icebearValley (BOI19_valley)C++14
100 / 100
126 ms44560 KiB
// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;

#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define rep(i, n) for(int i=0; i<(n); ++i)
#define red(i, n) for(int i=(n)-1; i>=0; --i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebearat"

const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll LLinf = 1e18 + 27092008;
const int N = 1e5 + 5;
int numNode, numQuery, numShop, esc;
ll distE[N], f[N];
vector<ii> G[N];
int h[N], tin[N], tout[N], timer = 0;
ii edge[N];
bool isShop[N];
int par[N][21], shop[N];
ll Min[N][21];

void DFS(int u) {
    tin[u] = ++timer;
    if (isShop[u]) {
        f[u] = distE[u];
        shop[u] = 1;
    }

    for(ii e : G[u]) {
        int v, w; tie(v, w) = e;
        if (v == par[u][0]) continue;
        h[v] = h[u] + 1;
        distE[v] = distE[u] + w;
        par[v][0] = u;
        DFS(v);
        f[u] = min(f[u], f[v]);
        shop[u] += shop[v];
    }
    tout[u] = timer;
}

void buildSparse() {
    FOR(i, 1, numNode) Min[i][0] = f[i];
    FOR(j, 1, 20) FOR(i, 1, numNode) {
        par[i][j] = par[par[i][j - 1]][j - 1];
        Min[i][j] = min(Min[i][j - 1], Min[par[i][j - 1]][j - 1]);
    }
}   

ll getQuery(int u, int acs) {
    int dis = h[u] - h[acs] + 1;
    ll res = LLinf;
    FOR(j, 0, 20)
        if ((dis >> j) & 1) {
            res = min(res, Min[u][j]);
            u = par[u][j];
        }
    return res;
}

void solve() {
    cin >> numNode >> numShop >> numQuery >> esc;
    FOR(i, 1, numNode - 1) {
        int u, v, w;
        cin >> u >> v >> w;
        G[u].pb(mp(v, w));
        G[v].pb(mp(u, w));
        edge[i] = mp(u, v);
    }
    FOR(i, 1, numShop) {
        int x; cin >> x;
        isShop[x] = true;
    }

    memset(f, 0x3f, sizeof f);
    DFS(esc);
    FOR(i, 1, numNode) f[i] -= 2 * distE[i];
    buildSparse();

    while(numQuery--) {
        int destroyId, node;
        cin >> destroyId >> node;

        int u, v; tie(u, v) = edge[destroyId];
        int lower_node = (h[u] > h[v] ? u : v);

        if (tin[lower_node] <= tin[node] && tout[node] <= tout[lower_node]) {
            if (shop[lower_node] == 0) cout << "oo\n";
            else cout << distE[node] + getQuery(node, lower_node) << '\n';
        } else cout << "escaped\n";
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int tc = 1;
//     cin >> tc;
    while(tc--) solve();
    return 0;
}

Compilation message (stderr)

valley.cpp: In function 'int main()':
valley.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |         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...