This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=2e3+9,M=2e18+9,mod=1e9+7;
ll n,m,q,a[N],b[N],pa[N],dep[N],s,t,x,y,z;
vector<ll>v[N],ed[N][N],p;
void dfs(ll node,ll par)
{
pa[node]=par;
dep[node]=dep[par]+1;
for(auto i:v[node])if(i!=par)dfs(i,node);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m>>q;
for(ll i=1;i<n;++i)
{
cin>>a[i]>>b[i];
v[a[i]].push_back(b[i]);
v[b[i]].push_back(a[i]);
}
for(ll i=0;i<m;++i)
{
cin>>x>>y;
ed[a[x]][b[x]].push_back(y);
ed[b[x]][a[x]].push_back(y);
}
dep[0]=-1,pa[1]=0;
dfs(1,0);
while(q--)
{
p.clear();
cin>>s>>t>>x>>y;
if(dep[s]<dep[t])swap(s,t);
while(dep[s]>dep[t])
{
for(auto i:ed[s][pa[s]])p.push_back(i);
s=pa[s];
}
while(s!=t)
{
for(auto i:ed[s][pa[s]])p.push_back(i);
s=pa[s];
for(auto i:ed[t][pa[t]])p.push_back(i);
t=pa[t];
}
sort(p.begin(),p.end());
n=p.size();
for(ll i=0;i<n;++i)
if(y>=p[i])y-=p[i];
else
{
x-=n-i;
break;
}
cout<<(x>=0?x:-1)<<'\n';
}
return 0;
}
# | 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... |