标签:
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Description
Input
Output
Sample Input
Sample Output
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 20010; 4 using LL = long long; 5 int uf[maxn]; 6 LL ret,ans[maxn],cnt[maxn]; 7 struct arc { 8 int u,v,w; 9 bool operator<(const arc &rhs) const { 10 return w < rhs.w; 11 } 12 } e[500010]; 13 struct QU { 14 int w,id; 15 bool operator<(const QU &rhs) const{ 16 return w < rhs.w; 17 } 18 } Q[maxn]; 19 int Find(int x) { 20 if(x != uf[x]) uf[x] = Find(uf[x]); 21 return uf[x]; 22 } 23 bool Union(int x,int y) { 24 x = Find(x); 25 y = Find(y); 26 if(x == y) return false; 27 ret -= cnt[x]*(cnt[x] - 1) + cnt[y]*(cnt[y] - 1); 28 ret += (cnt[x] + cnt[y])*(cnt[x] + cnt[y] - 1); 29 if(cnt[x] < cnt[y]) { 30 uf[x] = y; 31 cnt[y] += cnt[x]; 32 cnt[x] = 0; 33 } else { 34 uf[y] = x; 35 cnt[x] += cnt[y]; 36 cnt[y] = 0; 37 } 38 return true; 39 } 40 int main() { 41 int kase,n,m,q; 42 scanf("%d",&kase); 43 while(kase--) { 44 scanf("%d%d%d",&n,&m,&q); 45 for(int i = 0; i < m; ++i) 46 scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); 47 for(int i = 0; i < q; ++i) { 48 scanf("%d",&Q[i].w); 49 Q[i].id = i; 50 } 51 sort(e,e + m); 52 sort(Q,Q + q); 53 for(int i = 0; i <= n; ++i) { 54 uf[i] = i; 55 cnt[i] = 1; 56 } 57 ret = 0; 58 for(int i = 0,j = 0; i < q; ++i) { 59 while(j < m && e[j].w <= Q[i].w) { 60 Union(e[j].u,e[j].v); 61 ++j; 62 } 63 ans[Q[i].id] = ret; 64 } 65 for(int i = 0; i < q; ++i) 66 printf("%I64d\n",ans[i]); 67 } 68 return 0; 69 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4964003.html