Submission #987651

# Submission time Handle Problem Language Result Execution time Memory
987651 2024-05-23T09:19:18 Z NoLove Valley (BOI19_valley) C++14
23 / 100
176 ms 70204 KB
/**
 *    author : Lăng Trọng Đạt
 *    created: 23-05-2024 
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int long long
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
using pii = pair<int, int>;
using vi = vector<int>;
#define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
void mx(int& a, int b) { if (b > a) a = b; }
void mi(int& a, int b) { if (b < a) a = b; }
#define si(x) (int)(x.size())
const int INF = 1e18;
const int MOD = 1e9 + 7;

const int MAXN = 1e5 + 5;
int g[MAXN];
int n, s, q, root, r;
vector<pii> adj[MAXN];

pii up[20][MAXN];

bool is_shop[MAXN];
int far[MAXN], lca[2*MAXN];
int get(int x) { return (far[x] == 0 ? x : far[x] = get(far[x])); }
vector<pair<int, int>> query_lcas[MAXN];
int l[MAXN]; // shortest length from node u to shop which is in subtree of u
int d[MAXN]; // length of path from root to node u
int depth[MAXN];
void dfs(int v, int prv, int steps, int dist) {
    d[v] = dist;
    depth[v] = steps;
    if (is_shop[v]) l[v] = 0;
    for (auto[u, w] : adj[v]) {
        if (u == prv) continue;
        dfs(u, v, steps + 1, dist + w);
        mi(l[v], l[u] + w);
        far[u] = v;
        up[0][u].f = v;
        if (is_shop[u]) up[0][u].se = u;
        else if (is_shop[v]) up[0][u].se = v;
    }

    for (auto[u, id] : query_lcas[v]) {
        if (depth[u]) {
            lca[id] = get(u);
        }
    }
}

int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    if (fopen("hi.inp", "r")) {
        freopen("hi.inp", "r", stdin);
//        freopen("hi.out", "w", stdout);
    } 


    // input
    cin >> n >> s >> q >> root;
    int a, b, w;
    vector<pii> edges;
    FOR(i, 1, n - 1) {
        cin >> a >> b >> w;
        adj[a].pb({b, w});
        adj[b].pb({a, w});
        edges.pb({a, b});
    }
    FOR(i, 1, s) {
        cin >> a;
        is_shop[a] = 1;
    }
    vector<pii> Q(q + 1);
    FOR(i, 1, q) {
        cin >> Q[i].f >> Q[i].se;
        int r = Q[i].se;
        auto[a, b] = edges[Q[i].f - 1];
        query_lcas[r].pb({a, i});
        query_lcas[a].pb({r, i});
        query_lcas[r].pb({b, q + i});
        query_lcas[b].pb({r, q + i});
    }

    // preprocess
    memset(l, 0x3f, sizeof(l));
    dfs(root, -1, 1, 0);

    FOR(i, 1, 19) {
        FOR(v, 1, n) {
            up[i][v].f = up[i - 1][up[i - 1][v].f].f;
            if (up[i - 1][v].se) up[i][v].se = up[i - 1][v].se;
            else if (up[i - 1][up[i - 1][v].f].se) 
                up[i][v].se = up[i - 1][up[i - 1][v].f].se;
        }
    }

    // answer query
    FOR(i, 1, q) {
        auto[a, b] = edges[Q[i].f - 1];
        if (depth[a] < depth[b] && lca[i + q] != b) cout << "escaped\n";
        else if (depth[a] > depth[b] && lca[i] != a) cout << "escaped\n";
        else {
            int tren = a, r = Q[i].se;
            if (depth[a] < depth[b]) tren = b;
            if (l[tren] > INF) cout << "oo\n";
            else {
                db(tren, d[tren], d[r])
                int ans = l[tren] + d[r] - d[tren];
                int jump = depth[r] - depth[tren];
                int shop_in_path = -1;
                int tmp = r;
                FOR(i, 0, 19) {
                    if ((1 << i) && jump) {
                        if (up[i][tmp].se != 0) {
                            shop_in_path = up[i][tmp].se;
                        }
                        tmp = up[i][tmp].f;
                    }
                    if (shop_in_path != -1) {
                        mi(ans, d[r] - d[shop_in_path]);
                        break;
                    }
                }

                cout << ans << "\n";   
            }
        }
    }
}

Compilation message

valley.cpp: In function 'void dfs(long long int, long long int, long long int, long long int)':
valley.cpp:43:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   43 |     for (auto[u, w] : adj[v]) {
      |              ^
valley.cpp:53:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   53 |     for (auto[u, id] : query_lcas[v]) {
      |              ^
valley.cpp: In function 'int32_t main()':
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:72:5: note: in expansion of macro 'FOR'
   72 |     FOR(i, 1, n - 1) {
      |     ^~~
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:78:5: note: in expansion of macro 'FOR'
   78 |     FOR(i, 1, s) {
      |     ^~~
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:83:5: note: in expansion of macro 'FOR'
   83 |     FOR(i, 1, q) {
      |     ^~~
valley.cpp:86:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   86 |         auto[a, b] = edges[Q[i].f - 1];
      |             ^
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:97:5: note: in expansion of macro 'FOR'
   97 |     FOR(i, 1, 19) {
      |     ^~~
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:98:9: note: in expansion of macro 'FOR'
   98 |         FOR(v, 1, n) {
      |         ^~~
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:107:5: note: in expansion of macro 'FOR'
  107 |     FOR(i, 1, q) {
      |     ^~~
valley.cpp:108:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  108 |         auto[a, b] = edges[Q[i].f - 1];
      |             ^
valley.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
valley.cpp:121:17: note: in expansion of macro 'FOR'
  121 |                 FOR(i, 0, 19) {
      |                 ^~~
valley.cpp:122:28: warning: '<<' in boolean context, did you mean '<'? [-Wint-in-bool-context]
  122 |                     if ((1 << i) && jump) {
      |                         ~~~^~~~~
valley.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |         freopen("hi.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 12632 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 12632 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 176 ms 64000 KB Output is correct
2 Correct 170 ms 64184 KB Output is correct
3 Correct 160 ms 64260 KB Output is correct
4 Correct 175 ms 66820 KB Output is correct
5 Correct 150 ms 66668 KB Output is correct
6 Correct 159 ms 70204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 12632 KB Output isn't correct
2 Halted 0 ms 0 KB -