제출 #6620

#제출 시각아이디문제언어결과실행 시간메모리
6620mrcamel비용 (KOI11_cost)C++98
24 / 24
148 ms4412 KiB
#include <iostream> #include <algorithm> using namespace std; #define in cin #define out cout #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]; //합 const int64 DIV = 1000000000; 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 % DIV) * (Vtx[p2].subN % DIV) * (sum[i] % DIV); Vtx[p1].subN += Vtx[p2].subN; Vtx[p2].parent = p1; } } out << (res % DIV) << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...