Submission #847421

# Submission time Handle Problem Language Result Execution time Memory
847421 2023-09-09T15:51:31 Z hariaakas646 Valley (BOI19_valley) C++17
100 / 100
147 ms 42980 KB
#include <bits/stdc++.h>

using namespace std;

#define scd(t) scanf("%d", &t)
#define scld(t) scanf("%ld", &t)
#define sclld(t) scanf("%lld", &t)
#define scc(t) scanf("%c", &t)
#define scs(t) scanf("%s", t)
#define scf(t) scanf("%f", &t)
#define sclf(t) scanf("%lf", &t)
#define forr(i, j, k) for (int i = j; i < k; i++)
#define frange(i, j) forr(i, 0, j)
#define all(cont) cont.begin(), cont.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long int li;
typedef unsigned long int uli;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<lli> vll;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int, int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long double ld;

void usaco()
{
    freopen("input.in", "r", stdin);
//    freopen("problem.out", "w", stdout);
}

vector<vii> graph;
vector<pair<pii, int>> edges;
vvi lift;
vector<vll> liftmi;
vi depth;
vll dp;
vll dist;
vb shop;

void dfs(int x, int par, int dt, lli d) {
    if(shop[x]) dp[x] = 0;
    depth[x] = dt;
    dist[x] = d;
    lift[0][x] = par;

    for(auto p : graph[x]) {
        if(p.f != par) {
            dfs(p.f, x, dt+1, d+p.s);
            dp[x] = min(dp[x], dp[p.f] + p.s);
        }
    }
}

int binlift(int x, int c) {
    frange(i, 20) {
        if(c & (1<<i)) x = lift[i][x];
    }
    return x;
}

lli binval(int x, int c) {
    lli mi = 1e18;
    frange(i, 20) {
        if(c & (1<<i)) {
            mi = min(mi, liftmi[i][x]);
            x = lift[i][x];
            // printf("%lld %d\n", mi, x);
        }
    }
    // printf("%lld %d\n", dp[x] - dist[x], x);
    return min(dp[x] - dist[x], mi);
}

int main() {
    // usaco();
    int n, st, q, e;
    scd(n);
    scd(st);
    scd(q);
    scd(e);

    graph = vector<vii>(n+1);
    depth = vi(n+1, 1);
    dp = vll(n+1, 1e18);
    dist = vll(n+1, 1e18);

    frange(i, n-1) {
        int a, b, w;
        scd(a);
        scd(b);
        scd(w);
        edges.pb(mp(mp(a, b), w));
        graph[a].pb(mp(b, w));
        graph[b].pb(mp(a, w));
    }

    shop = vb(n+1);
    frange(i, st) {
        int a;
        scd(a);
        shop[a] = true;
    }

    lift = vvi(20, vi(n+1));
    liftmi = vector<vll>(20, vll(n+1, 1e18));

    dfs(e, 0, 0, 0);

    forr(i, 1, n+1) {
        liftmi[0][i] = dp[i] - dist[i];
    }

    forr(i, 1, 20) {
        forr(j, 1, n+1) {
            lift[i][j] = lift[i-1][lift[i-1][j]];
            liftmi[i][j] = min(liftmi[i-1][j], liftmi[i-1][lift[i-1][j]]);
        }
    }

    frange(i, q) {
        int ed, r;
        scd(ed);
        scd(r);
        ed--;
        int a = edges[ed].f.f;
        int b = edges[ed].f.s;
        if(depth[a] > depth[b]) swap(a, b);
        if(depth[r] >= depth[b] && binlift(r, depth[r]- depth[b]) == b) {
            // printf("%lld ", binval(r, depth[r] - depth[b]));
            lli mi = dist[r] + binval(r, depth[r] - depth[b]);
            if(mi <= 1e16) {
                printf("%lld\n", mi);
            }
            else {
                printf("oo\n");
            }
        }
        else {
            printf("escaped\n");
        }
    }
    // forr(i, 1, n+1) printf("%lld ", dp[i]);
    // printf("\n");
    // forr(i, 1, n+1) printf("%lld ", dist[i]);
}

Compilation message

valley.cpp: In function 'void usaco()':
valley.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     freopen("input.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp: In function 'int main()':
valley.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
valley.cpp:87:5: note: in expansion of macro 'scd'
   87 |     scd(n);
      |     ^~~
valley.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
valley.cpp:88:5: note: in expansion of macro 'scd'
   88 |     scd(st);
      |     ^~~
valley.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
valley.cpp:89:5: note: in expansion of macro 'scd'
   89 |     scd(q);
      |     ^~~
valley.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
valley.cpp:90:5: note: in expansion of macro 'scd'
   90 |     scd(e);
      |     ^~~
valley.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
valley.cpp:99:9: note: in expansion of macro 'scd'
   99 |         scd(a);
      |         ^~~
valley.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
valley.cpp:100:9: note: in expansion of macro 'scd'
  100 |         scd(b);
      |         ^~~
valley.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
valley.cpp:101:9: note: in expansion of macro 'scd'
  101 |         scd(w);
      |         ^~~
valley.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
valley.cpp:110:9: note: in expansion of macro 'scd'
  110 |         scd(a);
      |         ^~~
valley.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
valley.cpp:132:9: note: in expansion of macro 'scd'
  132 |         scd(ed);
      |         ^~~
valley.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
valley.cpp:133:9: note: in expansion of macro 'scd'
  133 |         scd(r);
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 3 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 3 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 804 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 560 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 38132 KB Output is correct
2 Correct 106 ms 37788 KB Output is correct
3 Correct 117 ms 37872 KB Output is correct
4 Correct 120 ms 39876 KB Output is correct
5 Correct 129 ms 39868 KB Output is correct
6 Correct 147 ms 42372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 3 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 804 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 560 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 98 ms 38132 KB Output is correct
16 Correct 106 ms 37788 KB Output is correct
17 Correct 117 ms 37872 KB Output is correct
18 Correct 120 ms 39876 KB Output is correct
19 Correct 129 ms 39868 KB Output is correct
20 Correct 147 ms 42372 KB Output is correct
21 Correct 93 ms 37768 KB Output is correct
22 Correct 90 ms 37364 KB Output is correct
23 Correct 105 ms 37172 KB Output is correct
24 Correct 142 ms 39672 KB Output is correct
25 Correct 126 ms 42980 KB Output is correct
26 Correct 87 ms 37620 KB Output is correct
27 Correct 93 ms 37364 KB Output is correct
28 Correct 103 ms 37608 KB Output is correct
29 Correct 116 ms 38796 KB Output is correct
30 Correct 135 ms 40484 KB Output is correct
31 Correct 90 ms 37688 KB Output is correct
32 Correct 102 ms 37448 KB Output is correct
33 Correct 125 ms 37368 KB Output is correct
34 Correct 119 ms 39524 KB Output is correct
35 Correct 124 ms 42836 KB Output is correct
36 Correct 113 ms 39928 KB Output is correct