QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#308886 | #8136. Rebellious Edge | ucup-team896# | RE | 194ms | 40832kb | C++20 | 4.3kb | 2024-01-20 13:34:01 | 2024-01-20 13:34:02 |
Judging History
answer
// Author: Klay Thompson
// Problem: P4716 【模板】最小树形图
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4716
// Memory Limit: 250 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#ifdef dbg
#define D(...) fprintf(stderr, __VA_ARGS__)
#define DD(...) D(#__VA_ARGS__ " = "), debug_helper::debug(__VA_ARGS__), D("\n")
#include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp"
#else
#define D(...) ((void)0)
#define DD(...) ((void)0)
#endif
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using namespace std;
const ll inf = 1E18;
const int N = 400010;
const int M = 500010;
#define int ll
namespace Edmonds {
struct ltt_node {
int lson, rson;
int val, tag;
int from, to;
int dis;
};
struct leftist_tree {
ltt_node ltt[M];
int tot;
void init() {
rep (i, 1, tot) {
ltt[i].lson = ltt[i].rson = 0;
ltt[i].val = ltt[i].tag = 0;
ltt[i].from = ltt[i].to = 0;
ltt[i].dis = 0;
}
tot = 0;
}
int newnode(int val, int from, int to) {
tot++;
ltt[tot].val = val;
ltt[tot].from = from;
ltt[tot].to = to;
return tot;
}
void pushdown(int now) {
int ls = ltt[now].lson, rs = ltt[now].rson;
ltt[ls].val += ltt[now].tag;
ltt[rs].val += ltt[now].tag;
ltt[ls].tag += ltt[now].tag;
ltt[rs].tag += ltt[now].tag;
ltt[now].tag = 0;
}
int merge(int x, int y) {
if (!x || !y) return x + y;
pushdown(x), pushdown(y);
if (ltt[x].val > ltt[y].val) swap(x, y);
ltt[x].rson = merge(ltt[x].rson, y);
if (ltt[ltt[x].rson].dis > ltt[ltt[x].lson].dis)
swap(ltt[x].lson, ltt[x].rson);
ltt[x].dis = ltt[ltt[x].rson].dis + 1;
return x;
}
int del(int rt) {
pushdown(rt);
int ls = ltt[rt].lson;
int rs = ltt[rt].rson;
return merge(ls, rs);
}
};
leftist_tree ltt;
int root[N], fa[N];
int sta[N], top;
bool vis[N];
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void add_edge(int u, int v, int w) {
int lp = ltt.newnode(w, u, v);
root[v] = ltt.merge(root[v], lp);
}
int dmst(int n, int r) {
for (int i = 1; i <= n; i++) {
int x = i, y = i % n + 1;
int p = ltt.newnode(inf, x, y);
root[y] = ltt.merge(root[y], p);
}
for (int i = 1; i <= 2 * n; i++) fa[i] = i;
sta[++top] = r, vis[r] = true;
int ans = 0, cnt = n;
while (root[sta[top]]) {
int lp = root[sta[top]];
ltt_node tmp = ltt.ltt[lp];
int u = find(tmp.from);
if (u == sta[top]) {
root[sta[top]] = ltt.del(root[sta[top]]);
continue;
}
if (!vis[u]) {
sta[++top] = u, vis[u] = true;
continue;
}
int p = ++cnt;
while (vis[u]) {
int v = sta[top--];
vis[v] = false, fa[v] = p;
int val = ltt.ltt[root[v]].val;
ltt.ltt[root[v]].tag -= val;
int x = find(ltt.ltt[root[v]].to);
ans += (x != find(r)) * val;
root[v] = ltt.del(root[v]);
root[p] = ltt.merge(root[p], root[v]);
}
sta[++top] = p, vis[p] = true;
}
return ans;
}
}
int n, m, rt;
void work() {
cin >> n >> m, rt = 1;
fill(Edmonds::root + 1, Edmonds::root + 2 * n + 1, 0);
fill(Edmonds::fa + 1, Edmonds::fa + 2 * n + 1, 0);
fill(Edmonds::sta + 1, Edmonds::sta + 2 * n + 1, 0);
fill(Edmonds::vis + 1, Edmonds::vis + 2 * n + 1, false);
Edmonds::top = 0, Edmonds::ltt.init();
rep (i, 1, m) {
int u, v, w; cin >> u >> v >> w;
Edmonds::add_edge(u, v, w);
}
ll ans = Edmonds::dmst(n, rt);
cout << (ans >= inf ? -1 : ans) << '\n';
}
signed main() {
cin.tie(nullptr) -> ios::sync_with_stdio(false);
int t; cin >> t;
while (t--) work();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 9752kb
input:
3 5 6 1 2 4 1 3 2 2 3 0 3 4 1 4 5 1 5 2 1 4 4 1 2 4 1 3 6 1 4 8 4 2 1000000 3 3 1 2 100 2 1 10 2 3 1000
output:
5 18 1100
result:
ok 3 number(s): "5 18 1100"
Test #2:
score: 0
Accepted
time: 41ms
memory: 11884kb
input:
50000 4 5 2 4 998973548 2 3 501271695 4 1 778395982 1 4 32454891 1 2 757288934 4 5 1 4 720856669 2 3 665098951 3 4 407461517 2 1 866914401 1 2 457859826 4 5 1 2 75288664 1 4 624893594 3 2 458973866 2 4 769074655 2 3 834773751 4 5 2 3 237060634 2 4 297307882 3 4 200383586 1 2 42856502 3 1 16574713 4 ...
output:
1291015520 1530420294 1534956009 480300722 1366795927 1541095843 2493849488 858095911 1034153425 793861088 605832428 1051598350 612891589 1265994009 517769091 1678219738 1556463491 93634961 960978736 984886788 1696503797 1002892611 1969660290 1431417780 1515267731 977157479 1937478556 654475526 1401...
result:
ok 50000 numbers
Test #3:
score: 0
Accepted
time: 55ms
memory: 11868kb
input:
50000 4 6 1 3 754771977 2 3 517886121 1 4 392356144 1 2 702785740 3 4 888230940 2 1 829304957 4 6 2 4 255940037 1 2 380616616 1 4 819140865 3 4 36965607 1 3 496378345 4 3 652252309 4 6 2 4 179216787 4 2 401136502 1 2 715271531 1 3 859345710 3 4 24470489 1 4 148650889 4 6 3 2 20348069 1 2 152861663 1...
output:
1613028005 913960568 1284952701 1294564551 199037829 1236121455 983447828 1161647829 1289612543 2444304029 431872921 1272140390 949528400 2328941976 696541797 363553357 999320657 2221495861 879052116 1287531701 912524980 1072419431 1187727045 1571845059 1184110956 1136184594 430092563 1132894799 962...
result:
ok 50000 numbers
Test #4:
score: 0
Accepted
time: 39ms
memory: 9840kb
input:
25000 8 8 5 3 903349949 5 6 230898386 6 7 456285865 2 3 235279998 3 8 75554555 1 2 534445049 2 5 91984463 3 4 930308596 8 8 4 8 542820075 2 1 505042427 1 2 383870408 1 4 115526736 2 5 120351002 4 7 303640587 2 3 343568518 1 6 643773351 8 8 1 2 362133720 1 3 865161709 2 4 418085685 2 5 431348178 1 6 ...
output:
2554756912 2453550677 3352164037 2548011007 2946870949 2974098273 3702415713 3077518439 3933251840 3561133042 3661712043 3019312810 3492992897 3247019175 2091050366 3228447489 4450182982 4547489101 2740661646 3507248157 3421959708 3851505640 4373872101 3925763540 4160440497 2854028840 2797343113 338...
result:
ok 25000 numbers
Test #5:
score: 0
Accepted
time: 41ms
memory: 11868kb
input:
25000 8 9 1 3 8441923 2 8 804745873 1 4 814834377 1 5 576030519 8 5 953473409 2 6 714140471 2 5 77120962 1 2 511057482 4 7 145441656 8 9 1 2 276813465 4 5 165256724 1 4 522691074 1 7 417909938 1 8 962962225 1 3 805076553 2 6 623033783 8 4 647061395 3 4 815842049 8 9 3 5 20296808 3 6 597281111 1 2 76...
output:
3075782744 3773743762 3278650492 3231838190 3374994424 2926778533 4348233362 2822143392 3076722255 2596140564 2247390371 3854832394 4319882601 3180102719 3094944789 3435791714 4405129128 2340656122 2604427605 3572178751 3803314864 3351077905 3767890679 3567633269 3527865042 2895660631 3817016063 188...
result:
ok 25000 numbers
Test #6:
score: 0
Accepted
time: 45ms
memory: 9884kb
input:
25000 8 10 7 4 221904276 1 5 121915318 1 3 600836161 4 7 488300065 1 4 921802874 1 2 757229498 2 3 307033253 3 6 824028669 1 7 171060397 3 8 972502017 8 10 3 5 957779273 1 2 548128903 1 3 973419517 6 5 565445547 3 6 551907938 1 7 793064110 7 8 466118245 1 4 353130404 5 6 927135859 2 8 758403259 8 10...
output:
3375673428 4251214664 3757000574 3166584844 2788975514 2116310775 2715058780 3287246298 4025635987 3624892566 2769498633 2958022862 4290202794 2920552027 3277314894 3924975405 3237273140 2821648429 2360729796 3475710401 3972691777 4115444652 2692775193 3118467245 3132060418 2872337367 4722221967 264...
result:
ok 25000 numbers
Test #7:
score: 0
Accepted
time: 58ms
memory: 9848kb
input:
25000 8 12 2 6 449032103 1 5 646093619 7 8 888519232 2 4 749443783 4 5 475422884 1 2 434507939 6 7 525256368 3 7 280175107 6 4 267731039 2 3 727214343 5 7 91009177 1 6 499664950 8 12 4 6 657574176 1 2 5296179 5 7 312584650 3 4 999483683 2 3 491085057 2 5 719151963 7 2 741976162 1 4 636522791 1 6 458...
output:
3333436717 2807585131 4920461769 3090434252 2560790821 2380624756 2626592068 3772048085 2624368713 1844640463 2565437000 2622643735 2416136939 2618783813 2813654707 3205164529 3974831765 3722108973 2648137059 3165927114 2050934228 3202220845 3802856334 3396453248 2597210524 3975316045 2920520206 301...
result:
ok 25000 numbers
Test #8:
score: 0
Accepted
time: 63ms
memory: 11904kb
input:
200 1000 1000 239 588 133547926 183 534 928409215 241 330 641475654 535 957 589849559 10 620 614874088 309 928 569710487 3 11 494917111 543 890 68083403 400 918 317003458 251 321 376382937 147 160 903343522 452 924 589468457 20 197 468355859 108 599 820206568 230 378 910782377 220 293 903007367 115 ...
output:
497713793250 492962928026 507239162096 489416880760 498919499738 494390387999 505624748297 502273360170 487437969890 492882286087 488855359914 493516382821 492154885694 494693728666 501962518846 492234657903 499522656353 518342153867 494724608349 500831455708 498690702121 494557144727 503463772263 5...
result:
ok 200 numbers
Test #9:
score: 0
Accepted
time: 63ms
memory: 11924kb
input:
200 1000 1005 508 559 424192332 74 278 148045823 327 993 663345255 157 544 928557499 20 41 523494941 51 254 437200278 2 18 506681783 782 928 138677470 12 838 503910272 156 645 862366636 200 508 218413621 231 635 194448744 611 619 124981290 255 469 614512720 134 333 818641028 181 412 921437717 5 184 ...
output:
501129145945 508207765375 504606717649 501142420466 496433756584 508422487274 507007286774 489195446348 499101136725 504420343219 485311666315 489123709490 499113103288 496900542557 481508383246 492384657383 496513770885 509736025057 495760467649 497002983791 491910563136 494017531362 498058386677 4...
result:
ok 200 numbers
Test #10:
score: 0
Accepted
time: 119ms
memory: 9860kb
input:
200 1000 2000 39 238 864415857 26 156 707268612 146 546 499579162 417 761 332452795 10 410 961630809 587 745 361841763 131 716 968927701 834 888 74441039 142 781 613126744 221 273 614239182 900 945 849004495 668 938 475774163 297 748 413513365 679 862 473286635 262 523 476328885 852 977 55421955 256...
output:
377088491179 368197541228 378166264449 385285967410 363829015221 378666687744 376662529392 383676533289 374782208099 373676435135 373318306315 377833081332 386724232469 362733514349 384542854056 376214712692 376322456811 374258497868 379984477018 375276245615 384876693250 369634790945 375323862443 3...
result:
ok 200 numbers
Test #11:
score: 0
Accepted
time: 145ms
memory: 9772kb
input:
160 1000 3000 361 390 785950892 564 577 524217752 334 931 11528988 228 304 70325077 694 747 1928318 474 945 249706711 239 912 124894561 736 834 191207404 215 303 773416167 182 763 70656237 454 457 937174144 346 568 535198270 666 676 739691416 478 631 580490353 263 625 449713668 64 907 740013425 44 2...
output:
299833687308 304977657092 307270423217 294716038190 309503138234 305635005136 310814442139 312934222331 299600885716 305288964059 308283739944 300775461259 301205352773 309409716517 296295870502 294594618853 299117813318 295956044320 300467617156 305256736121 305862015525 314513636581 294398803135 2...
result:
ok 160 numbers
Test #12:
score: 0
Accepted
time: 43ms
memory: 13956kb
input:
1 1000 100000 50 415 104868280 691 700 424553918 111 968 56214986 269 529 173077590 52 117 925712505 535 560 991283286 444 729 923471207 917 964 567645976 122 305 958342700 682 945 719398103 383 527 201210989 304 318 705615051 152 926 526523918 18 527 542190718 253 593 892670139 688 706 379199924 57...
output:
23974268933
result:
ok 1 number(s): "23974268933"
Test #13:
score: 0
Accepted
time: 137ms
memory: 36580kb
input:
1 200000 200000 46957 87435 612302422 91840 173370 891722968 25009 74170 884361660 74176 99042 131797405 16139 58764 648773820 58722 191045 349784377 24095 72719 77508332 7510 25316 299592819 51067 124120 258305363 65132 104676 752760402 19337 21868 193391749 21056 155761 223617084 69406 105016 6650...
output:
100416224076541
result:
ok 1 number(s): "100416224076541"
Test #14:
score: 0
Accepted
time: 132ms
memory: 36412kb
input:
1 200000 200005 18188 18759 447257093 23283 144020 296580050 31611 45965 223988792 38100 119202 291195007 101055 137132 899526750 5591 49075 27414736 56949 151097 671413227 41985 47180 503110244 30438 46109 460979545 86928 117978 250704533 12466 168536 241962293 136066 184729 200523801 8327 161157 9...
output:
99973876572792
result:
ok 1 number(s): "99973876572792"
Test #15:
score: 0
Accepted
time: 194ms
memory: 40832kb
input:
1 200000 300000 155561 161597 537100897 126654 128124 199395618 71901 163500 259430846 142983 177985 36516674 51299 136136 392555170 27776 129030 599861396 196834 199267 608694287 10521 128879 579122252 53483 134123 144345200 101992 194707 710827766 9703 25975 222504051 40022 110651 509241864 6002 1...
output:
85802950605768
result:
ok 1 number(s): "85802950605768"
Test #16:
score: -100
Runtime Error
input:
1 200000 400000 83662 178071 426605522 11425 16196 19198021 143760 158308 516994131 32332 137595 862193296 49812 100060 500080963 158302 197705 152597027 31283 167752 375247059 3021 147864 874473750 79941 123131 80284324 18754 41021 30099126 20756 24387 802117779 9987 18379 197504832 8515 74052 3263...