Submission #1222108

#TimeUsernameProblemLanguageResultExecution timeMemory
1222108AvianshTwo Currencies (JOI23_currencies)C++20
0 / 100
65 ms840 KiB
#include <bits/stdc++.h>

using namespace std;

vector<int>checks;

void dfs(int st, vector<array<int,2>>g[], vector<int>costs[], vector<int>&curr, int tar, int p){
    if(st==tar){
        for(int i : curr){
            checks.push_back(i);
        }
        return;
    }
    for(array<int,2>e:g[st]){
        if(e[0]==p)
            continue;
        for(int i : costs[e[1]]){
            curr.push_back(i);
        }
        dfs(e[0],g,costs,curr,tar,st);
        for(int i : costs[e[1]]){
            assert(curr.size());
            curr.pop_back();
        }
    }
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,m,q;
    cin >> n >> m >> q;
    vector<array<int,2>>g[n];
    for(int i = 0;i<n-1;i++){
        int a,b;
        cin >> a >> b;
        a--;b--;
        g[a].push_back({b,i});
        g[b].push_back({a,i});
    }
    vector<int>costs[n-1];
    for(int i = 0;i<m;i++){
        int p,c;
        cin >> p >> c;
        p--;
        costs[p].push_back(c);
    }
    for(int i = 0;i<n-1;i++){
        if(costs[i].size())
            sort(costs[i].begin(),costs[i].end());
    }
    while(q--){
        int s,t,x,y;
        cin >> s >> t >> x >> y;
        s--;t--;
        checks.clear();
        vector<int>temp;
        dfs(s,g,costs,temp,t,-1);
        if(checks.size()!=0){
            sort(checks.begin(),checks.end());
            x-=checks.size();
            for(int i : checks){
                if(y-i>=0){
                    y-=i;
                    x++;
                }
                else{
                    break;
                }
            }
        }
        if(x<0){
            cout << -1 << "\n";
        }
        else{
            cout << x << "\n";
        }
    }
    return 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...