# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
781719 | YassineBenYounes | Two Currencies (JOI23_currencies) | C++17 | 281 ms | 29844 KiB |
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>
typedef long long ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<pair<ll, ll>, null_type, less<pair<ll,ll>>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
////////////////////Only Clear Code//////////////////////////
void usaco_problem(){
freopen("milkvisits.in", "r", stdin);
freopen("milkvisits.out", "w", stdout);
}
void init(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
const int mx = 1e5+5;
const int LOG = 25;
const ll inf = 1e18;
const ll mod = 1e8;
vector<int> graph[mx];
ll obstacles[mx];
vector<vector<int>> queries;
vector<pair<int, int>> edges;
int anc[mx][LOG], dis[mx];
void dfs(int node, int p){
anc[node][0] = p;
for(int j = 1; j < 20;j++)anc[node][j] = anc[anc[node][j-1]][j-1];
for(int adj : graph[node]){
if(adj == p)continue;
dis[adj] = dis[node]+1;
dfs(adj, node);
}
}
void dfs1(int node, int p){
for(int adj : graph[node]){
if(adj == p)continue;
obstacles[adj] += obstacles[node];
dfs1(adj, node);
}
}
int lca(int a, int b){
if(dis[b] > dis[a])swap(a, b);
int k = dis[a] - dis[b];
for(int j = 20; j >= 0;j--){
if(k & (1 << j)){
a = anc[a][j];
}
}
if(a == b)return a;
for(int j = 20; j >= 0;j--){
if(dis[a] >= (1 << j) && anc[a][j] != anc[b][j]){
a = anc[a][j];
b = anc[b][j];
}
}
return anc[a][0];
}
void run_case(){
int n, m, q;cin >> n >> m >> q;
for(int i = 0; i < n-1;i++){
int a, b;cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
if(a > b)swap(a, b);
edges.push_back({a, b});
}
dfs(1, 1);
ll one = 0;
for(int i = 0; i < m;i++){
int a, b;cin >> a >> b;a--;
one = b;
int x = edges[a].first, y = edges[a].second;
if(x == anc[y][0])swap(x, y);
obstacles[x]++;
}
dfs1(1, 1);
for(int i = 0; i < q;i++){
ll a, b, gold, silver;cin >> a >> b >> gold >> silver;
ll k = lca(a, b);
ll tot = obstacles[a] + obstacles[b] - 2*obstacles[k];
//cout << a << " " << b << " " << << endl;
ll left = tot - (silver / one);
gold -= max((ll)0, left);
cout << max(gold, (ll)-1) << endl;
}
}
int main(){
speed;
int t = 1;
//cin >> t;
while(t--){
run_case();
}
}
/*
NEVER GIVE UP!
DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
Your Guide when stuck:
- Continue keyword only after reading the whole input
- Don't use memset with testcases
- Check for corner cases(n=1, n=0)
- Check where you declare n(Be careful of declaring it globally and in main)
*/
Compilation message (stderr)
# | 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... |