제출 #917823

#제출 시각아이디문제언어결과실행 시간메모리
917823OAleksaTwo Currencies (JOI23_currencies)C++14
10 / 100
5028 ms25368 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
const int N = 1e5 + 69;
const int inf = 1e18;
int n, m, q, p[N], c[N], par[N], timer, tin[N], tout[N];
vector<int> g[N];
vector<tuple<int, int, int>> e;
map<pair<int, int>, vector<int>> cnt;
bool anc(int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}
void dfs(int v, int p) {
	par[v] = p;
	tin[v] = ++timer;
	for (auto u : g[v]) {
		if (u == p)
			continue;
		dfs(u, v);
	}
	tout[v] = timer;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tt = 1;
  //cin >> tt;
  while (tt--) {
  	cin >> n >> m >> q;
  	for (int i = 1;i <= n - 1;i++) {
  		int a, b;
  		cin >> a >> b;
  		g[a].push_back(b);
  		g[b].push_back(a);
  		e.push_back({a, b, 0});
  	}
  	for (int i = 1;i <= m;i++) {
  		cin >> p[i] >> c[i];
  		--p[i];
  		int a, b, w;
  		tie(a, b, w) = e[p[i]];
  		if (a > b)
  			swap(a, b);
  		cnt[{a, b}].push_back(c[i]);
  	}
  	dfs(1, 0);
  	for (int i = 1;i <= q;i++) {
  		int s, t, x, y;
  		cin >> s >> t >> x >> y;
  		vector<int> gas;
  		int up = s;
  		while (!anc(up, t)) {
				int x = up, y = par[up];
				if (x > y)
					swap(x, y);
				if (cnt.count({x, y})) {
					for (auto u : cnt[{x, y}])
						gas.push_back(u);
				}
  			up = par[up];
  		}
  		int v = t;
  		while (v != up) {
  			int x = v, y = par[v];
				if (x > y)
					swap(x, y);
				if (cnt.count({x, y})) {
					for (auto u : cnt[{x, y}])
						gas.push_back(u);
				}
  			v = par[v];
  		}
  		sort(gas.begin(), gas.end());
  		int ans = x, sm = 0, m = gas.size();
  		for (int i = 0;i < m;i++) {
  			if (sm + gas[i] <= y) 
  				sm += gas[i];
  			else {
  				if (m - i <= x) 
  					ans = x - (m - i);
  				else
  					ans = -1;
  				break;
  			}
  		}
  		cout << ans << '\n';
  	}
	}
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...