QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#380942 | #2429. Conquer The World | 8BQube# | WA | 10ms | 43268kb | C++20 | 3.5kb | 2024-04-07 15:20:35 | 2024-04-07 15:20:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define ALL(v) v.begin(), v.end()
#define pb push_back
#define SZ(a) ((int)a.size())
const int N = 250005;
set<pair<ll, pll>> pr;
int org[N], tar[N], flag[N];
struct Cent_Dec {
vector<pll> G[N];
pll info[N];
pll upinfo[N];
int n, pa[N], layer[N], sz[N], done[N];
ll dis[__lg(N) + 1][N];
set<pll> st[2][N];
void init(int _n) {
n = _n, layer[0] = -1;
fill_n(pa + 1, n, 0), fill_n(done + 1, n, 0);
for (int i = 1; i <= n; ++i) G[i].clear();
}
void add_edge(int a, int b, int w) {
G[a].pb(pll(b, w)), G[b].pb(pll(a, w));
}
void get_cent(int u, int f, int &mx, int &c, int num) {
int mxsz = 0;
sz[u] = 1;
for (pll e : G[u])
if (!done[e.X] && e.X != f) {
get_cent(e.X, u, mx, c, num);
sz[u] += sz[e.X], mxsz = max(mxsz, sz[e.X]);
}
if (mx > max(mxsz, num - sz[u]))
mx = max(mxsz, num - sz[u]), c = u;
}
void dfs(int u, int f, ll d, int orgg) {
dis[layer[orgg]][u] = d;
if (flag[u] != -1) st[flag[u]][orgg].insert(pll(d, u));
for (pll e : G[u])
if (!done[e.X] && e.X != f)
dfs(e.X, u, d + e.Y, orgg);
}
int cut(int u, int f, int num) {
int mx = 1e9, c = 0, lc;
get_cent(u, f, mx, c, num);
done[c] = 1, pa[c] = f, layer[c] = layer[f] + 1;
for (pll e : G[c]) {
if (!done[e.X]) {
if (sz[e.X] > sz[c])
lc = cut(e.X, c, num - sz[c]);
else lc = cut(e.X, c, sz[e.X]);
upinfo[lc] = pll(), dfs(e.X, c, e.Y, c);
}
}
if (flag[c] != -1) st[flag[c]][c].insert(pll(0, c));
if (auto p = get_pr(c); p.X != -1) pr.insert(p);
return done[c] = 0, c;
}
void build() { cut(1, 0, n); }
pair<ll, pll> get_pr(int u) {
if (st[0][u].empty() || st[1][u].empty()) return make_pair(-1, pll(-1, -1));
//cerr << "get_pr " << u << ": " << st[0][u].begin()->X + st[1][u].begin()->X << " " << st[0][u].begin()->Y << " --> " << st[1][u].begin()->Y << "\n";
return make_pair(st[0][u].begin()->X + st[1][u].begin()->X, pll(st[0][u].begin()->Y, st[1][u].begin()->Y));
}
void modify(int u) {
assert(flag[u] != -1);
for (int a = u, ly = layer[a]; a; a = pa[a], --ly) {
if (auto p = get_pr(a); p.X != -1) pr.erase(p);
st[flag[u]][a].erase(pll(dis[ly][u], u));
if (auto p = get_pr(a); p.X != -1) pr.insert(p);
}
}
} cent;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
cent.init(n);
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
cent.add_edge(u, v, w);
}
ll tot = 0;
for (int i = 1; i <= n; ++i) {
cin >> org[i] >> tar[i];
if (org[i] < tar[i]) flag[i] = 1, tot += tar[i] - org[i];
else if (org[i] > tar[i]) flag[i] = 0;
else flag[i] = -1;
}
cent.build();
ll ans = 0;
while (tot--) {
assert(!pr.empty());
auto [d, p] = *pr.begin();
ans += d;
--org[p.X], ++org[p.Y];
//cerr << "flow " << p.X << " --> " << p.Y << "\n";
if (org[p.X] == tar[p.X]) cent.modify(p.X);
if (org[p.Y] == tar[p.Y]) cent.modify(p.Y);
}
cout << ans << "\n";
}
Details
Test #1:
score: 100
Accepted
time: 0ms
memory: 42916kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 42916kb
Test #3:
score: 0
Accepted
time: 10ms
memory: 40956kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 41648kb
Test #5:
score: 0
Accepted
time: 3ms
memory: 42948kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 42932kb
Test #7:
score: 0
Accepted
time: 3ms
memory: 42376kb
Test #8:
score: 0
Accepted
time: 3ms
memory: 42996kb
Test #9:
score: 0
Accepted
time: 9ms
memory: 42916kb
Test #10:
score: 0
Accepted
time: 7ms
memory: 42928kb
Test #11:
score: 0
Accepted
time: 7ms
memory: 42984kb
Test #12:
score: -100
Wrong Answer
time: 9ms
memory: 43268kb