QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#302880 | #2429. Conquer The World | zlt | AC ✓ | 540ms | 155576kb | C++14 | 1.8kb | 2024-01-11 14:38:42 | 2024-01-11 14:38:42 |
Judging History
answer
// Problem: P6943 [ICPC2018 WF] Conquer The World
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P6943
// Memory Limit: 1 MB
// Time Limit: 8000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 250100;
const ll inf = 1e12;
ll n, dep[maxn], a[maxn], ans;
vector<pii> G[maxn];
__gnu_pbds::priority_queue< ll, greater<ll> > A[maxn], B[maxn];
void dfs(int u, int fa) {
if (a[u] < 0) {
for (int _ = 0; _ < -a[u]; ++_) {
A[u].push(dep[u] - inf);
}
} else if (a[u] > 0) {
for (int _ = 0; _ < a[u]; ++_) {
B[u].push(dep[u]);
}
}
for (pii p : G[u]) {
ll v = p.fst, d = p.scd;
if (v == fa) {
continue;
}
dep[v] = dep[u] + d;
dfs(v, u);
A[u].join(A[v]);
B[u].join(B[v]);
}
while (A[u].size() && B[u].size() && A[u].top() + B[u].top() - dep[u] * 2 < 0) {
ll x = A[u].top(), y = B[u].top();
A[u].pop();
B[u].pop();
ans += x + y - dep[u] * 2;
A[u].push(dep[u] * 2 - y);
B[u].push(dep[u] * 2 - x);
}
}
void solve() {
scanf("%lld", &n);
for (int i = 1, u, v, d; i < n; ++i) {
scanf("%d%d%d", &u, &v, &d);
G[u].pb(v, d);
G[v].pb(u, d);
}
ll s = 0;
for (int i = 1, x, y; i <= n; ++i) {
scanf("%d%d", &x, &y);
a[i] = x - y;
if (a[i] < 0) {
s -= a[i];
}
}
dfs(1, -1);
printf("%lld\n", ans + s * inf);
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}
Details
Test #1:
score: 100
Accepted
time: 6ms
memory: 21448kb
Test #2:
score: 0
Accepted
time: 4ms
memory: 22616kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 21680kb
Test #4:
score: 0
Accepted
time: 6ms
memory: 23312kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 22640kb
Test #6:
score: 0
Accepted
time: 5ms
memory: 23480kb
Test #7:
score: 0
Accepted
time: 5ms
memory: 21820kb
Test #8:
score: 0
Accepted
time: 2ms
memory: 22652kb
Test #9:
score: 0
Accepted
time: 20ms
memory: 49768kb
Test #10:
score: 0
Accepted
time: 46ms
memory: 52456kb
Test #11:
score: 0
Accepted
time: 48ms
memory: 73932kb
Test #12:
score: 0
Accepted
time: 3ms
memory: 23340kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 23112kb
Test #14:
score: 0
Accepted
time: 0ms
memory: 22496kb
Test #15:
score: 0
Accepted
time: 3ms
memory: 22100kb
Test #16:
score: 0
Accepted
time: 0ms
memory: 23060kb
Test #17:
score: 0
Accepted
time: 4ms
memory: 22688kb
Test #18:
score: 0
Accepted
time: 3ms
memory: 24996kb
Test #19:
score: 0
Accepted
time: 13ms
memory: 35052kb
Test #20:
score: 0
Accepted
time: 88ms
memory: 87668kb
Test #21:
score: 0
Accepted
time: 92ms
memory: 36572kb
Test #22:
score: 0
Accepted
time: 102ms
memory: 36464kb
Test #23:
score: 0
Accepted
time: 109ms
memory: 38172kb
Test #24:
score: 0
Accepted
time: 57ms
memory: 31408kb
Test #25:
score: 0
Accepted
time: 66ms
memory: 33160kb
Test #26:
score: 0
Accepted
time: 119ms
memory: 38728kb
Test #27:
score: 0
Accepted
time: 108ms
memory: 69536kb
Test #28:
score: 0
Accepted
time: 149ms
memory: 57108kb
Test #29:
score: 0
Accepted
time: 214ms
memory: 86896kb
Test #30:
score: 0
Accepted
time: 270ms
memory: 120332kb
Test #31:
score: 0
Accepted
time: 247ms
memory: 99796kb
Test #32:
score: 0
Accepted
time: 210ms
memory: 91484kb
Test #33:
score: 0
Accepted
time: 214ms
memory: 89336kb
Test #34:
score: 0
Accepted
time: 310ms
memory: 105488kb
Test #35:
score: 0
Accepted
time: 179ms
memory: 92684kb
Test #36:
score: 0
Accepted
time: 210ms
memory: 97748kb
Test #37:
score: 0
Accepted
time: 339ms
memory: 125960kb
Test #38:
score: 0
Accepted
time: 417ms
memory: 154744kb
Test #39:
score: 0
Accepted
time: 297ms
memory: 146964kb
Test #40:
score: 0
Accepted
time: 292ms
memory: 99736kb
Test #41:
score: 0
Accepted
time: 258ms
memory: 113628kb
Test #42:
score: 0
Accepted
time: 234ms
memory: 101640kb
Test #43:
score: 0
Accepted
time: 209ms
memory: 101552kb
Test #44:
score: 0
Accepted
time: 205ms
memory: 106680kb
Test #45:
score: 0
Accepted
time: 255ms
memory: 117004kb
Test #46:
score: 0
Accepted
time: 6ms
memory: 22176kb
Test #47:
score: 0
Accepted
time: 19ms
memory: 69040kb
Test #48:
score: 0
Accepted
time: 0ms
memory: 23452kb
Test #49:
score: 0
Accepted
time: 178ms
memory: 85032kb
Test #50:
score: 0
Accepted
time: 103ms
memory: 38172kb
Test #51:
score: 0
Accepted
time: 5ms
memory: 22896kb
Test #52:
score: 0
Accepted
time: 4ms
memory: 22072kb
Test #53:
score: 0
Accepted
time: 0ms
memory: 22976kb
Test #54:
score: 0
Accepted
time: 2ms
memory: 21672kb
Test #55:
score: 0
Accepted
time: 4ms
memory: 22836kb
Test #56:
score: 0
Accepted
time: 2ms
memory: 23052kb
Test #57:
score: 0
Accepted
time: 5ms
memory: 23152kb
Test #58:
score: 0
Accepted
time: 4ms
memory: 23832kb
Test #59:
score: 0
Accepted
time: 53ms
memory: 41668kb
Test #60:
score: 0
Accepted
time: 136ms
memory: 57680kb
Test #61:
score: 0
Accepted
time: 327ms
memory: 120436kb
Test #62:
score: 0
Accepted
time: 352ms
memory: 126404kb
Test #63:
score: 0
Accepted
time: 217ms
memory: 140624kb
Test #64:
score: 0
Accepted
time: 227ms
memory: 140672kb
Test #65:
score: 0
Accepted
time: 540ms
memory: 140596kb
Test #66:
score: 0
Accepted
time: 529ms
memory: 140808kb
Test #67:
score: 0
Accepted
time: 275ms
memory: 135380kb
Test #68:
score: 0
Accepted
time: 302ms
memory: 154228kb
Test #69:
score: 0
Accepted
time: 298ms
memory: 155576kb
Test #70:
score: 0
Accepted
time: 280ms
memory: 154928kb