ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#214301 | #3852. 最小生成树 | Anthonyyan | 30 | 45ms | 6936kb | C++ | 2.3kb | 2024-11-17 11:30:24 | 2024-11-17 13:06:04 |
answer
#include <bits/stdc++.h>
#define int long long
#define lowbit(x) ((x) & (-x))
#define rep(i, s, t, st) for (int i = s, __##i = t; i <= __##i; i += st)
#define per(i, s, t, st) for (int i = s, __##i = t; i >= __##i; i -= st)
#define gc getchar
#define pll pair<int, int>
#define clr(pst, siz) memset(pst, 0, sizeof(int) * siz)
#define cpy(pst, tar, siz) memcpy(pst, tar, sizeof(int) * siz)
inline int read()
{
int x = 0, f = 1, ch = gc();
while (!isdigit(ch))
{
if (ch == '-')
f = -f;
ch = gc();
}
while (isdigit(ch))
{
x = (x << 1) + (x << 3) + ch - '0';
ch = gc();
}
return x * f;
}
inline void write(int x)
{
if (x < 0)
putchar('-'), x = -x;
if (x / 10)
write(x / 10);
putchar('0' + x % 10);
}
struct Edge
{
int u, v, weight;
bool operator<(const Edge &e) const
{
return weight < e.weight;
}
};
class UnionFind
{
public:
int parent[1010], rank[1010];
UnionFind(int n)
{
rep(i, 0, n - 1, 1)
{
parent[i] = i;
rank[i] = 0;
}
}
int find(int x)
{
if (x != parent[x])
parent[x] = find(parent[x]);
return parent[x];
}
bool unite(int x, int y)
{
int rootX = find(x);
int rootY = find(y);
if (rootX != rootY)
{
if (rank[rootX] > rank[rootY])
parent[rootY] = rootX;
else if (rank[rootX] < rank[rootY])
parent[rootX] = rootY;
else
{
parent[rootY] = rootX;
rank[rootX]++;
}
return true;
}
return false;
}
};
int a[1010];
Edge e[1000010];
signed main()
{
#ifndef ONLINE_JUDGE
freopen("mst.in", "r", stdin);
freopen("mst.out", "w", stdout);
#endif
int n = read();
rep(i, 0, n - 1, 1) a[i] = read();
int cnt = 0;
rep(i, 0, n - 1, 1)
rep(j, i + 1, n - 1, 1) if (a[i] > a[j])
e[cnt++] = {i, j, a[i] - a[j]};
std::sort(e, e + cnt);
UnionFind uf(n);
int mst_weight_sum = 0;
int selected_edges = 0;
for (int i = 0; i < cnt; ++i)
{
const Edge &edge = e[i];
if (uf.unite(edge.u, edge.v))
{
mst_weight_sum += edge.weight;
selected_edges++;
if (selected_edges == n - 1)
break;
}
}
write(mst_weight_sum);
putchar('\n');
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 17ms
memory: 6804kb
input:
998 659 323 324 144 530 13 857 736 881 855 393 72 149 462 387 255 304 4 92 574 232 391 300 464 686 5...
output:
1801
result:
ok "1801"
Test #2:
score: 10
Accepted
time: 17ms
memory: 6936kb
input:
997 246 134 37 17 635 794 921 370 559 660 333 488 216 897 593 313 572 888 809 581 363 32 929 372 805...
output:
1824
result:
ok "1824"
Test #3:
score: 10
Accepted
time: 11ms
memory: 6820kb
input:
999 171 504 913 186 852 6 138 267 105 725 460 829 514 595 805 663 295 730 873 349 126 924 329 43 571...
output:
1838
result:
ok "1838"
Test #4:
score: 0
Runtime Error
input:
9996 5565 6310 3171 5034 3009 4627 1230 5305 4286 2812 4097 1669 3804 6661 9685 4031 2404 7681 4483 ...
output:
result:
Test #5:
score: 0
Runtime Error
input:
10000 2704 1827 9652 8229 8227 6104 349 3655 660 4045 9328 5512 8584 3254 9143 4556 2716 3574 5858 2...
output:
result:
Test #6:
score: 0
Runtime Error
input:
10000 23950783 781965025 16565420 291572110 128841529 363438218 359663422 727333852 367524122 226010...
output:
result:
Test #7:
score: 0
Runtime Error
input:
9999 452311202 701672009 791523862 178026433 144716116 184608716 59592754 30267781 818635776 8222542...
output:
result:
Test #8:
score: 0
Runtime Error
input:
300000 270756303 34017269 277560979 609142340 79149252 153098611 740386564 339460652 18451431 900438...
output:
result:
Test #9:
score: 0
Runtime Error
input:
299997 455780537 859019556 200709560 794471117 332485096 205293298 187001196 716562830 121596348 390...
output:
result:
Test #10:
score: 0
Runtime Error
input:
299996 51219018 155510262 688466426 766639015 395742109 862028671 109658585 526973 462033724 7922265...