ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#194206 | #3415. 生成花朵 | fghking | 100 | 4468ms | 12932kb | C++ | 1.2kb | 2023-10-15 09:52:51 | 2023-10-15 12:20:00 |
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, m, k, fa[N];
int dian[N], bian[N];
long long ans = 0;
struct ren {
int u, v, w;
} a[N];
bool cmp(ren n1, ren n2) {
return n1.w < n2.w;
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= m; i++) {
cin >> a[i].u >> a[i].v >> a[i].w;
dian[i] = 1;
}
for (int i = 1; i <= n; i++) {
fa[i] = i;
}
sort(a + 1, a + 1 + m, cmp);
for (int i = m; i >= 1; i--) {
int fx = find(a[i].u), fy = find(a[i].v);
if (fx != fy) {
if (bian[fx] > bian[fy]) {
int t = fy;
fy = fx;
fx = t;
}
if (dian[fx] + dian[fy] - bian[fy] - bian[fx] - 1 >= k) {
fa[fx] = fy;
dian[fy] += dian[fx];
bian[fy] += bian[fx] + 1;
ans += a[i].w;
// cout << fy << " " << dian[fy] << " " << bian[fy] << endl;
}
} else {
if (bian[fx] > bian[fy]) {
int t = fy;
fy = fx;
fx = t;
}
if (dian[fy] - bian[fy] - 1 >= k) {
bian[fy]++;
ans += a[i].w;
// cout << fy << " " << dian[fy] << " " << bian[fy] << endl;
}
}
}
cout << ans << endl;
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 0ms
memory: 1228kb
input:
15 20 1 6 7 963580 12 5 716228 4 1 403343 14 3 457011 7 1 436546 7 2 5466 15 4 475864 2 1 336271 10 ...
output:
8081145
result:
ok single line: '8081145'
Test #2:
score: 10
Accepted
time: 0ms
memory: 1228kb
input:
20 20 0 20 4 432617 4 3 629590 10 16 943103 19 18 497537 10 20 416253 6 16 115921 19 10 28488 11 15 ...
output:
9008387
result:
ok single line: '9008387'
Test #3:
score: 10
Accepted
time: 559ms
memory: 12932kb
input:
500000 500000 1 444008 222356 414783 222356 51288 425686 135088 494686 95472 51288 444008 160817 512...
output:
208774404701
result:
ok single line: '208774404701'
Test #4:
score: 10
Accepted
time: 538ms
memory: 12856kb
input:
490000 500000 1 96647 180570 271064 36008 360162 653861 270823 314031 502346 360162 317315 931956 48...
output:
207785443393
result:
ok single line: '207785443393'
Test #5:
score: 10
Accepted
time: 459ms
memory: 12916kb
input:
500000 500000 0 108208 98102 1 202388 305044 1 379342 59163 1 379342 202388 1 363727 379342 1 202388...
output:
381245
result:
ok single line: '381245'
Test #6:
score: 10
Accepted
time: 433ms
memory: 12896kb
input:
496666 500000 0 197894 345096 1 94268 107852 1 94268 197894 1 370883 197894 1 345096 370883 1 94268 ...
output:
376663
result:
ok single line: '376663'
Test #7:
score: 10
Accepted
time: 574ms
memory: 12924kb
input:
500000 500000 0 7736 74162 726251 345417 74162 913705 74162 170609 539950 204696 7736 414420 7736 34...
output:
212883906779
result:
ok single line: '212883906779'
Test #8:
score: 10
Accepted
time: 577ms
memory: 12152kb
input:
400000 500000 0 22826 70883 154409 203647 70883 812518 317370 59895 163272 203647 22826 283183 31737...
output:
204726430556
result:
ok single line: '204726430556'
Test #9:
score: 10
Accepted
time: 543ms
memory: 11372kb
input:
300000 500000 0 227622 219467 110779 205687 9592 959582 113992 227622 988579 219467 113992 606597 21...
output:
186656418583
result:
ok single line: '186656418583'
Test #10:
score: 10
Accepted
time: 785ms
memory: 11760kb
input:
350000 500000 0 252737 211924 582239 300699 14041 390756 300699 220584 650566 273799 14041 277337 67...
output:
198608090311
result:
ok single line: '198608090311'
Extra Test:
score: 0
Extra Test Passed