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 <iostream>
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define pll pair<lli,lli>
#define MAX 100000
//para los hijos
#define des first
#define w second
lli n,m,q,silver,x,y,k,cont,a,b;
pair<pll, lli> aristas[MAX+2];
vector<pll> hijos[MAX+2];
lli dis[MAX+2],anc[22][MAX+2];
pll rango[MAX+2];
void dfs(lli pos,lli padre, lli s) {
dis[pos] = dis[padre]+s;
anc[0][pos] = padre;
lli x = padre;
rep(i,1,20) {
anc[i][pos] = anc[i-1][x];
x = anc[i][pos];
}
rango[pos].first = cont++;
for(auto h : hijos[pos]) {
if (h.des == padre) continue;
dfs(h.des,pos,h.w);
}
rango[pos].second = cont++;
}
lli lca(lli a, lli b) {
if (rango[a].first > rango[b].first) swap(a,b);
if (rango[a].second > rango[b].second) return a;
lli x;
repa(i,20,0) {
x = anc[i][a];
if (x == 0) continue;
if (rango[x].first < rango[a].first && rango[x].second > rango[b].second) continue;
a = anc[i][a];
}
a = anc[0][a];
return a;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> q;
rep(i,1,n-1) {
cin >> a >> b;
aristas[i] = {{a,b},0};
}
rep(i,1,m) {
cin >> a >> b;
silver = b;
aristas[a].second++;
}
rep(i,1,n-1) {
hijos[aristas[i].first.first].push_back({aristas[i].first.second,aristas[i].second});
hijos[aristas[i].first.second].push_back({aristas[i].first.first,aristas[i].second});
}
//solucion
cont = 1;
dfs(1,0,0);
rep(i,1,q) {
cin >> a >> b >> x >> y;
k = lca(a,b);
k = dis[a]+ dis[b] - 2*dis[k];
y /= silver;
k -= y;
if (k <= 0) cout << x << "\n";
else if (x >= k) cout << (x-k) << "\n";
else cout << -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... |