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 <iostream>
#include <algorithm>
using namespace std;
#define in cin
#define out cout
#define DIV 1000000000
#define int64 long long int
struct LL
{
int s, e, w;
}L[100001];
struct VV
{
int subN;
int parent;
}Vtx[100001];
int cmp(const LL &a, const LL &b)
{
return a.w > b.w;
}
int find(int idx)
{
if(idx != Vtx[idx].parent) Vtx[idx].parent = find(Vtx[idx].parent);
return Vtx[idx].parent;
}
int64 sum[100001]; //합
int main()
{
int n, k;
in >> n >> k; //정점, 간선
for(int i=0; i<k; i++)
in >> L[i].s >> L[i].e >> L[i].w;
sort(L, L+k, cmp);
for(int i=1; i<=n; i++)
{
Vtx[i].parent = i;
Vtx[i].subN = 1;
}
for(int i=0; i<k; i++) {
if(i != 0) sum[i] = sum[i-1] + L[k-i-1].w;
else sum[i] = L[k-1].w;
sum[i] %= DIV;
} reverse(sum, sum+k);
int64 res = 0;
for(int i=0; i<k; i++)
{
int v1 = L[i].s;
int v2 = L[i].e;
int p1 = find(v1);
int p2 = find(v2);
if(p1 != p2)
{
res += (Vtx[p1].subN * Vtx[p2].subN * sum[i]);
res %= DIV;
Vtx[p1].subN += Vtx[p2].subN;
Vtx[p2].parent = p1;
}
}
out << res << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |