This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
typedef long long lint;
const int MAXN = 1005;
struct disj{
int pa[MAXN], sz[MAXN];
void init(int n){
for(int i=0; i<=n; i++){
pa[i] = i;
sz[i] = 1;
}
}
int find(int x){
return pa[x] = (pa[x] == x ? x : find(pa[x]));
}
bool uni(int p, int q){
p = find(p);
q = find(q);
if(p == q) return 0;
pa[q] = p;
sz[p] += sz[q];
return 1;
}
}disj;
struct edg{int s, e, x;}e[3005];
int n, m;
int main(){
cin >> n >> m;
for(int i=0; i<m; i++){
cin >> e[i].s >> e[i].e >> e[i].x;
}
sort(e, e+m, [&](const edg &a, const edg &b){
return a.x < b.x;
});
for(int i=1; i<=n; i++){
int ans = 0;
disj.init(n);
for(int j=0; j<m; j++){
if(e[j].s == i || e[j].e == i){
if(disj.uni(e[j].s, e[j].e)){
ans += e[j].x;
}
}
}
for(int j=0; j<m; j++){
if(disj.uni(e[j].s, e[j].e)) ans += e[j].x;
}
cout << ans << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |