QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#105108 | #6406. Stage Clear | YaoBIG | ML | 1489ms | 265432kb | C++17 | 5.1kb | 2023-05-12 23:37:30 | 2023-05-12 23:37:33 |
Judging History
answer
#include "bits/stdc++.h"
#define rep(i, a, n) for (auto i = a; i <= (n); ++i)
#define revrep(i, a, n) for (auto i = n; i >= (a); --i)
#define all(a) a.begin(), a.end()
#define sz(a) (int)(a).size()
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, T b) { if (b < a) { a = b; return 1; } return 0; }
using namespace std;
template<class A, class B> string to_string(const pair<A, B> &p);
template<class A, class B, class C> string to_string(const tuple<A, B, C> &p);
template<class A, class B, class C, class D> string to_string(const tuple<A, B, C, D> &p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string) s); }
string to_string(char c) { return "'" + string(1, c) + "'"; }
string to_string(bool x) { return x ? "true" : "false"; }
template<class A> string to_string(const A &v) {
bool first = 1;
string res = "{";
for (const auto &x: v) {
if (!first) res += ", ";
first = 0;
res += to_string(x);
}
res += "}";
return res;
}
template<class A, class B> string to_string(const pair<A, B> &p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template<class A, class B, class C> string to_string(const tuple<A, B, C> &p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template<class A, class B, class C, class D> string to_string(const tuple<A, B, C, D> &p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template<class H, class... T> void debug_out(const H& h, const T&... t) {
cerr << " " << to_string(h);
debug_out(t...);
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) if (0) puts("No effect.")
#endif
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
/**
* Author: Yuhao Yao
* Date: 22-10-24
* Description: Disjoint set union. $merge(x, y)$ merges components which $x$ and $y$ are in respectively and returns $1$ if $x$ and $y$ are in different components.
* Time: amortized O(\alpha(M, N)) where $M$ is the number of operations. Almost constant in competitive programming.
*/
struct DSU {
vi fa, siz;
DSU(int n): fa(n), siz(n, 1) { iota(all(fa), 0); }
int getcomp(int x) {
return fa[x] == x ? x : fa[x] = getcomp(fa[x]);
}
bool merge(int x, int y) {
int fx = getcomp(x), fy = getcomp(y);
if (fx == fy) return 0;
if (siz[fx] < siz[fy]) swap(fx, fy);
fa[fy] = fx;
siz[fx] += siz[fy];
return 1;
}
};
struct node {
ll a, b;
node(ll a = 0, ll b = 0): a(a), b(b) {}
friend node operator +(const node &lhs, const node &rhs) {
ll a = max(lhs.a, lhs.a - lhs.b + rhs.a);
ll b = lhs.b + rhs.b - lhs.a - rhs.a + a;
return node{a, b};
}
friend bool operator <(const node &lhs, const node &rhs) {
bool negl = lhs.a > lhs.b;
bool negr = rhs.a > rhs.b;
if (negl != negr) return negl < negr;
else if (negl == 0) return lhs.a < rhs.a;
else return lhs.b > rhs.b;
}
};
ll hdu6326(const vector<node> &vs, const vvi &g) {
auto as = vs;
int n = sz(as);
vi pars(n);
auto dfs = [&](auto &dfs, int now, int fa) -> void {
pars[now] = fa;
for (auto v: g[now]) if (v != fa) {
dfs(dfs, v, now);
}
};
dfs(dfs, 0, -1);
DSU dsu(n);
vi rs(n); iota(all(rs), 0);
set<pair<node, int>> mp;
rep(i, 1, n - 1) mp.emplace(as[i], i);
while (sz(mp)) {
auto [nd, x] = *mp.begin();
mp.erase(mp.begin());
int fx = dsu.getcomp(x);
int rx = rs[fx];
assert(x == rx);
int y = pars[rx];
int fy = dsu.getcomp(y);
int ry = rs[fy];
dsu.merge(x, y);
rs[fx] = ry;
rs[fy] = ry;
if (ry != 0) mp.erase(make_pair(as[ry], ry));
as[ry] = as[ry] + as[rx];
if (ry != 0) mp.emplace(as[ry], ry);
}
return as[0].a;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, m; cin >> n >> m;
vector<node> as(n);
rep(i, 1, n - 1) cin >> as[i].a >> as[i].b;
vvi ig(n);
vi ig_msks(n);
rep(i, 1, m) {
int x, y; cin >> x >> y;
x--, y--;
ig[y].emplace_back(x);
ig_msks[y] |= 1 << x;
}
const ll inf = 1ll << 60;
if (n <= 26) {
int tot = 1 << n;
vector<ll> dp(tot, -inf), sums(tot);
dp[1] = 0;
rep(msk, 1, tot - 1) {
int low = __builtin_ctz(msk);
sums[msk] = sums[msk ^ (1 << low)] - as[low].a + as[low].b;
rep(i, 0, n - 1) if (!(msk >> i & 1)) {
if (ig_msks[i] & msk) chmax(dp[msk | (1 << i)], min(dp[msk], sums[msk] - as[i].a));
}
}
ll ans = -dp[tot - 1];
printf("%lld\n", ans);
} else {
vi chs(n);
ll ans = inf;
auto dfs = [&](auto &dfs, int now) -> void {
if (now == n) {
static vvi g(n);
for (auto &vec: g) vec.clear();
rep(i, 1, n - 1) g[chs[i]].push_back(i);
chmin(ans, hdu6326(as, g));
} else {
for (auto v: ig[now]) {
chs[now] = v;
dfs(dfs, now + 1);
}
}
};
dfs(dfs, 1);
printf("%lld\n", ans);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3572kb
input:
4 4 4 2 5 3 2 6 1 2 1 3 2 4 3 4
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3548kb
input:
15 14 254040392438309 117083115436273 500005748229691 557255157630172 821034233718230 865199673774998 659892147898798 987564141425694 81172575487567 811635577877255 751768357864605 341103322647288 454926350150218 140191090713900 921608121471585 659295670987251 223751724062143 505619245326640 8907765...
output:
1665396301509143
result:
ok 1 number(s): "1665396301509143"
Test #3:
score: 0
Accepted
time: 17ms
memory: 7212kb
input:
18 17 636830992776530 847574431876821 330869946457865 78274534165482 450581372553540 11565219334965 8736347226844 17186323694285 870805093198860 559070167736042 674369178493171 930151818400874 641605209598997 222521062460239 450936030349531 469197172169023 831295459816974 626096008793091 53095460351...
output:
2375957544280218
result:
ok 1 number(s): "2375957544280218"
Test #4:
score: 0
Accepted
time: 63ms
memory: 19316kb
input:
20 19 539893468691183 767805205447882 240338186903141 960937349402327 942645580569365 896509929612645 542601575005817 191461109090531 540992546866047 765080044816119 904535155855114 858111921213175 452499200048240 115895143306864 983856946412026 838504718536099 586421298181479 265212699386882 677124...
output:
800919806038419
result:
ok 1 number(s): "800919806038419"
Test #5:
score: 0
Accepted
time: 1115ms
memory: 265432kb
input:
24 23 114281007218527 308690671179962 145951034437731 718976086594208 709172151907814 926071954787084 224496444610281 498657753059525 874422017133378 857676356343078 532175866197017 818525693672607 303837639402605 374469705563954 512244364294540 952911486867703 748959419417502 249992707230361 512696...
output:
114281007218527
result:
ok 1 number(s): "114281007218527"
Test #6:
score: 0
Accepted
time: 2ms
memory: 3604kb
input:
36 35 389328367777319 678636570542258 32216944647452 612585362150577 891592845704885 596030605892036 688825276167602 461516360471825 916552899998310 106733202183953 400050408958777 670724326933521 995792861502757 894514508573875 14511185222713 612305257166443 175168368096281 508263855969282 85578802...
output:
171942144116875
result:
ok 1 number(s): "171942144116875"
Test #7:
score: 0
Accepted
time: 2ms
memory: 3584kb
input:
36 35 759037289890767 849577210686635 16379509883489 441829377955433 589378488455351 990818352083122 871208015900506 727359003875494 207852561142533 28766987248469 81321183353129 892618157632070 198487099788393 519364502513651 83942803274015 988821788304459 868185445880277 269956013388079 3834515054...
output:
759037289890767
result:
ok 1 number(s): "759037289890767"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3672kb
input:
36 35 100792831728257 823656493168793 866936535786311 187861146327778 132998929717538 605906559206892 3319598846477 393401056223733 964444786730964 932398059281618 925176496607384 148825907337833 985037559482190 646827297289525 469876125353024 641923164294854 453796287874442 291205025001534 72806942...
output:
1397699717661157
result:
ok 1 number(s): "1397699717661157"
Test #9:
score: 0
Accepted
time: 2ms
memory: 3560kb
input:
36 36 245996406159980 462974248377488 839352152971124 40282565369163 572695144110271 507726167903341 671102350267895 18090181781241 643724978558334 551787913319524 936340565446887 517649577919257 158127116487034 175750969611510 396852573858996 670814068366285 534702788102341 124550558279140 69441153...
output:
2508008255775889
result:
ok 1 number(s): "2508008255775889"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
34 38 656738239290510 319959252044415 760511943177376 828562698756504 470087249708484 441916827764162 105399930988058 761192720347117 81742549616394 195819875734286 782982110569406 72384154665629 647269989285797 720280547207448 531182311814386 160821851115134 292963780645658 871789628567253 74499577...
output:
656738239290510
result:
ok 1 number(s): "656738239290510"
Test #11:
score: 0
Accepted
time: 3ms
memory: 3568kb
input:
32 40 818105834607446 689904077664886 717146597564762 686987602224041 538827104521875 147060924732538 604913134601443 802546720879673 45376965619246 480061093729529 686039951678173 889398415870480 374408509732957 354006189233817 103818950629279 863526642478066 719174876808085 130061851080766 9744074...
output:
2289520618562758
result:
ok 1 number(s): "2289520618562758"
Test #12:
score: 0
Accepted
time: 27ms
memory: 3664kb
input:
30 42 730678486091139 762413502107242 564137648972911 492217680357057 677122869459914 634406715345550 766223620461328 750896882727596 34139073751269 875301336250330 948602995486093 589201509496124 333847023521138 673322700954330 774661538057122 360743409997856 301647343463502 78371781314140 44979585...
output:
2296677982487339
result:
ok 1 number(s): "2296677982487339"
Test #13:
score: 0
Accepted
time: 397ms
memory: 3596kb
input:
28 44 996216744822715 15265122654307 591377215147374 392892022614182 134817686923570 666778840251745 603108267679560 939679039946396 792878600465606 943254465658609 705582931165204 626247204621328 833947774992752 802610518921019 60510220659563 935537906466250 900509663884138 957082020010408 38517385...
output:
1021065751521024
result:
ok 1 number(s): "1021065751521024"
Test #14:
score: 0
Accepted
time: 1489ms
memory: 3556kb
input:
27 45 271265179156100 385209948242010 548010795825703 286502371912374 203557541769729 336737491323929 32253800857105 902537647325928 835008409588714 227495683621084 573457473959732 478446911624066 447407603972649 401150715116732 597962487418392 594931676764990 326718612562917 293848561935121 6497688...
output:
271265179156100
result:
ok 1 number(s): "271265179156100"
Test #15:
score: -100
Memory Limit Exceeded
input:
26 46 511128167626061 755154773895250 469460004382432 144928349121735 272299544034000 41881588292305 453271611317466 830211882616629 877138218711823 441367083696839 476515315035731 252150151731957 174547198161633 921197665643069 56919360991429 297636468095153 717743189152864 552120784448634 95767590...