QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#707328 | #2516. Optimization for UltraNet | becaido | AC ✓ | 100ms | 15616kb | C++20 | 1.8kb | 2024-11-03 15:31:20 | 2024-11-03 15:31:21 |
Judging History
answer
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout<<"["<<#HEHE<<"] : ",dout(HEHE)
void dout(){cout<<'\n';}
template<typename T,typename...U>
void dout(T t,U...u){cout<<t<<(sizeof...(u)?", ":""),dout(u...);}
#else
#define debug(...) 7122
#endif
#define int long long
#define ll long long
#define Waimai ios::sync_with_stdio(false),cin.tie(0)
#define FOR(x,a,b) for(int x=a,I=b;x<=I;x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 1e4 + 5;
const int MSIZ = 5e5 + 5;
int n, m, cc;
tuple<int, int, int> e[MSIZ];
int to[SIZE], h[SIZE];
bool is[MSIZ];
int dsu (int x) {
return x == to[x] ? x : (to[x] = dsu (to[x]));
}
bool Merge (int a, int b) {
a = dsu (a), b = dsu (b);
if (a == b) return 0;
if (h[a] < h[b]) swap (a, b);
to[b] = a;
h[a] += h[b];
return 1;
}
void solve() {
cin >> n >> m;
FOR (i, 1, m) {
auto &[w, a, b] = e[i];
cin >> a >> b >> w;
}
sort (e + 1, e + m + 1);
cc = n;
iota (to, to + n + 1, 0);
fill (h, h + n + 1, 1);
int start;
for (int i = m; i >= 1; i--) {
auto [w, a, b] = e[i];
if (Merge (a, b)) cc--;
if (cc == 1) {
start = i;
break;
}
}
iota (to, to + n + 1, 0);
fill (h, h + n + 1, 1);
FOR (i, start, m) {
auto [w, a, b] = e[i];
is[i] = Merge (a, b);
}
int ans = 0;
iota (to, to + n + 1, 0);
fill (h, h + n + 1, 1);
for (int i = m; i >= 1; i--) if (is[i]) {
auto [w, a, b] = e[i];
a = dsu (a), b = dsu (b);
ans += w * h[a] * h[b];
Merge (a, b);
}
cout << ans << '\n';
}
int32_t main() {
Waimai;
solve();
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3764kb
input:
3 3 1 2 5 1 3 6 2 3 8
output:
20
result:
ok single line: '20'
Test #2:
score: 0
Accepted
time: 74ms
memory: 14160kb
input:
1000 400000 14 454 44070 454 833 388967 214 833 224325 214 299 42867 299 716 204641 496 716 360732 156 496 82915 156 246 194391 225 246 171063 225 935 316414 733 935 247844 229 733 40569 229 366 379320 333 366 341584 333 724 355040 358 724 121724 133 358 155430 133 144 34224 144 663 63095 663 997 20...
output:
197964536523
result:
ok single line: '197964536523'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3820kb
input:
100 4950 36 54 83 54 94 1487 8 94 819 8 14 175 14 73 1291 4 73 3632 4 40 4326 40 44 3572 44 81 2602 81 93 4336 42 93 1976 42 50 4925 50 55 4018 55 72 4726 47 72 3765 23 47 808 23 59 2530 48 59 669 16 48 166 16 34 440 34 35 1203 35 37 1 37 41 201 41 76 3728 53 76 2455 53 96 2151 11 96 2529 11 24 1740...
output:
23182814
result:
ok single line: '23182814'
Test #4:
score: 0
Accepted
time: 2ms
memory: 5668kb
input:
5000 4999 3160 3947 889 3947 4144 1357 2583 4144 350 2583 4098 1153 1572 4098 3467 1572 3602 2268 3602 4348 1767 3910 4348 749 3910 4279 4411 3933 4279 3776 3933 4158 4733 1363 4158 439 1363 4696 1683 2118 4696 2900 1543 2118 3455 1543 1574 4333 1574 3871 4589 1367 3871 3107 764 1367 3356 764 1639 8...
output:
164191103
result:
ok single line: '164191103'
Test #5:
score: 0
Accepted
time: 98ms
memory: 15416kb
input:
1000 499500 110 193 186899 193 709 390656 492 709 109715 166 492 191058 166 373 404067 373 958 318461 57 958 381722 57 831 161143 527 831 383417 164 527 160207 164 797 449345 706 797 252032 212 706 339548 86 212 245712 86 644 371843 644 817 270186 194 817 110699 194 851 498434 378 851 178867 356 378...
output:
247800288368
result:
ok single line: '247800288368'
Test #6:
score: 0
Accepted
time: 9ms
memory: 5700kb
input:
1000 49950 284 977 46046 135 977 20521 63 135 38311 63 404 31468 288 404 19920 288 785 20057 623 785 12647 623 834 30924 94 834 7321 94 924 6385 311 924 14002 192 311 17744 192 209 13388 209 766 26499 113 766 35029 113 616 12993 277 616 3033 277 886 36474 72 886 36225 72 602 21127 344 602 13262 51 3...
output:
23246616282
result:
ok single line: '23246616282'
Test #7:
score: 0
Accepted
time: 0ms
memory: 5772kb
input:
1000 4995 467 480 3996 293 467 774 293 311 2235 311 805 2028 805 912 2316 616 912 1318 616 983 473 50 983 1742 50 478 2180 355 478 567 355 776 2765 615 776 4262 615 910 3411 235 910 4463 235 972 2970 238 972 4680 238 635 1942 553 635 791 553 856 924 272 856 447 233 272 2078 233 979 1102 296 979 973 ...
output:
1058421208
result:
ok single line: '1058421208'
Test #8:
score: 0
Accepted
time: 100ms
memory: 15588kb
input:
5000 500000 3142 3839 459682 1101 3142 6844 1101 1310 247849 1310 2786 210132 2786 3257 382129 260 3257 382929 260 896 398264 219 896 430495 219 456 498732 456 721 193037 721 2422 247661 1141 2422 281599 1141 4592 300222 2647 4592 393533 1770 2647 204105 1770 2915 341004 2915 3780 175653 2977 3780 1...
output:
5939594634342
result:
ok single line: '5939594634342'
Test #9:
score: 0
Accepted
time: 1ms
memory: 3912kb
input:
5000 5000 651 1484 1065 651 2754 1918 1642 2754 3854 230 1642 1309 230 1607 3482 87 1607 2637 87 4190 3111 4190 4973 1308 3428 4973 2279 2819 3428 1693 2588 2819 4939 643 2588 4310 643 5000 36 3076 5000 1877 3076 3941 2557 2894 3941 370 1691 2894 1346 1691 3629 2755 3556 3629 2986 3494 3556 373 3494...
output:
165772381
result:
ok single line: '165772381'
Test #10:
score: 0
Accepted
time: 3ms
memory: 5732kb
input:
5000 10000 1018 3487 2 2588 3487 2400 2588 3685 4601 2323 3685 4772 2169 2323 1755 42 2169 5236 42 3216 4609 3216 3377 1292 2849 3377 9846 2849 2997 3525 2678 2997 2785 1756 2678 5414 1756 3061 2853 3061 3395 1081 3395 4060 2023 944 4060 163 63 944 3 63 2806 5102 1469 2806 733 1229 1469 1537 1229 31...
output:
2754393913
result:
ok single line: '2754393913'
Test #11:
score: 0
Accepted
time: 1ms
memory: 5668kb
input:
5 7 1 2 6 1 3 10 1 4 12 2 4 8 2 5 3 3 4 4 4 5 2
output:
44
result:
ok single line: '44'
Test #12:
score: 0
Accepted
time: 0ms
memory: 3700kb
input:
10 9 2 3 5 2 4 2 4 6 3 6 9 9 1 9 4 1 5 6 5 10 7 8 10 8 7 8 1
output:
141
result:
ok single line: '141'
Test #13:
score: 0
Accepted
time: 1ms
memory: 5660kb
input:
10 10 1 3 1 3 4 10 2 4 9 2 7 4 7 8 7 8 10 2 6 10 3 6 9 5 5 9 8 2 10 6
output:
150
result:
ok single line: '150'
Test #14:
score: 0
Accepted
time: 0ms
memory: 5804kb
input:
10 20 1 9 19 6 9 9 5 6 8 5 10 5 8 10 18 3 8 11 2 3 14 2 7 4 4 7 16 5 8 6 5 9 13 2 9 10 2 10 1 1 2 7 1 5 15 6 10 12 2 4 2 6 8 3 1 8 20 1 4 17
output:
598
result:
ok single line: '598'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3716kb
input:
10 45 2 10 20 1 10 28 1 9 12 4 9 6 3 4 13 3 7 43 7 8 29 5 8 17 5 6 26 5 9 32 3 5 38 5 7 27 6 10 24 2 6 25 8 10 11 2 9 8 2 5 2 1 2 33 4 10 5 6 9 34 3 10 45 2 4 37 7 10 9 4 6 16 3 9 30 6 8 36 8 9 10 6 7 7 3 6 4 2 7 14 4 8 31 5 10 44 1 8 15 1 7 35 2 3 21 4 7 40 1 4 3 3 8 22 1 3 18 7 9 1 9 10 23 1 5 19 ...
output:
1632
result:
ok single line: '1632'
Test #16:
score: 0
Accepted
time: 0ms
memory: 3768kb
input:
5 5 2 5 1 1 2 2 2 3 4 1 3 5 2 4 6
output:
24
result:
ok single line: '24'
Test #17:
score: 0
Accepted
time: 3ms
memory: 4080kb
input:
10000 9999 744 8773 6661 744 3106 3225 901 3106 1716 901 1979 8175 1979 4101 190 4101 4637 3371 3546 4637 346 3546 7739 5460 1983 7739 8599 1983 2985 2457 2985 8584 5637 5406 8584 9597 5406 8829 295 7996 8829 8521 7996 8603 8012 6860 8603 4324 2739 6860 6629 2739 3473 4817 963 3473 5948 963 8589 729...
output:
769234978
result:
ok single line: '769234978'
Test #18:
score: 0
Accepted
time: 95ms
memory: 15616kb
input:
10000 500000 157 3262 192950 3262 9953 298268 7205 9953 397428 3804 7205 66914 3804 8019 271770 8019 9862 433756 143 9862 332527 143 3791 24066 3791 6129 27645 2911 6129 434448 2911 4700 152073 4700 5254 419045 5254 6677 44493 1432 6677 393993 1432 7957 63860 5622 7957 368475 5622 6114 380304 361 61...
output:
22085762261459
result:
ok single line: '22085762261459'
Test #19:
score: 0
Accepted
time: 1ms
memory: 3760kb
input:
1000 1000 265 851 465 4 265 145 4 77 461 20 77 927 20 454 152 406 454 124 406 784 436 784 911 315 402 911 16 30 402 530 30 770 434 770 995 410 838 995 803 838 917 619 460 917 535 460 785 205 48 785 845 48 224 787 224 999 166 343 999 893 113 343 310 113 867 812 563 867 580 563 692 814 503 692 368 503...
output:
5870690
result:
ok single line: '5870690'
Test #20:
score: 0
Accepted
time: 1ms
memory: 3824kb
input:
1000 5000 55 896 4573 55 570 2474 249 570 603 249 873 2655 384 873 546 338 384 2928 118 338 2903 118 206 2140 206 301 2204 301 447 2587 447 670 4439 670 920 949 403 920 1518 403 757 4479 507 757 799 507 871 400 854 871 5000 854 973 4893 713 973 3154 713 851 2343 57 851 3680 57 352 1561 352 578 1711 ...
output:
298353389
result:
ok single line: '298353389'
Test #21:
score: 0
Accepted
time: 0ms
memory: 5676kb
input:
1000 10000 242 519 2102 242 667 8176 11 667 2982 11 491 6814 360 491 7111 341 360 9951 341 600 159 172 600 9227 172 403 9066 236 403 1754 236 459 9542 276 459 335 276 682 2043 682 915 4074 859 915 4380 859 911 9688 186 911 6040 186 795 5170 62 795 4015 62 554 144 498 554 2730 498 525 3768 176 525 25...
output:
3189903198
result:
ok single line: '3189903198'
Test #22:
score: 0
Accepted
time: 19ms
memory: 7848kb
input:
1000 100000 211 891 43177 211 402 20361 58 402 14319 58 176 56211 176 593 88915 214 593 87300 214 574 90523 91 574 4494 91 935 58168 815 935 44727 662 815 2964 662 682 47777 332 682 22978 332 691 37212 691 956 2080 869 956 5083 841 869 48469 841 943 11697 186 943 96135 186 599 79437 6 599 32633 6 65...
output:
47914759610
result:
ok single line: '47914759610'
Test #23:
score: 0
Accepted
time: 54ms
memory: 11864kb
input:
1000 300000 454 906 84250 486 906 254863 486 622 59523 36 622 96350 36 137 123628 137 984 88519 75 984 62616 75 350 70020 350 447 254833 447 522 196949 522 693 140130 323 693 160820 323 925 258753 10 925 124608 10 699 116059 80 699 125162 80 445 112313 309 445 31053 309 375 164396 375 469 225915 70 ...
output:
148255794619
result:
ok single line: '148255794619'