답안 #1096289

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1096289 2024-10-04T08:36:03 Z thieunguyenhuy Valley (BOI19_valley) C++17
100 / 100
139 ms 39252 KB
#include <bits/stdc++.h>
using namespace std;

#define popcount(n) (__builtin_popcountll((n)))
#define clz(n) (__builtin_clzll((n)))
#define ctz(n) (__builtin_ctzll((n)))
#define lg(n) (63 - __builtin_clzll((n)))
#define BIT(n, i) (((n) >> (i)) & 1ll)
#define MASK(i) (1ll << (i))
#define FLIP(n, i) ((n) ^ (1ll << (i)))
#define ON(n, i) ((n) | MASK(i))
#define OFF(n, i) ((n) & ~MASK(i))

#define Int __int128
#define fi first
#define se second

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<long long, int>> vli;
typedef vector<pair<int, long long>> vil;

template <class T1, class T2>
bool maximize(T1 &x, T2 y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
template <class T1, class T2>
bool minimize(T1 &x, T2 y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}

template <class T>
void remove_duplicate(vector<T> &ve) {
    sort (ve.begin(), ve.end());
    ve.resize(unique(ve.begin(), ve.end()) - ve.begin());
}

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
template <class T> T random(T l, T r) {
    return uniform_int_distribution<T>(l, r)(rng);
}
template <class T> T random(T r) {
    return rng() % r;
}

const int N = 1e5 + 5, LG = 18;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
const ll MAX_ANS = 1e15;

int n, s, q, root, dfs_time = 0;
int tin[N], tout[N], up[N][LG];
ll dist[N], mi[N][LG], opt[N];
vii adj[N];
pii edges[N];
bitset<N> mark;

void dfs(int u, int fa) {
    tin[u] = ++dfs_time;
    if (mark[u]) opt[u] = 0;
    for (auto [v, w] : adj[u]) if (v != fa) {
        up[v][0] = u, dist[v] = dist[u] + w;
        for (int i = 1; i < LG; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
        dfs(v, u); minimize(opt[u], opt[v] + w);
    }
    tout[u] = dfs_time;
}

bool is_anc(int u, int v) {
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    #ifdef hwe
        freopen("input.inp", "r", stdin);
        freopen("output.out", "w", stdout);
    #else
        #define taskname ""
        if (fopen(taskname".inp", "r")) {
            freopen(taskname".inp", "r", stdin);
            freopen(taskname".out", "w", stdout);
        }
    #endif

    cin >> n >> s >> q >> root;

    for (int i = 1; i < n; ++i) {
        int u, v, w; cin >> u >> v >> w;
        adj[u].emplace_back(v, w), adj[v].emplace_back(u, w);
        edges[i] = make_pair(u, v);
    }

    for (int i = 1; i <= s; ++i) {
        int u; cin >> u;
        mark[u] = true;
    }

    memset(opt, 0x0f, sizeof opt);
    memset(mi, 0x0f, sizeof mi);
    // cerr << "mk\n";
    dfs(root, -1);
    for (int i = 1; i <= n; ++i) mi[i][0] = opt[up[i][0]] - dist[up[i][0]];
    for (int i = 1; i < LG; ++i) {
        for (int j = 1; j <= n; ++j) {
            mi[j][i] = min(mi[j][i - 1], mi[up[j][i - 1]][i - 1]);
        }
    }

    // for (int i = 1; i <= n; ++i) for (int j = 0; j <= lg(n); ++j) {
    //     cout << i << ' ' << j << ' ' << mi[i][j] << '\n';
    // }

    // cerr << up[6][2] << '\n';

    for (int i = 1; i <= q; ++i) {
        int id, r; cin >> id >> r;
        auto [u, v] = edges[id];
        if (up[u][0] == v) swap(u, v);
        // cerr << u << ' ' << v << '\n';
        if (!is_anc(v, r)) cout << "escaped\n";
        else {
            ll ans = opt[r], init = dist[r];
            for (int j = LG - 1; j >= 0; --j) if (is_anc(v, up[r][j])) {
                // cout << r << ' ' << j << ' ' << up[r][j] << ' ' << mi[r][j] << '\n';
                minimize(ans, init + mi[r][j]);
                r = up[r][j];
            }
            // cout << ans << '\n';
            if (ans < MAX_ANS) cout << ans << '\n';
            else cout << "oo\n";
        }
    }
    cerr << '\n'; return 0;
}

Compilation message

valley.cpp: In function 'int main()':
valley.cpp:98:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |             freopen(taskname".inp", "r", stdin);
      |             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:99:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |             freopen(taskname".out", "w", stdout);
      |             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 17756 KB Output is correct
2 Correct 8 ms 17756 KB Output is correct
3 Correct 9 ms 17756 KB Output is correct
4 Correct 10 ms 17756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 17756 KB Output is correct
2 Correct 8 ms 17756 KB Output is correct
3 Correct 9 ms 17756 KB Output is correct
4 Correct 10 ms 17756 KB Output is correct
5 Correct 7 ms 17756 KB Output is correct
6 Correct 9 ms 17640 KB Output is correct
7 Correct 9 ms 17752 KB Output is correct
8 Correct 8 ms 17756 KB Output is correct
9 Correct 11 ms 17752 KB Output is correct
10 Correct 9 ms 17756 KB Output is correct
11 Correct 7 ms 17680 KB Output is correct
12 Correct 12 ms 17756 KB Output is correct
13 Correct 7 ms 17756 KB Output is correct
14 Correct 8 ms 17756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 31600 KB Output is correct
2 Correct 107 ms 35156 KB Output is correct
3 Correct 101 ms 34896 KB Output is correct
4 Correct 112 ms 36692 KB Output is correct
5 Correct 111 ms 36756 KB Output is correct
6 Correct 139 ms 38736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 17756 KB Output is correct
2 Correct 8 ms 17756 KB Output is correct
3 Correct 9 ms 17756 KB Output is correct
4 Correct 10 ms 17756 KB Output is correct
5 Correct 7 ms 17756 KB Output is correct
6 Correct 9 ms 17640 KB Output is correct
7 Correct 9 ms 17752 KB Output is correct
8 Correct 8 ms 17756 KB Output is correct
9 Correct 11 ms 17752 KB Output is correct
10 Correct 9 ms 17756 KB Output is correct
11 Correct 7 ms 17680 KB Output is correct
12 Correct 12 ms 17756 KB Output is correct
13 Correct 7 ms 17756 KB Output is correct
14 Correct 8 ms 17756 KB Output is correct
15 Correct 85 ms 31600 KB Output is correct
16 Correct 107 ms 35156 KB Output is correct
17 Correct 101 ms 34896 KB Output is correct
18 Correct 112 ms 36692 KB Output is correct
19 Correct 111 ms 36756 KB Output is correct
20 Correct 139 ms 38736 KB Output is correct
21 Correct 93 ms 34944 KB Output is correct
22 Correct 109 ms 34556 KB Output is correct
23 Correct 115 ms 34556 KB Output is correct
24 Correct 98 ms 36432 KB Output is correct
25 Correct 118 ms 39176 KB Output is correct
26 Correct 82 ms 34900 KB Output is correct
27 Correct 79 ms 34384 KB Output is correct
28 Correct 80 ms 34388 KB Output is correct
29 Correct 105 ms 35924 KB Output is correct
30 Correct 102 ms 37204 KB Output is correct
31 Correct 72 ms 35036 KB Output is correct
32 Correct 79 ms 34504 KB Output is correct
33 Correct 83 ms 34500 KB Output is correct
34 Correct 107 ms 36436 KB Output is correct
35 Correct 103 ms 39252 KB Output is correct
36 Correct 91 ms 36668 KB Output is correct