답안 #919951

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
919951 2024-02-01T23:26:43 Z sverma22 Valley (BOI19_valley) C++17
컴파일 오류
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std; 

using ll = long long; 

int n, s, q, e; 

struct Edge {
    int b;
    ll w; 
    int i;  
}; 

vector<vector<Edge> > adj; 

vector<int> paren; 
vector<int> start; 
vector<int> en; 
vector<bool> is_shop; 
vector<pair<int, int> > edges; 
vector<ll> dist; // dist from root; 
vector<ll> magic; 

// SET PARENT NODES AND START AND END TIMEs 
void dfs(int node, int &cnt, int par = 0) {
    start[node] = cnt++; // increment count yep 
    int add = 0; 
    for(auto neigh : adj[node]) {
        if(neigh.b == par) continue; 
        paren[neigh.b] = node; 
        dist[neigh.b] = dist[node] + neigh.w; 
        dfs(neigh.b, cnt, node); 
    }
    en[node] = cnt++; 
}

// this is quite tuff 
void compute_magic(int node, int par = 0) {
    for(auto neigh : adj[node]) {
        if(neigh.b == par) continue; 
        compute_magic(neigh.b, node); 
    }

    if(is_shop[node]) magic[node] = dist[node]; 
    else magic[node] = LLONG_MAX; 

    for(auto neigh : adj[node]) {
        if(neigh.b == par) continue; 
        magic[node] = min(magic[node], magic[neigh.b]); 
    }
}

const int MAXN = 1e5 + 5; 
const int MAXD = 18; 

int jumpVert[MAXN][MAXD]; 
int jumpMagic[MAXN][MAXD]; 

void buildLifting() {
    for(int i = 1; i <= n; i++) {
        jumpVert[i][0] = paren[i]; 
        jumpMagic[i][0] = magic[i]; 
    }

    for(int d = 1; d < MAXD; d++) {
        for(int i = 1; i <= n; i++) {
            jumpVert[i][d] = jumpVert[jumpVert[i][d - 1]][d - 1]; 
            jumpMagic[i][d] = min(jumpMagic[i][d - 1], jumpMagic[jumpMagic[i][d-1]][d-1]); 
        }
    }
}


void solve() {
    cin >> n >> s >> q >> e; 
    adj.resize(n + 1); 
    start.resize(n + 1); 
    en.resize(n + 1); 
    paren.resize(n + 1); 
    is_shop.resize(n + 1); 
    dist.resize(n + 1); 
    magic.resize(n + 1); 

    for(int i = 0; i < n - 1; i++) {
        int a, b, w; cin >> a >> b >> w; 
        adj[a].push_back({b, w, i + 1}); 
        adj[b].push_back({a, w, i + 1}); 
        edges.push_back({a, b}); 
    }   

    int cnt = 0; 
    dfs(e, cnt); 

    // for(int i = 1; i <= n; i++) {
    //     cout << start[i] << ' ' << en[i] << '\n';
    // }

    for(int i = 0; i < s; i++) {
        int c; cin >> c; 
        is_shop[c] = true; 
    }

    compute_magic(e); 
    for(int i = 1; i <= n; i++) {
        if(magic[i] != LLONG_MAX) magic[i] = magic[i] - (2LL) * dist[i]; 
    }
    buildLifting(); 

    while(q--) {
        int i, r; cin >> i >> r; 
        int par = edges[i - 1].first, child = edges[i - 1].second; 
        if(paren[par] == child) swap(par, child); // paren[child] = par woohoo

        // cout << par << ',' << child << '\n';

        if(!(start[child] <= start[r] && en[r] <= en[child])) {
            cout << "escaped\n"; 
            continue; 
        }

        // now the hard part ... we need dist have that ... 
        // now we need this magic crap ...

        // answer to query -> dist[r] + min_w magic[w] for all w -> r -> child 
        int curr_vert = r; 
        ll ans = magic[child]; 
        for(int d = 0; d < MAXD; d++) {
            int vert = jumpVert[curr_vert][d]; 
            if(start[child] <= start[vert] && en[vert] <= en[child]) { // vertex is a child of the goon
                ans = min(ans, jumpMagic[curr_vert][d]);
                curr_vert = vert; 
            }
        }


        if(ans != LLONG_MAX) {
            ans = ans + dist[r]; 
            cout << ans << '\n';
        } else cout << "oo\n";
    }

}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // #ifndef ONLINE_JUDGE
    // freopen("file.txt", "r", stdin);
    // #endif
    int t = 1; // cin >> t; 
    while(t--) {
        solve(); 
    }
}

Compilation message

valley.cpp: In function 'void dfs(int, int&, int)':
valley.cpp:27:9: warning: unused variable 'add' [-Wunused-variable]
   27 |     int add = 0;
      |         ^~~
valley.cpp: In function 'void solve()':
valley.cpp:130:55: error: no matching function for call to 'min(ll&, int&)'
  130 |                 ans = min(ans, jumpMagic[curr_vert][d]);
      |                                                       ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from valley.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:230:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  230 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:230:5: note:   template argument deduction/substitution failed:
valley.cpp:130:55: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
  130 |                 ans = min(ans, jumpMagic[curr_vert][d]);
      |                                                       ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from valley.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:278:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  278 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:278:5: note:   template argument deduction/substitution failed:
valley.cpp:130:55: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
  130 |                 ans = min(ans, jumpMagic[curr_vert][d]);
      |                                                       ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from valley.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3468:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(std::initializer_list<_Tp>)'
 3468 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3468:5: note:   template argument deduction/substitution failed:
valley.cpp:130:55: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  130 |                 ans = min(ans, jumpMagic[curr_vert][d]);
      |                                                       ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from valley.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3474:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(std::initializer_list<_Tp>, _Compare)'
 3474 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3474:5: note:   template argument deduction/substitution failed:
valley.cpp:130:55: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  130 |                 ans = min(ans, jumpMagic[curr_vert][d]);
      |                                                       ^