Submission #568188

#TimeUsernameProblemLanguageResultExecution timeMemory
568188mat_vValley (BOI19_valley)C++14
100 / 100
226 ms44360 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,ll>


using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());


int n,m,q,e;
struct gr{
    int a;
    int b;
    ll w;
};

gr edges[100005];
vector<pii> graf[100005];

int lca[100005][20];
ll najm[100005][20];
int disc[100005];
int out[100005];
ll dub[100005];
ll dp[100005];
bool dobar[100005];
int br;

ll pom = 1e8;


void dfs(int x, int y, ll z){
    lca[x][0] = y;
    disc[x] = br;
    dub[x] = z;
    dp[x] = pom;
    if(dobar[x])dp[x] = 0;
    for(auto c:graf[x]){
        if(c.xx == y)continue;
        br++;
        dfs(c.xx,x,z+c.yy);
        dp[x] = min(dp[x], dp[c.xx] + c.yy);
    }
    out[x] = br;
    najm[x][0] = (dp[x] - dub[x]);
}

void resilca(int x, int y){
    ff(j,1,19){
        lca[x][j] = lca[lca[x][j-1]][j-1];
        najm[x][j] = min(najm[x][j - 1], najm[lca[x][j-1]][j-1]);
    }
    for(auto c:graf[x]){
        if(c.xx == y)continue;
        resilca(c.xx, x);
    }
}


bool insubtr(int x, int y){
    if(x == 0)return 1;
    if(x == y)return 1;
    if(disc[y] >= disc[x] && disc[y] <= out[x])return 1;
    return 0;
}

int main()
{

    ios_base::sync_with_stdio(false); cin.tie(0);
    pom *= pom;
    cin >> n >> m >> q >> e;
    ff(i,1,n)dp[i] = pom;
    ff(i,1,n - 1){
        int x,y,z;
        cin >> x >> y >> z;
        edges[i] = {x,y,z};
        graf[x].pb({y,z});
        graf[y].pb({x,z});
    }
    ff(i,1,m){
        int x;
        cin >> x;
        dobar[x] = 1;
    }
    ff(i,1,n)dp[i] += dub[i];
    br = 1;
    dfs(e,0,1);
    resilca(e,0);
    ll mks = 1e9;
    ll mks2 = 1e5;
    mks *= mks2;
    while(q--){
        int r,y;
        cin >> r >> y;
        int koji = edges[r].a;
        if(dub[edges[r].a] < dub[edges[r].b])koji = edges[r].b;

        if(!insubtr(koji,y)){
            cout << "escaped\n";
            continue;
        }
        ll ans = pom;
        ll dlt = dub[y];
        fb(j,19,0){
            if(insubtr(koji,lca[y][j])){
                ans = min(ans, najm[y][j]);
                y = lca[y][j];
            }
        }
        ans = min(ans, najm[y][0]);
        if(ans > mks)cout << "oo\n";
        else cout << ans + dlt << "\n";

    }

    return 0;
}

Compilation message (stderr)

valley.cpp: In function 'void resilca(int, int)':
valley.cpp:6:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
valley.cpp:62:5: note: in expansion of macro 'ff'
   62 |     ff(j,1,19){
      |     ^~
valley.cpp: In function 'int main()':
valley.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
valley.cpp:86:5: note: in expansion of macro 'ff'
   86 |     ff(i,1,n)dp[i] = pom;
      |     ^~
valley.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
valley.cpp:87:5: note: in expansion of macro 'ff'
   87 |     ff(i,1,n - 1){
      |     ^~
valley.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
valley.cpp:94:5: note: in expansion of macro 'ff'
   94 |     ff(i,1,m){
      |     ^~
valley.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
valley.cpp:99:5: note: in expansion of macro 'ff'
   99 |     ff(i,1,n)dp[i] += dub[i];
      |     ^~
valley.cpp:7:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
valley.cpp:118:9: note: in expansion of macro 'fb'
  118 |         fb(j,19,0){
      |         ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...