#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_multiset tree<type1, null_type, less_equal<type1>, rb_tree_tag, tree_order_statistics_node_update>;
using namespace std;
using namespace __gnu_pbds;
const int mod = 998244353;
const long double PI = acos(-1.0);
const double epsilon = 1e-6;
const int N = 1e5 + 5;
void solve(){
int n,m,q;
cin >> n >> m >> q;
if(n <= 2000 && m <= 2000 && q <= 2000){
vector<int> u(n+1),v(n+1);
vector<vector<int> > g(n+1);
for(int i = 1; i < n; i++){
cin >> u[i] >> v[i];
g[u[i]].pb(v[i]);
g[v[i]].pb(u[i]);
}
map<pair<int,int>,vector<int> > dis;
for(int i = 0; i < m; i++){
int a,b;
cin >> a >> b;
dis[{u[a],v[a]}].pb(b);
dis[{v[a],u[a]}].pb(b);
}
vector<int> ansv;
int s,t,x,y;
function<void(int,int,vector<int>)> dfs = [&](int w,int p,vector<int> us){
for(auto i : dis[{p,w}]){
us.pb(i);
}
if(w == t){
ansv = us;
return;
}
for(int to : g[w]){
if(to != p){
dfs(to,w,us);
}
}
};
while(q--){
cin >> s >> t >> x >> y;
dfs(s,0,{});
sort(all(ansv));
for(int i : ansv){
if(i > y) x--;
else y -= i;
}
if(x >= 0) cout << x << '\n';
else cout << "-1\n";
}
}
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
solve();
}
}
Compilation message
currencies.cpp:72:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
72 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
305 ms |
1196 KB |
Output is correct |
6 |
Correct |
538 ms |
1108 KB |
Output is correct |
7 |
Correct |
313 ms |
892 KB |
Output is correct |
8 |
Correct |
648 ms |
1112 KB |
Output is correct |
9 |
Correct |
601 ms |
1296 KB |
Output is correct |
10 |
Correct |
611 ms |
860 KB |
Output is correct |
11 |
Correct |
632 ms |
1092 KB |
Output is correct |
12 |
Correct |
618 ms |
1108 KB |
Output is correct |
13 |
Correct |
1782 ms |
32540 KB |
Output is correct |
14 |
Correct |
1817 ms |
32928 KB |
Output is correct |
15 |
Correct |
1309 ms |
6824 KB |
Output is correct |
16 |
Correct |
1242 ms |
3984 KB |
Output is correct |
17 |
Correct |
1149 ms |
3644 KB |
Output is correct |
18 |
Correct |
1293 ms |
7352 KB |
Output is correct |
19 |
Correct |
549 ms |
1056 KB |
Output is correct |
20 |
Correct |
534 ms |
960 KB |
Output is correct |
21 |
Correct |
569 ms |
1204 KB |
Output is correct |
22 |
Correct |
526 ms |
856 KB |
Output is correct |
23 |
Correct |
967 ms |
1564 KB |
Output is correct |
24 |
Correct |
988 ms |
1452 KB |
Output is correct |
25 |
Correct |
954 ms |
1732 KB |
Output is correct |
26 |
Correct |
608 ms |
1196 KB |
Output is correct |
27 |
Correct |
621 ms |
936 KB |
Output is correct |
28 |
Correct |
581 ms |
1372 KB |
Output is correct |
29 |
Correct |
588 ms |
1104 KB |
Output is correct |
30 |
Correct |
597 ms |
984 KB |
Output is correct |
31 |
Correct |
596 ms |
1328 KB |
Output is correct |
32 |
Correct |
598 ms |
1216 KB |
Output is correct |
33 |
Correct |
1536 ms |
32836 KB |
Output is correct |
34 |
Correct |
1574 ms |
32912 KB |
Output is correct |
35 |
Correct |
1642 ms |
33128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
597 ms |
976 KB |
Output is correct |
3 |
Correct |
608 ms |
1260 KB |
Output is correct |
4 |
Correct |
578 ms |
856 KB |
Output is correct |
5 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1644 ms |
32816 KB |
Output is correct |
3 |
Correct |
1630 ms |
33072 KB |
Output is correct |
4 |
Correct |
1594 ms |
33128 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
305 ms |
1196 KB |
Output is correct |
6 |
Correct |
538 ms |
1108 KB |
Output is correct |
7 |
Correct |
313 ms |
892 KB |
Output is correct |
8 |
Correct |
648 ms |
1112 KB |
Output is correct |
9 |
Correct |
601 ms |
1296 KB |
Output is correct |
10 |
Correct |
611 ms |
860 KB |
Output is correct |
11 |
Correct |
632 ms |
1092 KB |
Output is correct |
12 |
Correct |
618 ms |
1108 KB |
Output is correct |
13 |
Correct |
1782 ms |
32540 KB |
Output is correct |
14 |
Correct |
1817 ms |
32928 KB |
Output is correct |
15 |
Correct |
1309 ms |
6824 KB |
Output is correct |
16 |
Correct |
1242 ms |
3984 KB |
Output is correct |
17 |
Correct |
1149 ms |
3644 KB |
Output is correct |
18 |
Correct |
1293 ms |
7352 KB |
Output is correct |
19 |
Correct |
549 ms |
1056 KB |
Output is correct |
20 |
Correct |
534 ms |
960 KB |
Output is correct |
21 |
Correct |
569 ms |
1204 KB |
Output is correct |
22 |
Correct |
526 ms |
856 KB |
Output is correct |
23 |
Correct |
967 ms |
1564 KB |
Output is correct |
24 |
Correct |
988 ms |
1452 KB |
Output is correct |
25 |
Correct |
954 ms |
1732 KB |
Output is correct |
26 |
Correct |
608 ms |
1196 KB |
Output is correct |
27 |
Correct |
621 ms |
936 KB |
Output is correct |
28 |
Correct |
581 ms |
1372 KB |
Output is correct |
29 |
Correct |
588 ms |
1104 KB |
Output is correct |
30 |
Correct |
597 ms |
984 KB |
Output is correct |
31 |
Correct |
596 ms |
1328 KB |
Output is correct |
32 |
Correct |
598 ms |
1216 KB |
Output is correct |
33 |
Correct |
1536 ms |
32836 KB |
Output is correct |
34 |
Correct |
1574 ms |
32912 KB |
Output is correct |
35 |
Correct |
1642 ms |
33128 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
597 ms |
976 KB |
Output is correct |
38 |
Correct |
608 ms |
1260 KB |
Output is correct |
39 |
Correct |
578 ms |
856 KB |
Output is correct |
40 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |