标签:ssi 大连 pre test help 8 8 连通图 stream point
http://acm.hdu.edu.cn/showproblem.php?pid=3938
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1921 Accepted Submission(s): 955
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 struct edge{ 7 int to1; 8 int to2; 9 int lrn; 10 }EDGE[50005]; 11 struct qury{ 12 int id; 13 int x; 14 }qwq[10005]; 15 int n,m,q; 16 int pre[10005],num[10005];; 17 int find(int x) 18 { 19 20 int xx=x; 21 while(x!=pre[x]) 22 { 23 x=pre[x]; 24 } 25 while(pre[xx]!=x) 26 { 27 int t=pre[xx]; 28 pre[xx]=x; 29 xx=t; 30 } 31 return x; 32 } 33 bool cmp1(struct edge orz1,struct edge orz2) 34 { 35 return orz1.lrn<orz2.lrn; 36 } 37 bool cmp2(struct qury orz3,struct qury orz4) 38 { 39 return orz3.x<orz4.x; 40 } 41 int main() 42 { 43 while(scanf("%d%d%d",&n,&m,&q)==3) 44 { 45 for(int i = 1 ; i <= n ; i++) 46 { 47 pre[i]=i; 48 num[i]=1; 49 } 50 for(int i = 0 ; i < m ; i++) 51 { 52 scanf("%d%d%d",&EDGE[i].to1,&EDGE[i].to2,&EDGE[i].lrn); 53 } 54 sort(EDGE,EDGE+m,cmp1); 55 for(int i = 0; i < q ; i++) 56 { 57 qwq[i].id=i; 58 scanf("%d",&qwq[i].x); 59 } 60 sort(qwq,qwq+q,cmp2); 61 int j=0; 62 long long sum=0; 63 long long ans[10005]; 64 memset(ans,0,sizeof(ans)); 65 for(int i = 0 ; i < q ; i++) 66 { 67 for(;j<m;j++) 68 { 69 if(EDGE[j].lrn>qwq[i].x)break; 70 int wqw1=find(EDGE[j].to1); 71 int wqw2=find(EDGE[j].to2); 72 if(wqw1!=wqw2) 73 { 74 pre[wqw1]=wqw2; 75 sum+=num[wqw1]*num[wqw2]; 76 num[wqw2]+=num[wqw1]; 77 } 78 } 79 ans[qwq[i].id]=sum; 80 81 } 82 for(int i = 0 ;i < q ; i++) 83 { 84 printf("%lld\n",ans[i]); 85 } 86 } 87 return 0; 88 }
标签:ssi 大连 pre test help 8 8 连通图 stream point
原文地址:https://www.cnblogs.com/MekakuCityActor/p/9015623.html