标签:
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 46 Accepted Submission(s): 20
#include<bits/stdc++.h> using namespace std; const int maxq = 5e3+5; int Qry[maxq]; long long ans[maxq]; bool cmp(int a,int b) { return Qry[a] < Qry[b]; } int rk[maxq]; int n,m,q; const int maxm = 1e5+5,maxn = 2e4+5; struct Edge { int u,v,w; bool operator < (const Edge &rh) const { return w < rh.w; } void IN() { scanf("%d%d%d",&u,&v,&w); } }edges[maxm]; int pa[maxn],cnt[maxn]; int fdst(int x) { return x==pa[x]?x:pa[x]=fdst(pa[x]); } int main() { //freopen("in.txt","r",stdin); int T; scanf("%d",&T); while(T--){ scanf("%d%d%d",&n,&m,&q); for(int i = 0; i < m ;i++){ edges[i].IN(); } for(int i = 0; i < q; i++){ rk[i] = i; scanf("%I64d",Qry+i); } sort(rk,rk+q,cmp); sort(edges,edges+m); for(int i = 1; i <= n; i++) pa[i] = i,cnt[i] = 1; long long cur = 0; for(int i = 0,j = 0; i < q; i++){ int id = rk[i]; int cq = Qry[id]; while(j < m && edges[j].w <= cq){ int u = fdst(edges[j].u),v = fdst(edges[j].v); if(u != v){ pa[u] = v; cur += 2LL*cnt[u]*cnt[v]; cnt[v] += cnt[u]; } j++; } ans[id] = cur; } for(int i = 0; i < q; i++){ printf("%I64d\n",ans[i]); } } return 0; }
2015 ACM/ICPC Asia Regional Changchun Online Pro 1005 Travel
标签:
原文地址:http://www.cnblogs.com/jerryRey/p/4804928.html