标签:
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 94 Accepted Submission(s): 32
/* *********************************************** Author :pk28 Created Time :2015/9/13 15:57:19 File Name :4.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 100000+10 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; struct node{ int x,y,z; }edge[maxn]; bool cmp(node a,node b){ return a.z<b.z; } struct pk{ int w,id; }q[maxn]; bool cmp1(pk a,pk b){ return a.w<b.w; } int ans[maxn]; int fa[maxn],num[maxn]; int findfa(int x){ if(x==fa[x])return fa[x]; else return fa[x]=findfa(fa[x]); } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int n,m,Q,t; cin>>t; while(t--){ cin>>n>>m>>Q; for(int i=0;i<m;i++)scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].z); for(int i=0;i<Q;i++)scanf("%d",&q[i].w),q[i].id=i; sort(edge,edge+m,cmp); sort(q,q+Q,cmp1); for(int i=1;i<=n;i++)fa[i]=i,num[i]=1; int cnt=0,j=0; for(int i=0;i<Q;i++){ while(j<m&&edge[j].z<=q[i].w){ int x=findfa(edge[j].x); int y=findfa(edge[j].y); if(x!=y){ cnt+=num[x]*num[y]; fa[x]=y; num[y]+=num[x]; } j++; } ans[q[i].id]=2*cnt; } for(int i=0;i<Q;i++){ printf("%d\n",ans[i]); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/pk28/p/4805157.html