Submission #785725

#TimeUsernameProblemLanguageResultExecution timeMemory
785725chanhchuong123Valley (BOI19_valley)C++14
23 / 100
122 ms39212 KiB
#include <bits/stdc++.h>
using namespace std;
#define task ""
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)

template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}

const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};

const long long INF = 1e18 + 7;
const int MAX = 100010;
int N, S, Q, E;
bool isShop[MAX];
vector<pair<int, int>> adj[MAX];
pair<int, pair<int, int>> edges[MAX];

int timer;
int tin[MAX], tout[MAX];
int h[MAX], anc[17][MAX];
long long dep[MAX], down[MAX], dp[17][MAX];
void dfs(int u, int p) { // calc down[_], dp[_]
    tin[u] = ++timer;
    if (isShop[u]) {
        down[u] = 0;
    }
    for (pair<int, int> &x: adj[u]) if (x.fi != p) {
        int v = x.fi, w = x.se;
        dep[v] = dep[u] + w;
        h[v] = h[u] + 1;
        anc[0][v] = u;
        dfs(v, u);
        for (int j = 1; j <= 16; ++j) {
            anc[j][v] = anc[j - 1][anc[j - 1][v]];
        }
        minimize(down[u], down[v] + w);
    }
    for (pair<int, int> &x: adj[u]) if (x.fi != p) {
        int v = x.fi;
        minimize(dp[0][v], down[u] - dep[u]);
    }
    tout[u] = timer;
}

long long jump(int u, int p) {
    long long res = down[u] - dep[u];
    int k = h[u] - h[p];
    for (int j = 0; (1 << j) <= k; ++j) {
        if (k >> j & 1) {
            res = min(res, dp[j][u]);
            u = anc[j][u];
        }
    }
    return res;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);

	if (fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}

	cin >> N >> S >> Q >> E;
	for (int i = 1; i < N; ++i) {
        int u, v, w; cin >> u >> v >> w;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
        edges[i] = {w, {u, v}};
	}
	for (int i = 0; i < S; ++i) {
        int x; cin >> x;
        isShop[x] = true;
	}
	for (int i = 1; i <= N; ++i) {
        down[i] = INF;
        for (int j = 0; j <= 16; ++j) {
            dp[j][i] = INF;
        }
	}
	dfs(E, -1);
    for (int j = 1; j <= 16; ++j) {
        for (int i = 1; i <= N; ++i) {
            dp[j][i] = min(dp[j - 1][i], dp[j - 1][anc[j - 1][i]]);
        }
    }

	while (Q--) {
        int i, R; cin >> i >> R;
        int u = edges[i].se.fi, v = edges[i].se.se;
        if (h[u] > h[v]) swap(u, v); // v is child of u
        if (tin[v] <= tin[R] && tout[R] <= tout[v]) {
            long long res = jump(R, v) + dep[R];
            if (res >= 1e16) cout << "oo\n";
            else cout << res << '\n';
        } else cout << "escaped\n";
	}

//	cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
	return 0;
}

Compilation message (stderr)

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