#include<bits/stdc++.h>
#define TIME (1.0* clock()/CLOCKS_PER_SEC)
#define pb push_back
#define eb emplace_back
#define id1 (id<<1)
#define id2 (id<<1)|1
#define ll long long
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<long long>
#define vll vector <pair<ll,ll>>
#define li pair<long long,int>
#define vil vector <pair<int,ll>>
#define db double
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i <=(b); i++)
#define F0R(i, a) FOR(i, 0, a-1)
#define ROF(i, a, b) for (int i = (b); i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a-1)
#define rep(a) F0R(_, a)
#define each(a, x) for (auto &a : x)
#define ALL(x) (x).begin(),(x).end()
#define pq priority_queue <li, vector <li>, greater <li>>
using namespace std;
const int maxn=1e6;
//const int MOD=1e9+7;
//const int MOD=998244353;
//const int dx[4]{1, 0, -1, 0}, dy[4]{0, 1, 0, -1};
#define int long long
int n,m,q;
int a[maxn],b[maxn],w[maxn],c[maxn];
vector<pair<int,int>> ke[maxn];
int h[maxn];
bool sub1(){
return(n<=2000 and m<=2000 and q<=2000);
}
int pa[maxn],e[maxn];
void dfssub1(int u,int p){
for(auto[v,i]:ke[u]){
if(v==p) continue;
h[v]=h[u]+1;
pa[v]=u;
e[v]=i;
dfssub1(v,u);
}
}
int querysub1(int s,int t,int x,int y){
if(s==t) return x;
vector<int> vec;
if(h[s]>h[t]) swap(s,t);
int delta=h[t]-h[s];
for(int i=1;i<=delta;i++){
if(w[e[t]]!=0){
vec.push_back(w[e[t]]);
}
t=pa[t];
}
while(s!=t){
if(w[e[t]]!=0){
vec.push_back(w[e[t]]);
}
t=pa[t];
if(w[e[s]]!=0){
vec.push_back(w[e[s]]);
}
s=pa[s];
}
sort(ALL(vec));
int i=0;
while(i<vec.size() and vec[i]<=y){
y-=vec[i];
i++;
}
x-=vec.size()-i+1;
if(x>=0) return x;
return -1;
}
void solvesub1(){
dfssub1(1,0);
while(q--){
int s,t,x,y;
cin>>s>>t>>x>>y;
cout<<querysub1(s,t,x,y)<<"\n";
}
}
bool sub2(){
bool check=true;
for(int i=1;i<m;i++){
if(c[i]!=c[i+1]) check=false;
}
return check;
}
void solvesub2(){
}
void solve(){
cin>>n>>m>>q;
for(int i=1;i<n;i++){
cin>>a[i]>>b[i];
ke[a[i]].eb(b[i],i);
ke[b[i]].eb(a[i],i);
}
for(int i=1;i<=m;i++){
int p,c[i];
cin>>p>>c[i];
w[p]=c[i];
}
solvesub1();
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
if (fopen("TASK.INP", "r")){
freopen("TASK.INP", "r", stdin);
freopen("TASK.OUT", "w", stdout);
}
int ntest;
ntest=1;
//cin>>ntest;
for(int i=1;i<=ntest;i++) solve();
cerr<<"\n"<<"Time elapsed "<<TIME<<"s.\n";
return 0;
}
Compilation message (stderr)
currencies.cpp: In function 'int main()':
currencies.cpp:137:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen("TASK.INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
currencies.cpp:138:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen("TASK.OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |