#include <stdio.h>
#include <algorithm>
#include <vector>
#include <functional>
#include <set>
#include <map>
#include <queue>
#include <tuple>
#include <string.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
typedef long long ll;
typedef pair<int, int> pi;
const int MAX_N = 1e5 + 100;
struct ED {
int x, y, c, ix;
ED() {}
ED(int x_, int y_, int c_, int i_) : x(x_), y(y_), c(c_), ix(i_) {}
};
int N, M;
vector<ED> all;
vector<ED> Ed[MAX_N];
int Chk[MAX_N], UNF[MAX_N];
int F(int v) { return UNF[v]==v?v:UNF[v]=F(UNF[v]); }
bool U(int a, int b) {
a = F(a), b = F(b);
UNF[a] = b;
return a != b; //if union well;
}
int main() {
scanf("%d%d", &N, &M);
while(M--) {
int x, y, c; scanf("%d%d%d", &x, &y, &c);
Ed[x].emplace_back(x, y, c, M);
Ed[y].emplace_back(x, y, c, M);
all.emplace_back(x, y, c, M);
}
sort(all.begin(), all.end(), [&](const ED &a, const ED &b) {return a.c < b.c;});
for(int s=1; s<=N; s++) {
for(int i=1; i<=N; i++) UNF[i] = i;
ll ans = 0;
for(ED &e : Ed[s]) {
Chk[e.ix] = s;
U(e.x, e.y); ans += e.c;
}
for(ED &e : all) {
if(Chk[e.ix] == s) continue;
if(U(e.x, e.y)) ans += e.c;
}
printf("%lld\n", ans);
}
return 0;
}
Compilation message
LM.cpp: In function 'int main()':
LM.cpp:37:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
^
LM.cpp:39:43: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x, y, c; scanf("%d%d%d", &x, &y, &c);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
4600 KB |
Output is correct |
2 |
Correct |
76 ms |
4596 KB |
Output is correct |
3 |
Correct |
79 ms |
4600 KB |
Output is correct |
4 |
Correct |
79 ms |
4604 KB |
Output is correct |
5 |
Correct |
83 ms |
4600 KB |
Output is correct |
6 |
Correct |
79 ms |
4592 KB |
Output is correct |
7 |
Correct |
76 ms |
4596 KB |
Output is correct |
8 |
Correct |
79 ms |
4588 KB |
Output is correct |
9 |
Correct |
79 ms |
4592 KB |
Output is correct |
10 |
Correct |
76 ms |
4600 KB |
Output is correct |
11 |
Correct |
79 ms |
4592 KB |
Output is correct |
12 |
Correct |
83 ms |
4588 KB |
Output is correct |
13 |
Correct |
93 ms |
4584 KB |
Output is correct |
14 |
Correct |
93 ms |
4588 KB |
Output is correct |
15 |
Correct |
83 ms |
4584 KB |
Output is correct |
16 |
Correct |
39 ms |
4580 KB |
Output is correct |
17 |
Correct |
43 ms |
4580 KB |
Output is correct |
18 |
Correct |
39 ms |
4588 KB |
Output is correct |
19 |
Correct |
43 ms |
4580 KB |
Output is correct |
20 |
Correct |
39 ms |
4588 KB |
Output is correct |
21 |
Correct |
39 ms |
4584 KB |
Output is correct |
22 |
Correct |
43 ms |
4576 KB |
Output is correct |
23 |
Correct |
49 ms |
4576 KB |
Output is correct |
24 |
Correct |
46 ms |
4576 KB |
Output is correct |
25 |
Correct |
0 ms |
4304 KB |
Output is correct |
26 |
Correct |
0 ms |
4304 KB |
Output is correct |
27 |
Correct |
0 ms |
4304 KB |
Output is correct |
28 |
Correct |
13 ms |
4436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2000 ms |
30364 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |