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>
using namespace std;
//#define int int;
vector<vector<int>> adj;
vector<vector<int>> ddj;
vector<vector<int>> rdj;
vector<vector<int>> roads;
vector<vector<int>> che;
// BRUTE FORCE
int solve(int s, int t, int x, int y, int n){
// road, node, parent
queue<vector<int>> q;
q.push({-1, s, s});
vector<int> v(n, 0);
vector<int> parents(n, 0);
vector<int> ru(n, 0);
parents[s] = -1;
int node; int road; int parent;
while(!q.empty()){
node = q.front()[1]; road = q.front()[0]; parent = q.front()[2];
q.pop();
if(!v[node]){
parents[node] = parent;
ru[node] = road;
v[node] = 1;
for(int i = 0; i < adj[node].size(); i ++){
q.push({rdj[node][i], adj[node][i], node});
}
}
}
vector<int> coins;
node = t;
while(parents[node] != node){
if(ru[node] >= 0){
for(int i = 0; i < che[ru[node]].size(); i ++){
coins.push_back(che[ru[node]][i]);
}
}
node = parents[node];
}
sort(coins.begin(), coins.end());
for(int i = 0; i < coins.size(); i ++){
if(y - coins[i] >= 0){
y -= coins[i];
} else {
x -= 1;
}
}
if(x >= 0){
return x;
} else {
return -1;
}
}
// LCA
vector<int> visited;
vector<int> euler;
vector<int> first;
vector<int> height;
vector<int> prefix;
void dfs(int u, int h, int r) {
visited[u] = 1;
euler.push_back(u);
first[u] = euler.size() - 1;
height[u] = h;
/*if(r >= 0){
prefix.push_back(che[r].size());
}*/
//cout << u;
for (int i = 0; i < adj[u].size(); i ++){
if (!visited[adj[u][i]]){
dfs(adj[u][i], h + 1, rdj[u][i]);
/*if(r >= 0){
prefix.push_back(che[r].size());
}*/
euler.push_back(u);
}
}
}
vector<int> tolls;
void dfs2(int u, int p, int delta) {
visited[u] = 1;
tolls[u] = tolls[p] + delta;
for (int i = 0; i < adj[u].size(); i ++){
if (!visited[adj[u][i]]){
dfs2(adj[u][i], u, che[rdj[u][i]].size());
}
}
}
vector<pair<int, int>> tree(800005, pair<int, int>());
void upd(int i, int val, int node, int lbound, int rbound, int j = 1){
if(i < lbound || rbound < i){
return;
}
if(lbound == rbound){
tree[j] = {val, node};
return;
}
int mid = (lbound + rbound) / 2;
upd(i, val, node, lbound, mid, j * 2);
upd(i, val, node, mid+1, rbound, j * 2 + 1);
tree[j] = min(tree[j * 2], tree[j * 2 + 1]);
}
pair<int, int> rangeq(int ql, int qr, int lbound, int rbound, int j = 1){
if(qr < lbound || rbound < ql){
return {LONG_LONG_MAX / 2, 0};
}
if(ql <= lbound and rbound <= qr){
return tree[j];
}
int mid = (lbound + rbound) / 2;
pair<int, int> left_value = rangeq(ql, qr, lbound, mid, j * 2);
pair<int, int> right_value = rangeq(ql, qr, mid+1, rbound, j * 2 + 1);
if(left_value.first < right_value.first){
return left_value;
} else {
return right_value;
}
}
int main(){
int n, m, queries; cin >> n >> m >> queries;
vector<vector<int>> adj0(n, vector<int>());
vector<vector<int>> ddj0(n, vector<int>());
vector<vector<int>> rdj0(n, vector<int>());
vector<vector<int>> roads;
map<pair<int, int>, int> ab_to_road;
int subtask2 = 1;
int a, b, p, c;
for(int i = 0; i < n - 1; i ++){
cin >> a >> b; a -= 1; b -= 1;
roads.push_back({a, b});
ab_to_road[{min(a, b), max(a, b)}] = i;
}
vector<vector<int>> check(n-1, vector<int>());
int pc = 0;
for(int i = 0; i < m; i ++){
cin >> p >> c;
if(i == 0){
pc = c;
} else {
if(c != pc){
subtask2 = 0;
}
pc = c;
}
p -= 1;
check[p].push_back(c);
}
for(int i = 0; i < roads.size(); i ++){
adj0[roads[i][0]].push_back(roads[i][1]); adj0[roads[i][1]].push_back(roads[i][0]);
ddj0[roads[i][0]].push_back(roads[i][2]); ddj0[roads[i][1]].push_back(roads[i][2]);
rdj0[roads[i][0]].push_back(i); rdj0[roads[i][1]].push_back(i);
}
adj = adj0; ddj = ddj0; rdj = rdj0; che = check;
vector<int> v0(n, 0);
visited = v0; first = v0; height = v0;
long long s, t, x, y;
vector<long long> res;
dfs(0, 0, -1);
visited = v0;
tolls = v0;
long long tc = 0;
int silver = 0;
int gold = 0;
dfs2(0, 0, 0);
for(int i = 0; i < euler.size(); i ++){
upd(i, height[euler[i]], euler[i], 0, euler.size()-1);
}
for(int i = 0; i < queries; i ++){
cin >> s >> t >> x >> y;
s -= 1; t -= 1;
if(!subtask2){
res.push_back(solve(s, t, x, y, n));
} else {
tc = (tolls[s] + tolls[t] - 2 * tolls[rangeq(min(first[s], first[t]), max(first[s], first[t]), 0, euler.size()-1).second]);
//silver = y / c;
tc -= y / c;
x -= max(0ll, tc);
res.push_back(max(-1ll, x));
}
}
for(int i = 0; i < res.size(); i ++){
cout << res[i] << "\n";
}
vector<int> roadsfix(n-1, 0);
vector<int> prefix2;
vector<int> dp(n-1, 0);
/*if(subtask2){
//cout << 'FORTNITE';
//cout << "|-";
for(int i = 0; i < euler.size(); i ++){
cout << euler[i] + 1 << " ";
/*if(i >= 1){
prefix.push_back(ab_to_road[{min(euler[i], euler[i-1]), max(euler[i], euler[i-1])}]);
}
} cout << "\n";
prefix2.push_back(0);
for(int i = 0; i < prefix.size(); i ++){
if(!dp[prefix[i]]){
//cout << prefix[i] + 1 << " ";
prefix2.push_back(prefix2[i] + 1);
dp[prefix[i]] = 1;
} else {
prefix2.push_back(prefix2[i] - 1);
//cout << 0 << " ";
}
}
for(int i = 0; i < tolls.size(); i ++){
cout << i + 1 << " ";
} cout << "\n";
for(int i = 0; i < tolls.size(); i ++){
cout << tolls[i] << " ";
}
}*/
}
/*
g++ -std=gnu++17 -O2 -pipe -o 7 7.cpp
5 4 3
1 2
1 3
2 4
2 5
2 9
2 4
3 5
4 7
3 4 2 11
5 3 4 5
2 3 1 1
10 7 9
1 8
6 3
5 9
7 9
3 1
3 4
10 1
2 6
5 6
9 4
7 4
7 4
2 4
7 4
7 4
1 4
8 6 5 3
3 9 8 0
4 7 6 15
7 4 9 3
6 4 8 0
9 10 5 16
5 3 2 4
2 8 4 3
6 1 3 3
*/
Compilation message (stderr)
currencies.cpp:199:13: warning: "/*" within comment [-Wcomment]
199 | /*if(i >= 1){
|
currencies.cpp: In function 'int solve(int, int, int, int, int)':
currencies.cpp:26:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i = 0; i < adj[node].size(); i ++){
| ~~^~~~~~~~~~~~~~~~~~
currencies.cpp:35:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i = 0; i < che[ru[node]].size(); i ++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
currencies.cpp:42:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i = 0; i < coins.size(); i ++){
| ~~^~~~~~~~~~~~~~
currencies.cpp: In function 'void dfs(int, int, int)':
currencies.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int i = 0; i < adj[u].size(); i ++){
| ~~^~~~~~~~~~~~~~~
currencies.cpp: In function 'void dfs2(int, int, int)':
currencies.cpp:84:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for (int i = 0; i < adj[u].size(); i ++){
| ~~^~~~~~~~~~~~~~~
currencies.cpp: In function 'int main()':
currencies.cpp:149:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
149 | for(int i = 0; i < roads.size(); i ++){
| ~~^~~~~~~~~~~~~~
currencies.cpp:166:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
166 | for(int i = 0; i < euler.size(); i ++){
| ~~^~~~~~~~~~~~~~
currencies.cpp:186:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
186 | for(int i = 0; i < res.size(); i ++){
| ~~^~~~~~~~~~~~
currencies.cpp:163:9: warning: unused variable 'silver' [-Wunused-variable]
163 | int silver = 0;
| ^~~~~~
currencies.cpp:164:9: warning: unused variable 'gold' [-Wunused-variable]
164 | int gold = 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... |