#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;
void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
int pow(int a,int b,int m){int ans=1;while(b){if(b&1){ans=(ans*a)%m;}b>>=1;a=(a*a)%m;}return ans;}
int binpow(int a,int b){int ans=1;while(b){if(b&1){ans=(ans*a);}b>>=1;a=(a*a);}return ans;}
const int N = 2e5 + 10;
vector <int> g[N], cost[N];
int up[N][30];
int tin[N], tout[N], ch[N], par[N], notch[N];
int timer;
void dfs(int v, int p){
par[v] = p;
tin[v] = timer++;
up[v][0] = p;
for (int i=1; i<=25; ++i)
up[v][i] = up[up[v][i-1]][i-1];
for(auto to : g[v]){
if(to == p)continue;
dfs(to, v);
}
tout[v] = timer++;
}
bool upper (int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
bool upper2 (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=25; i>=0; --i)
if (! upper (up[a][i], b))
a = up[a][i];
return up[a][0];
}
main(){
ios :: sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector <pair <int, int> > road = {{-1, -1}};
for(int i = 0; i < n - 1; i++){
int a, b;
cin >> a >> b;
road.pb({a, b});
g[a].pb(b);
g[b].pb(a);
}
dfs(1, 1);
for(int i = 1; i <= n; i++){
int x = road[i].ff, y = road[i].ss;
if(par[x] == y){
ch[i] = x;
notch[i] = y;
}else{
ch[i] = y;
notch[i] = x;
}
}
set <int> st;
vector <int> pref = {0};
int mr = 0;
for(int i = 0; i < m; i++){
int ind, silv;
cin >> ind >> silv;
pref.pb(silv);
st.insert(ind);
mr = ch[ind];
cost[ch[ind]].pb(silv);
}
sort(all(pref));
for(int i = 1; i <= m; i++){
pref[i] += pref[i - 1];
}
if((int)st.size() == 1){
for(int i = 0; i < q; i++){
int f, s, gold, silv;
cin >> f >> s >> gold >> silv;
int lc = lca(f, s);
//cout << mr <<"====" << endl;
if(upper2(lc, mr) && upper(mr, f) || upper(mr, s) && upper2(lc, mr)){
int ind = upper_bound(all(pref), silv) - pref.begin();
// cout << ind <<"->"<< m << endl;
if(gold < (m - ind + 1)){
cout <<"-1\n";
}else{
cout << gold - (m - ind + 1) << endl;
}
}else{
cout << gold << endl;
}
}
return 0;
}
//cout << "====" << endl;
for(int i = 0; i < q; i++){
int f, s, gold, silv;
cin >> f >> s >> gold >> silv;
vector <int> c;
int lc = lca(f, s);
while(s != lc){
for(auto x : cost[s]){
c.pb(x);
}
s= par[s];
}
while(f != lc){
for(auto x : cost[f]){
c.pb(x);
}
f = par[f];
}
sort(all(c));
for(auto x : c){
if(silv >= x){
silv -= x;
}else{
gold--;
}
}
if(gold < 0){
cout << "-1\n";
}else{
cout << gold << endl;
}
}
}
Compilation message
currencies.cpp:53:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
53 | main(){
| ^~~~
currencies.cpp: In function 'int main()':
currencies.cpp:99:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
99 | if(upper2(lc, mr) && upper(mr, f) || upper(mr, s) && upper2(lc, mr)){
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
currencies.cpp: In function 'void fp(std::string)':
currencies.cpp:12:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
currencies.cpp:12:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19292 KB |
Output is correct |
2 |
Correct |
3 ms |
19292 KB |
Output is correct |
3 |
Correct |
4 ms |
19292 KB |
Output is correct |
4 |
Correct |
3 ms |
19292 KB |
Output is correct |
5 |
Correct |
7 ms |
19544 KB |
Output is correct |
6 |
Correct |
8 ms |
19548 KB |
Output is correct |
7 |
Correct |
8 ms |
19292 KB |
Output is correct |
8 |
Correct |
8 ms |
19548 KB |
Output is correct |
9 |
Correct |
8 ms |
19548 KB |
Output is correct |
10 |
Correct |
8 ms |
19340 KB |
Output is correct |
11 |
Correct |
8 ms |
19548 KB |
Output is correct |
12 |
Correct |
8 ms |
19368 KB |
Output is correct |
13 |
Correct |
70 ms |
19800 KB |
Output is correct |
14 |
Correct |
70 ms |
19636 KB |
Output is correct |
15 |
Correct |
36 ms |
19544 KB |
Output is correct |
16 |
Correct |
27 ms |
19544 KB |
Output is correct |
17 |
Correct |
21 ms |
19548 KB |
Output is correct |
18 |
Correct |
38 ms |
19592 KB |
Output is correct |
19 |
Correct |
9 ms |
19548 KB |
Output is correct |
20 |
Correct |
7 ms |
19548 KB |
Output is correct |
21 |
Correct |
7 ms |
19548 KB |
Output is correct |
22 |
Correct |
7 ms |
19592 KB |
Output is correct |
23 |
Correct |
57 ms |
19548 KB |
Output is correct |
24 |
Correct |
45 ms |
19548 KB |
Output is correct |
25 |
Correct |
45 ms |
19548 KB |
Output is correct |
26 |
Correct |
7 ms |
19548 KB |
Output is correct |
27 |
Correct |
7 ms |
19548 KB |
Output is correct |
28 |
Correct |
8 ms |
19460 KB |
Output is correct |
29 |
Correct |
8 ms |
19548 KB |
Output is correct |
30 |
Correct |
8 ms |
19548 KB |
Output is correct |
31 |
Correct |
8 ms |
19588 KB |
Output is correct |
32 |
Correct |
8 ms |
19548 KB |
Output is correct |
33 |
Correct |
67 ms |
19600 KB |
Output is correct |
34 |
Correct |
73 ms |
19548 KB |
Output is correct |
35 |
Correct |
68 ms |
19544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19288 KB |
Output is correct |
2 |
Correct |
10 ms |
19544 KB |
Output is correct |
3 |
Correct |
8 ms |
19548 KB |
Output is correct |
4 |
Correct |
9 ms |
19584 KB |
Output is correct |
5 |
Correct |
269 ms |
55300 KB |
Output is correct |
6 |
Correct |
313 ms |
44472 KB |
Output is correct |
7 |
Correct |
272 ms |
49340 KB |
Output is correct |
8 |
Correct |
252 ms |
48832 KB |
Output is correct |
9 |
Correct |
221 ms |
49816 KB |
Output is correct |
10 |
Correct |
315 ms |
55488 KB |
Output is correct |
11 |
Correct |
326 ms |
55904 KB |
Output is correct |
12 |
Correct |
362 ms |
55468 KB |
Output is correct |
13 |
Correct |
339 ms |
55932 KB |
Output is correct |
14 |
Correct |
325 ms |
55444 KB |
Output is correct |
15 |
Execution timed out |
5042 ms |
60984 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19292 KB |
Output is correct |
2 |
Correct |
69 ms |
19548 KB |
Output is correct |
3 |
Correct |
72 ms |
19804 KB |
Output is correct |
4 |
Correct |
77 ms |
19544 KB |
Output is correct |
5 |
Execution timed out |
5057 ms |
56320 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19292 KB |
Output is correct |
2 |
Correct |
3 ms |
19292 KB |
Output is correct |
3 |
Correct |
4 ms |
19292 KB |
Output is correct |
4 |
Correct |
3 ms |
19292 KB |
Output is correct |
5 |
Correct |
7 ms |
19544 KB |
Output is correct |
6 |
Correct |
8 ms |
19548 KB |
Output is correct |
7 |
Correct |
8 ms |
19292 KB |
Output is correct |
8 |
Correct |
8 ms |
19548 KB |
Output is correct |
9 |
Correct |
8 ms |
19548 KB |
Output is correct |
10 |
Correct |
8 ms |
19340 KB |
Output is correct |
11 |
Correct |
8 ms |
19548 KB |
Output is correct |
12 |
Correct |
8 ms |
19368 KB |
Output is correct |
13 |
Correct |
70 ms |
19800 KB |
Output is correct |
14 |
Correct |
70 ms |
19636 KB |
Output is correct |
15 |
Correct |
36 ms |
19544 KB |
Output is correct |
16 |
Correct |
27 ms |
19544 KB |
Output is correct |
17 |
Correct |
21 ms |
19548 KB |
Output is correct |
18 |
Correct |
38 ms |
19592 KB |
Output is correct |
19 |
Correct |
9 ms |
19548 KB |
Output is correct |
20 |
Correct |
7 ms |
19548 KB |
Output is correct |
21 |
Correct |
7 ms |
19548 KB |
Output is correct |
22 |
Correct |
7 ms |
19592 KB |
Output is correct |
23 |
Correct |
57 ms |
19548 KB |
Output is correct |
24 |
Correct |
45 ms |
19548 KB |
Output is correct |
25 |
Correct |
45 ms |
19548 KB |
Output is correct |
26 |
Correct |
7 ms |
19548 KB |
Output is correct |
27 |
Correct |
7 ms |
19548 KB |
Output is correct |
28 |
Correct |
8 ms |
19460 KB |
Output is correct |
29 |
Correct |
8 ms |
19548 KB |
Output is correct |
30 |
Correct |
8 ms |
19548 KB |
Output is correct |
31 |
Correct |
8 ms |
19588 KB |
Output is correct |
32 |
Correct |
8 ms |
19548 KB |
Output is correct |
33 |
Correct |
67 ms |
19600 KB |
Output is correct |
34 |
Correct |
73 ms |
19548 KB |
Output is correct |
35 |
Correct |
68 ms |
19544 KB |
Output is correct |
36 |
Correct |
3 ms |
19288 KB |
Output is correct |
37 |
Correct |
10 ms |
19544 KB |
Output is correct |
38 |
Correct |
8 ms |
19548 KB |
Output is correct |
39 |
Correct |
9 ms |
19584 KB |
Output is correct |
40 |
Correct |
269 ms |
55300 KB |
Output is correct |
41 |
Correct |
313 ms |
44472 KB |
Output is correct |
42 |
Correct |
272 ms |
49340 KB |
Output is correct |
43 |
Correct |
252 ms |
48832 KB |
Output is correct |
44 |
Correct |
221 ms |
49816 KB |
Output is correct |
45 |
Correct |
315 ms |
55488 KB |
Output is correct |
46 |
Correct |
326 ms |
55904 KB |
Output is correct |
47 |
Correct |
362 ms |
55468 KB |
Output is correct |
48 |
Correct |
339 ms |
55932 KB |
Output is correct |
49 |
Correct |
325 ms |
55444 KB |
Output is correct |
50 |
Execution timed out |
5042 ms |
60984 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |