QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#380973 | #2429. Conquer The World | 8BQube# | WA | 7ms | 72924kb | C++20 | 3.5kb | 2024-04-07 15:43:46 | 2024-04-07 15:43:47 |
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())
#define int long long
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;
int32_t 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";
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 55780kb
Test #2:
score: 0
Accepted
time: 3ms
memory: 55928kb
Test #3:
score: 0
Accepted
time: 4ms
memory: 58752kb
Test #4:
score: 0
Accepted
time: 3ms
memory: 57280kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 62332kb
Test #6:
score: 0
Accepted
time: 7ms
memory: 61308kb
Test #7:
score: 0
Accepted
time: 4ms
memory: 54788kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 61040kb
Test #9:
score: 0
Accepted
time: 0ms
memory: 60412kb
Test #10:
score: 0
Accepted
time: 0ms
memory: 57868kb
Test #11:
score: 0
Accepted
time: 3ms
memory: 60748kb
Test #12:
score: -100
Wrong Answer
time: 0ms
memory: 72924kb