标签:
Sample Input
1
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000
10000
13000
Sample Output
2
6
12
#include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; const int oo = 0x3f3f3f3f; const int N = 200010; const int M = 100100; typedef long long LL; int father[N], num[N], QQ[N], n, m; /**< num[i]表示i这个根节点 所包含的节点个数*/ struct da { int u, v, w; bool operator < (const da &a)const { return w < a.w; } }as[N]; struct da1 { int id, x; bool operator < (const da1 &a)const { return x < a.x; } }ac[N]; int fin(int x) { if(x != father[x]) father[x] = fin(father[x]); return father[x]; } void init() { for(int i = 0; i <= n; i++) { father[i] = i; num[i] = 1; } memset(QQ, 0, sizeof(QQ)); } int main() { int T, i, j, k; scanf("%d", &T); while(T--) { scanf("%d %d %d", &n, &m, &k); init(); for(i = 0; i < m; i++) scanf("%d %d %d", &as[i].u, &as[i].v, &as[i].w); sort(as, as+m); for(i = 0; i < k; i++) { scanf("%d", &ac[i].x); ac[i].id = i; } sort(ac, ac+k); int ans=0;j = 0; for(i = 0; i < k; i++) { while(j < m && as[j].w <= ac[i].x) { int x = fin(as[j].u); int y = fin(as[j].v); j++; if(x != y) { father[x] = y; ans += num[y]*num[x];/**< 集合合并 */ num[y] += num[x]; } } QQ[ac[i].id] = 2*ans; } for(i = 0; i < k; i++) printf("%d\n", QQ[i]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/PersistFaith/p/4811585.html