Submission #825622

#TimeUsernameProblemLanguageResultExecution timeMemory
825622t6twotwoTwo Currencies (JOI23_currencies)C++17
38 / 100
5053 ms36812 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M, Q; cin >> N >> M >> Q; vector<pair<int, int>> edges(N - 1); vector<vector<pair<int, int>>> adj(N); for (int i = 0; i < N - 1; i++) { int x, y; cin >> x >> y; x--, y--; edges[i] = {x, y}; adj[x].emplace_back(y, i); adj[y].emplace_back(x, i); } vector<int> P(M), C(M); vector<vector<int>> c(N - 1); for (int i = 0; i < M; i++) { cin >> P[i] >> C[i]; P[i]--; c[P[i]].push_back(C[i]); } if (C == vector(M, C[0])) { vector<int> cnt(N - 1); for (int i = 0; i < M; i++) { cnt[P[i]]++; } vector<int> f(N), dep(N); vector par(N, vector<int>(20, -1)); auto dfs = [&](auto dfs, int x) -> void { for (int i = 0; i < 19 && par[x][i] != -1; i++) { par[x][i + 1] = par[par[x][i]][i]; } for (auto [y, z] : adj[x]) { if (y != par[x][0]) { dep[y] = dep[x] + 1; f[y] = f[x] + cnt[z]; par[y][0] = x; dfs(dfs, y); } } }; dfs(dfs, 0); auto lca = [&](int x, int y) { if (dep[x] < dep[y]) { swap(x, y); } for (int i = 0; i < 20; i++) { if ((dep[x] - dep[y]) >> i & 1) { x = par[x][i]; } } if (x == y) { return x; } for (int i = 19; i >= 0; i--) { if (par[x][i] != par[y][i]) { x = par[x][i]; y = par[y][i]; } } return par[x][0]; }; while (Q--) { int S, T, X; ll Y; cin >> S >> T >> X >> Y; S--, T--; int k = f[S] + f[T] - 2 * f[lca(S, T)]; cout << max(-1LL, X - max(0LL, k - Y / C[0])) << "\n"; } return 0; } while (Q--) { int S, T, X; ll Y; cin >> S >> T >> X >> Y; S--, T--; vector<int> p(N, -1); queue<int> q; p[S] = S; q.push(S); while (!q.empty()) { int x = q.front(); q.pop(); for (auto [y, z] : adj[x]) { if (p[y] == -1) { p[y] = z; q.push(y); } } } priority_queue<int, vector<int>, greater<int>> pq; while (T != S) { for (int v : c[p[T]]) { pq.push(v); } auto [x, y] = edges[p[T]]; T ^= x ^ y; } while (!pq.empty() && pq.top() <= Y) { Y -= pq.top(); pq.pop(); } X -= pq.size(); X = max(X, -1); cout << X << "\n"; } return 6/22; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...