QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#641197#7069. FarminstallbTL 1853ms41492kbC++174.9kb2024-10-14 19:08:152024-10-14 19:08:15

Judging History

This is the latest submission verdict.

  • [2024-10-14 19:08:15]
  • Judged
  • Verdict: TL
  • Time: 1853ms
  • Memory: 41492kb
  • [2024-10-14 19:08:15]
  • Submitted

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

inline int rd()
{
    int x=0;
    char ch,t=0;
    while(!isdigit(ch = getchar())) t|=ch=='-';
    while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
    return t?-x:x;
}

const int N = 1e6 + 5, inf = 0x3f3f3f3f;
int n, m, Q, ans = inf, base, tot;
vector<int> v;
struct edge {
    int a, b, v, id;
    bool operator < (const edge & rhs) const {
        return v == rhs.v ? id < rhs.id : v < rhs.v;
    }
} e[N], E[N];
struct cons {
    int a, b;
} q[N];
struct ope {
    int a, b, f;
};
stack<ope> sta;

struct LCT {
    struct node {
        int rv, ch[2], fa, mx, id;
        #define ls(x) nod[x].ch[0]
        #define rs(x) nod[x].ch[1]
        #define fa(x) nod[x].fa
        #define rv(x) nod[x].rv
        #define mx(x) nod[x].mx
        #define id(x) nod[x].id
    } nod[N];
    bool chk(int x) { return rs(fa(x)) == x; }
    bool isroot(int x) { return nod[fa(x)].ch[chk(x)] != x; }
    void pushup(int x) {
        mx(x) = id(x);
        if (mx(ls(x)) && (!mx(x) || e[mx(ls(x))].v > e[mx(x)].v)) mx(x) = mx(ls(x));
        if (mx(rs(x)) && (!mx(x) || e[mx(rs(x))].v > e[mx(x)].v)) mx(x) = mx(rs(x));
    }
    void reverse(int x) { rv(x) ^= 1, swap(ls(x), rs(x)); }
    void pushdown(int x) {
        if (rv(x)) reverse(ls(x)), reverse(rs(x)), rv(x) = 0;
    }
    void connect(int x, int fa, int son) { fa(x) = fa, nod[fa].ch[son] = x; }
    void rotate(int x) {
        int y = fa(x), z = fa(y), ys = chk(x), zs = chk(y), u = nod[x].ch[!ys];
        if (isroot(y)) fa(x) = z;
        else connect(x, z, zs);
        connect(u, y, ys), connect(y, x, !ys), pushup(y), pushup(x);
    }
    void pushall(int x) { if (!isroot(x)) pushall(fa(x)); pushdown(x); }
    void splay(int x) {
        pushall(x);
        while (!isroot(x)) {
            if (!isroot(fa(x))) rotate(chk(x) == chk(fa(x)) ? fa(x) : x);
            rotate(x);
        }
    }
    void access(int x) { for (int y = 0; x; y = x, x = fa(x)) splay(x), rs(x) = y, pushup(x); }
    void makeroot(int x) { access(x), splay(x), reverse(x); }
    int findroot(int x) { access(x), splay(x); while (ls(x)) pushdown(x), x = ls(x); return splay(x), x; }
    bool connecting(int x, int y) { makeroot(y); return findroot(x) == y; }
    void link(int x, int y) { makeroot(y); if (findroot(x) != y) fa(y) = x; }
    void split(int x, int y) { makeroot(y), access(x), splay(x); }
    void cut(int x, int y) { split(x, y); if (ls(x) == y) ls(x) = fa(y) = 0, pushup(x); }
    int Max(int x, int y) { split(x, y); return mx(x); }
} lct;

void init() {
    for (int i = 1; i <= m; i++) {
        lct.nod[n + i] = {0, {0, 0}, 0, i, i};
    }
    sort(E + 1, E + m + 1);
    int cnt = 0;
    for (int i = 1; i <= m; i++) {
        int x = E[i].a, y = E[i].b;
        if (lct.connecting(x, y)) continue;
        cnt++;
        lct.link(x, n + E[i].id), lct.link(y, n + E[i].id);
        base += E[i].v;
    }
    if (cnt != n - 1) {
        printf("-1\n");
        exit(0);
    }
}

void calc() {
    int res = base;
    tot = n + m;
    for (int i : v) {
        int j = lct.Max(e[i].a, e[i].b);
        if (e[i].a == e[i].b) {
            res += e[i].v;
        } else if (j == m + 1) {
            res += e[i].v;
        } else {
            sta.push({e[j].a, j + n, -1});
            sta.push({e[j].b, j + n, -1});
            lct.cut(e[j].a, j + n);
            lct.cut(e[j].b, j + n);
            res -= e[j].v;
            res += e[i].v;
            tot++;
            lct.nod[tot] = {0, {0, 0}, 0, m + 1, m + 1};
            lct.link(e[i].a, tot);
            lct.link(e[i].b, tot);
            sta.push({e[i].a, tot, 1});
            sta.push({e[i].b, tot, 1});
        }
    }
    ans = min(ans, res);
    while (!sta.empty()) {
        int a = sta.top().a, b = sta.top().b, f = sta.top().f;
        sta.pop();
        if (f == 1) {
            lct.cut(a, b);
        } else {
            lct.link(a, b);
        }
    }
}

void solve() {
    int lim = 1 << Q;
    for (int S = 0; S < lim; S++) {
        v.clear();
        for (int i = 1; i <= Q; i++) {
            if ((S >> (i - 1)) & 1) {
                v.push_back(q[i].b);
            } else {
                v.push_back(q[i].a);
            }
        }
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        sort(v.begin(), v.end(), [](int x, int y) {
            return e[x].v == e[y].v ? x < y : e[x].v < e[y].v;
        });
        calc();
    }
}

int main() {
    n = rd(), m = rd();
    for (int i = 1; i <= m; i++) {
        int a, b, v; a = rd(), b = rd(), v = rd();
        E[i] = e[i] = {a, b, v, i};
    }
    E[m + 1] = e[m + 1] = {0, 0, -inf, m + 1};
    scanf("%d", &Q);
    for (int i = 1; i <= Q; i++) {
        int a, b; a = rd(), b = rd();
        q[i] = {a, b};
    }
    init();
    solve();
    printf("%d\n", ans);
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 9984kb

input:

4 6
1 1 2
2 4 3
1 1 4
2 4 4
3 2 4
1 3 4
1
1 2

output:

11

result:

ok single line: '11'

Test #2:

score: 0
Accepted
time: 815ms
memory: 40136kb

input:

100000 500000
2516 13348 191
37713 25720 216
41568 13765 877
2116 27917 895
76904 65435 37
73053 24687 44
97127 44338 700
2251 85769 378
95166 20208 42
59303 57463 158
26863 18030 31
58613 6818 2
15455 18106 254
3232 13720 610
85677 16778 650
25618 72746 813
80365 162 47
10930 7403 645
79272 54568 6...

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 1060ms
memory: 41492kb

input:

100000 500000
34497 87538 658
69862 2776 861
93620 16992 904
77910 81200 149
83935 83752 880
17602 75791 259
85887 53289 710
4200 79358 181
8518 19264 737
94665 47462 822
50632 51994 143
55224 59127 656
615 92858 150
48450 9465 58
35713 45287 140
64861 32248 517
70296 45113 153
11189 90316 809
40673...

output:

12148224

result:

ok single line: '12148224'

Test #4:

score: 0
Accepted
time: 98ms
memory: 39792kb

input:

1 500000
1 1 963
1 1 349
1 1 157
1 1 6
1 1 312
1 1 377
1 1 783
1 1 42
1 1 18
1 1 327
1 1 499
1 1 824
1 1 343
1 1 798
1 1 193
1 1 667
1 1 378
1 1 641
1 1 692
1 1 622
1 1 584
1 1 590
1 1 324
1 1 858
1 1 914
1 1 601
1 1 734
1 1 61
1 1 559
1 1 681
1 1 825
1 1 888
1 1 585
1 1 55
1 1 818
1 1 190
1 1 278
1...

output:

1605

result:

ok single line: '1605'

Test #5:

score: 0
Accepted
time: 316ms
memory: 39864kb

input:

5 500000
5 1 817
2 1 273
3 5 674
1 5 15
5 2 872
3 4 728
3 2 807
5 3 28
2 5 96
1 5 100
4 2 224
4 4 980
5 5 727
2 2 520
4 1 29
2 1 142
4 2 963
4 4 118
4 4 615
4 3 719
5 3 200
5 2 746
4 2 68
5 4 859
1 3 182
3 4 286
3 1 229
4 1 895
2 1 730
1 2 622
2 4 913
2 1 697
5 5 130
4 5 507
5 2 425
2 4 716
2 1 884
...

output:

3097

result:

ok single line: '3097'

Test #6:

score: 0
Accepted
time: 502ms
memory: 36876kb

input:

10 500000
3 8 138
10 7 593
4 3 8
7 5 516
10 4 49
3 8 601
6 7 481
8 5 429
6 4 241
1 6 504
6 2 252
7 1 656
5 1 350
5 9 485
7 8 669
5 8 630
9 9 324
1 3 427
1 2 309
5 10 236
4 6 926
8 7 34
5 1 336
7 5 581
4 5 228
10 3 909
2 9 726
4 2 444
10 1 55
1 2 244
5 8 261
2 7 556
10 2 165
6 3 657
7 5 580
7 1 827
1...

output:

1533

result:

ok single line: '1533'

Test #7:

score: 0
Accepted
time: 1003ms
memory: 36256kb

input:

100 500000
10 46 133
79 13 987
26 2 743
8 47 390
79 19 737
11 64 197
16 65 207
73 9 944
77 58 841
50 3 245
81 100 293
21 12 713
60 65 155
89 87 865
88 67 278
9 15 920
46 52 704
26 26 731
44 98 525
20 68 346
14 95 932
84 19 697
41 21 290
83 24 750
3 71 369
54 80 396
20 70 208
25 55 456
40 22 938
90 1...

output:

2209

result:

ok single line: '2209'

Test #8:

score: 0
Accepted
time: 250ms
memory: 12288kb

input:

1000 10000
186 620 701
360 808 963
181 434 297
873 511 550
949 641 670
36 318 299
635 543 56
284 519 439
816 900 877
84 189 141
393 679 222
169 669 974
826 703 651
201 659 644
1000 388 69
263 104 625
278 386 526
35 262 697
776 871 702
407 153 783
130 857 596
78 140 46
391 173 636
8 419 879
804 197 7...

output:

60430

result:

ok single line: '60430'

Test #9:

score: 0
Accepted
time: 348ms
memory: 18192kb

input:

1000 100000
64 501 272
114 900 116
858 404 897
442 351 101
232 419 117
803 929 38
451 759 769
39 387 881
906 961 105
82 652 795
657 958 636
55 986 248
623 912 446
326 513 863
886 207 977
908 104 591
932 132 666
239 166 495
772 97 650
22 485 978
584 308 526
548 70 979
359 993 226
462 881 398
134 222 ...

output:

7739

result:

ok single line: '7739'

Test #10:

score: 0
Accepted
time: 671ms
memory: 36284kb

input:

1000 500000
869 767 467
831 564 631
555 269 463
490 912 126
978 305 712
940 979 160
339 21 141
511 430 140
80 937 427
286 983 680
880 302 160
143 691 411
360 439 853
797 37 867
804 299 98
651 278 495
590 213 229
768 741 351
286 343 573
935 641 155
747 707 468
653 148 489
936 284 780
737 138 947
978 ...

output:

3777

result:

ok single line: '3777'

Test #11:

score: 0
Accepted
time: 876ms
memory: 37304kb

input:

10000 500000
831 9646 869
7698 1808 110
1692 824 716
1949 126 562
7824 2747 105
2753 2203 63
6720 5207 251
2425 3114 828
1073 5865 688
5748 7968 709
2843 5863 162
239 1523 688
6219 4445 289
5919 7428 216
4462 4775 865
2397 3242 384
459 5263 590
6806 5824 442
4834 3960 398
2802 1664 722
5875 6346 526...

output:

127714

result:

ok single line: '127714'

Test #12:

score: 0
Accepted
time: 781ms
memory: 40332kb

input:

100000 500000
99571 415 842
88646 76834 340
7335 47388 939
29506 84999 845
6493 2762 484
61459 21964 192
48115 21757 648
47229 69919 198
31212 80973 812
41884 81592 730
22948 57810 760
51775 97910 854
75732 989 810
9540 13078 465
76070 7833 486
66356 54068 56
24313 31298 9
11873 17766 532
66456 7046...

output:

-1

result:

ok single line: '-1'

Test #13:

score: 0
Accepted
time: 1346ms
memory: 40760kb

input:

100000 500000
12797 57359 174
19769 96152 455
96587 35545 660
45759 66401 996
82264 92296 828
49738 92800 886
6264 91012 156
8370 26551 923
47840 28952 983
86182 40818 337
36097 67441 984
16458 53827 896
72519 77300 503
44464 19421 386
78386 12255 967
72406 29902 239
17363 27044 112
11359 42336 780
...

output:

12108785

result:

ok single line: '12108785'

Test #14:

score: 0
Accepted
time: 738ms
memory: 10500kb

input:

1000 10000
224 608 374
666 376 667
338 310 763
941 335 581
29 148 960
715 103 365
229 965 998
761 784 956
278 173 523
943 668 997
922 959 17
785 873 618
77 715 587
249 424 358
411 822 724
493 60 375
702 629 182
106 905 43
609 830 323
353 290 814
69 512 391
102 982 797
902 381 935
1 124 858
318 239 9...

output:

62175

result:

ok single line: '62175'

Test #15:

score: 0
Accepted
time: 914ms
memory: 14488kb

input:

1000 100000
879 175 809
383 382 795
991 410 883
737 145 82
468 846 91
933 318 620
710 893 41
15 501 197
724 471 114
293 108 9
192 633 606
984 107 214
179 862 512
488 493 970
766 83 832
502 955 852
835 512 298
395 311 512
59 314 736
904 695 1000
214 580 187
565 431 454
567 220 329
402 62 428
712 382 ...

output:

8023

result:

ok single line: '8023'

Test #16:

score: 0
Accepted
time: 1275ms
memory: 36260kb

input:

1000 500000
480 625 239
545 57 466
571 149 292
571 849 18
721 961 130
328 284 100
198 563 318
387 597 144
170 582 102
500 793 466
836 453 294
121 539 94
323 767 888
512 379 406
935 244 45
459 74 617
711 494 257
860 113 14
292 115 646
787 283 209
143 34 991
686 63 365
6 132 975
96 754 739
572 789 963...

output:

4501

result:

ok single line: '4501'

Test #17:

score: 0
Accepted
time: 1451ms
memory: 37092kb

input:

10000 500000
1184 3108 786
7191 748 754
5726 987 56
1471 8722 327
6546 1456 69
7616 9880 350
9632 289 574
3021 4111 386
2148 374 943
8652 7726 109
6210 302 282
6331 6404 419
6317 5277 482
4818 7055 227
8438 661 890
1610 5069 522
3207 3171 984
5293 3712 817
7326 9628 639
31 5689 209
4030 9448 749
556...

output:

126618

result:

ok single line: '126618'

Test #18:

score: 0
Accepted
time: 1045ms
memory: 41480kb

input:

100000 500000
24389 38801 388
68266 67993 450
21673 73700 289
40603 55687 560
48860 19420 464
31513 52943 501
32436 90543 307
37182 68965 918
49470 88373 336
73085 92886 403
46163 74568 377
85055 70697 807
34488 49121 736
92860 51095 864
10076 48017 783
55197 1449 984
84685 37278 613
31985 15952 148...

output:

-1

result:

ok single line: '-1'

Test #19:

score: 0
Accepted
time: 1482ms
memory: 39048kb

input:

100000 500000
36395 44833 733
19257 62963 582
4221 94578 570
61950 32455 749
98164 17289 237
86685 26360 787
78779 55624 985
81765 83293 312
64655 24186 861
87154 10499 859
58502 23298 974
99932 85595 574
21762 48036 977
69375 98183 369
76024 41555 784
24746 61062 773
74076 89563 228
26923 49725 853...

output:

12019772

result:

ok single line: '12019772'

Test #20:

score: 0
Accepted
time: 1057ms
memory: 12288kb

input:

1000 10000
815 438 261
263 620 464
492 833 701
43 3 724
73 761 333
670 501 753
812 966 627
382 124 282
863 619 664
288 531 848
855 456 954
309 995 497
747 977 530
684 174 966
168 180 875
937 710 440
773 806 727
348 746 422
920 865 85
812 727 767
396 332 506
636 626 160
32 758 829
344 193 54
714 691 ...

output:

60511

result:

ok single line: '60511'

Test #21:

score: 0
Accepted
time: 1117ms
memory: 15720kb

input:

1000 100000
278 738 442
462 916 320
80 75 699
106 57 10
416 398 523
76 333 144
495 367 533
882 152 713
447 550 887
205 220 220
834 958 32
152 188 733
376 250 317
408 818 791
91 142 320
252 118 50
982 88 184
462 69 140
880 965 723
182 517 565
737 442 336
905 75 915
553 465 753
471 825 407
393 834 809...

output:

10574

result:

ok single line: '10574'

Test #22:

score: 0
Accepted
time: 1841ms
memory: 40228kb

input:

1000 500000
140 811 393
232 53 383
517 252 530
726 980 985
42 796 633
732 738 89
839 336 983
994 896 171
293 405 769
847 643 57
251 898 229
844 196 110
73 995 428
689 162 845
441 854 376
712 419 11
398 499 168
833 507 154
697 559 18
622 411 511
594 810 912
616 262 79
483 418 917
217 864 261
179 231 ...

output:

5072

result:

ok single line: '5072'

Test #23:

score: 0
Accepted
time: 1853ms
memory: 36276kb

input:

10000 500000
3548 4998 748
773 1447 567
4887 1282 294
5330 2854 293
8867 9780 275
6427 2334 339
6977 6870 535
9628 5219 900
1863 8493 121
9998 1576 892
8626 9939 513
9862 2253 434
5771 609 319
2699 5350 563
1944 5567 325
7160 7709 619
597 8880 600
5266 6811 662
7219 776 819
9866 1009 807
5185 1520 6...

output:

126914

result:

ok single line: '126914'

Test #24:

score: 0
Accepted
time: 1146ms
memory: 40656kb

input:

100000 500000
50847 99096 626
79421 12125 312
19803 7831 791
34006 75437 549
80797 6002 426
45416 20946 528
14855 5559 486
42721 22815 701
76077 96521 975
23512 4543 418
37104 31388 918
12936 38993 142
27994 41324 875
86922 6440 962
44763 53726 991
37874 91843 828
33090 58426 280
24524 11273 450
823...

output:

-1

result:

ok single line: '-1'

Test #25:

score: 0
Accepted
time: 1842ms
memory: 40284kb

input:

100000 500000
38677 51452 46
92797 50792 76
27756 67517 781
83039 90966 713
1856 20153 907
80757 72883 862
59510 23882 707
92401 44326 755
80746 84793 854
65465 91796 262
22070 44712 114
11696 4616 328
6882 57057 484
88644 59844 766
74636 28862 38
12409 13641 609
90106 95866 316
35766 81123 57
99518...

output:

12083121

result:

ok single line: '12083121'

Test #26:

score: 0
Accepted
time: 1162ms
memory: 10240kb

input:

1000 10000
986 200 591
474 282 554
743 141 189
126 222 221
16 411 529
40 332 733
307 830 520
332 398 854
943 816 893
952 240 74
878 82 232
940 927 885
990 52 267
559 41 297
995 378 278
690 311 631
742 462 133
533 933 865
592 616 343
333 551 483
450 749 409
718 564 711
169 83 386
420 503 629
10 707 8...

output:

61590

result:

ok single line: '61590'

Test #27:

score: 0
Accepted
time: 1829ms
memory: 16660kb

input:

1000 100000
481 458 905
251 681 173
302 615 954
420 419 292
987 598 982
80 490 663
163 11 598
625 871 551
627 707 15
246 406 856
492 397 346
677 774 323
843 625 658
845 86 46
191 464 198
997 492 967
888 353 357
432 283 964
445 548 930
976 503 726
408 311 745
714 909 889
293 448 601
618 470 152
424 8...

output:

12088

result:

ok single line: '12088'

Test #28:

score: -100
Time Limit Exceeded

input:

1000 500000
596 385 935
80 632 98
180 250 325
42 94 552
336 815 581
330 795 629
908 797 595
866 514 50
963 521 12
662 23 111
161 929 890
697 5 703
752 908 543
202 980 892
10 21 260
654 456 10
584 941 322
278 222 888
281 896 433
418 833 789
341 146 416
439 885 953
348 593 998
630 738 901
473 429 616
...

output:

5804

result: