标签:
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2404 Accepted Submission(s): 842
1
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000
10000
13000
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 #define Max 20000+5 6 using namespace std; 7 struct edge 8 { 9 int s,e,val; 10 }a[100000+5]; 11 struct ques 12 { 13 int d,id,res; 14 }p[5005]; 15 int per[Max],num[Max]; 16 int n,m,q; 17 bool cmp(ques a,ques b) 18 { 19 return a.d<=b.d; 20 } 21 bool cmp1(ques a,ques b) 22 { 23 return a.id<b.id; 24 } 25 void init() 26 { 27 for(int i=0;i<=n;i++) 28 { 29 per[i]=i; 30 num[i]=1; 31 } 32 for(int i=0;i<=q;i++) 33 p[i].res=0; 34 } 35 int find(int x) 36 { 37 if(x==per[x]) 38 return x; 39 return per[x]=find(per[x]); 40 } 41 int unite(int a,int b) 42 { 43 a=find(a); 44 b=find(b); 45 if(a<b) //a最大 46 swap(a,b); 47 per[a]=b; //根节点下标最小,记录树的节点个数 48 num[b]+=num[a]; 49 return 0; 50 } 51 bool cmp2(edge a,edge b) 52 { 53 return a.val<b.val; 54 } 55 int main() 56 { 57 int T; 58 int i,j; 59 freopen("in.txt","r",stdin); 60 scanf("%d",&T); 61 while(T--) 62 { 63 scanf("%d%d%d",&n,&m,&q); 64 init(); 65 for(i=0;i<m;i++) 66 scanf("%d%d%d",&a[i].s,&a[i].e,&a[i].val); 67 for(i=0;i<q;i++) 68 { 69 scanf("%d",&p[i].d); 70 p[i].id=i; 71 } 72 sort(a,a+m,cmp2); 73 sort(p,p+q,cmp); 74 j=0; 75 int t1,t2,ans=0; 76 for(i=0;i<q;i++) 77 { 78 while(j<m&&a[j].val<=p[i].d) 79 { 80 t1=find(a[j].s); 81 t2=find(a[j].e); 82 j++; 83 if(t1!=t2) 84 { 85 ans+=2*num[t1]*num[t2]; 86 unite(t1,t2); 87 } 88 } 89 p[i].res=ans; 90 } 91 sort(p,p+q,cmp1); 92 for(i=0;i<q;i++) 93 printf("%d\n",p[i].res); 94 } 95 }
Travel(HDU 5441 2015长春区域赛 带权并查集)
标签:
原文地址:http://www.cnblogs.com/a1225234/p/5182048.html