#include <bits/stdc++.h>
using namespace std;/*
<<<<It's never too late for a new beginning in your life>>>>
Today is hard
tomorrow will worse
but the day after tomorrow will be the sunshine..
HARD WORK BEATS TALENT WHEN TALENT DOESN'T WORK HARD............
Never give up */
//The most CHALISHKANCHIK
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define int long long
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vii;
const int N = 1e5+5;
vi g[N];
map<pii, vi> mp;
int pre[N];
int n, q, timer;
int tin[N], tout[N];
int up[20][N];
int num[N]{};
void dfs(int v, int pr) {
tin[v] = ++timer;
up[0][v] = pr;
for (int i = 1; i <= 17; i++) {
up[i][v] = up[i - 1][ up[i - 1][v] ];
}
for (int to : g[v]) {
if (to == pr) continue;
dfs(to, v);
}
tout[v] = timer;
}
bool upper(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int lca(int a, int b) {
if (upper(a, b)) return a;
if (upper(b, a)) return b;
for (int i = 17; i >= 0; i--) {
if (!upper(up[i][a], b)) {
a = up[i][a];
}
}
return up[0][a];
}
void sukfe(int v, int pr){
for (int to : g[v]) {
if (to == pr) continue;
num[to] = num[v];
num[to] += mp[{v, to}].size();
dfs(to, v);
}
}
void dfs2(int v, int pr){
for (int to : g[v]) {
if (to == pr) continue;
pre[to] = v;
dfs2(to, v);
}
}
void solve(){
int n, m, q;
cin >> n >> m >> q;
vii cui;
for(int i = 0; i < n-1; i++){
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
cui.pb({a, b});
}
int j, c;
for(int i = 0; i < m; i++){
cin >> j >> c;j--;
mp[cui[j]].pb(c);
mp[{cui[j].ss, cui[j].ff}].pb(c);
}
if(n <= 2000){
vi pas;
while(q--){
int s, t, x, y;
cin >> s >> t >> x >> y;
dfs2(s, -1);
int r = t;
while(r!=s){
for(auto i:mp[{r, pre[r]}])pas.pb(i);
r = pre[r];
}
sort(all(pas));
for(auto i:pas){
if(i <= y)y -= i;
else x--;
}pas.clear();
cout << (x < 0 ? -1 : x) << '\n';
}
}else{
dfs(1, -1);
sukfe(1, -1);
while(q--){
int a, b, x, y;
cin >> a >> b >> x >> y;
int cnt = num[a] + num[b];
int lc = lca(a, b);
cnt -= num[lc] * 2;
int ans = y/c;
cnt -= ans;
if(x - cnt < 0)cout << -1 << '\n';
else cout << x - cnt << '\n';
}
}
}
main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t = 1;
//~ cin >> t;
while(t--){
solve();
}
}
Compilation message
currencies.cpp:124:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
124 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
27 ms |
5212 KB |
Output is correct |
6 |
Correct |
44 ms |
5460 KB |
Output is correct |
7 |
Correct |
24 ms |
5208 KB |
Output is correct |
8 |
Correct |
47 ms |
5448 KB |
Output is correct |
9 |
Correct |
49 ms |
5460 KB |
Output is correct |
10 |
Correct |
47 ms |
5464 KB |
Output is correct |
11 |
Correct |
50 ms |
5468 KB |
Output is correct |
12 |
Correct |
55 ms |
5468 KB |
Output is correct |
13 |
Correct |
240 ms |
5548 KB |
Output is correct |
14 |
Correct |
238 ms |
5464 KB |
Output is correct |
15 |
Correct |
132 ms |
5512 KB |
Output is correct |
16 |
Correct |
115 ms |
5464 KB |
Output is correct |
17 |
Correct |
110 ms |
5464 KB |
Output is correct |
18 |
Correct |
134 ms |
5468 KB |
Output is correct |
19 |
Correct |
17 ms |
5464 KB |
Output is correct |
20 |
Correct |
17 ms |
5468 KB |
Output is correct |
21 |
Correct |
18 ms |
5448 KB |
Output is correct |
22 |
Correct |
17 ms |
5468 KB |
Output is correct |
23 |
Correct |
84 ms |
5356 KB |
Output is correct |
24 |
Correct |
82 ms |
5384 KB |
Output is correct |
25 |
Correct |
81 ms |
5212 KB |
Output is correct |
26 |
Correct |
29 ms |
5208 KB |
Output is correct |
27 |
Correct |
28 ms |
5212 KB |
Output is correct |
28 |
Correct |
28 ms |
5208 KB |
Output is correct |
29 |
Correct |
27 ms |
5212 KB |
Output is correct |
30 |
Correct |
46 ms |
5468 KB |
Output is correct |
31 |
Correct |
46 ms |
5456 KB |
Output is correct |
32 |
Correct |
45 ms |
5472 KB |
Output is correct |
33 |
Correct |
153 ms |
5464 KB |
Output is correct |
34 |
Correct |
154 ms |
5556 KB |
Output is correct |
35 |
Correct |
155 ms |
5556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
46 ms |
5720 KB |
Output is correct |
3 |
Correct |
47 ms |
5464 KB |
Output is correct |
4 |
Correct |
46 ms |
5456 KB |
Output is correct |
5 |
Incorrect |
193 ms |
39360 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
152 ms |
5468 KB |
Output is correct |
3 |
Correct |
154 ms |
5468 KB |
Output is correct |
4 |
Correct |
161 ms |
5564 KB |
Output is correct |
5 |
Incorrect |
111 ms |
40384 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
27 ms |
5212 KB |
Output is correct |
6 |
Correct |
44 ms |
5460 KB |
Output is correct |
7 |
Correct |
24 ms |
5208 KB |
Output is correct |
8 |
Correct |
47 ms |
5448 KB |
Output is correct |
9 |
Correct |
49 ms |
5460 KB |
Output is correct |
10 |
Correct |
47 ms |
5464 KB |
Output is correct |
11 |
Correct |
50 ms |
5468 KB |
Output is correct |
12 |
Correct |
55 ms |
5468 KB |
Output is correct |
13 |
Correct |
240 ms |
5548 KB |
Output is correct |
14 |
Correct |
238 ms |
5464 KB |
Output is correct |
15 |
Correct |
132 ms |
5512 KB |
Output is correct |
16 |
Correct |
115 ms |
5464 KB |
Output is correct |
17 |
Correct |
110 ms |
5464 KB |
Output is correct |
18 |
Correct |
134 ms |
5468 KB |
Output is correct |
19 |
Correct |
17 ms |
5464 KB |
Output is correct |
20 |
Correct |
17 ms |
5468 KB |
Output is correct |
21 |
Correct |
18 ms |
5448 KB |
Output is correct |
22 |
Correct |
17 ms |
5468 KB |
Output is correct |
23 |
Correct |
84 ms |
5356 KB |
Output is correct |
24 |
Correct |
82 ms |
5384 KB |
Output is correct |
25 |
Correct |
81 ms |
5212 KB |
Output is correct |
26 |
Correct |
29 ms |
5208 KB |
Output is correct |
27 |
Correct |
28 ms |
5212 KB |
Output is correct |
28 |
Correct |
28 ms |
5208 KB |
Output is correct |
29 |
Correct |
27 ms |
5212 KB |
Output is correct |
30 |
Correct |
46 ms |
5468 KB |
Output is correct |
31 |
Correct |
46 ms |
5456 KB |
Output is correct |
32 |
Correct |
45 ms |
5472 KB |
Output is correct |
33 |
Correct |
153 ms |
5464 KB |
Output is correct |
34 |
Correct |
154 ms |
5556 KB |
Output is correct |
35 |
Correct |
155 ms |
5556 KB |
Output is correct |
36 |
Correct |
1 ms |
4952 KB |
Output is correct |
37 |
Correct |
46 ms |
5720 KB |
Output is correct |
38 |
Correct |
47 ms |
5464 KB |
Output is correct |
39 |
Correct |
46 ms |
5456 KB |
Output is correct |
40 |
Incorrect |
193 ms |
39360 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |