标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2611 Accepted Submission(s): 978
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> using namespace std; int n,k,fa[100010],mark[100010]; struct node { int x,y,z; }e[100010]; bool cmp(node a,node b) { return a.z>b.z; } int find(int x) { if(x!=fa[x]) fa[x]=find(fa[x]); return fa[x]; } int main() { int tt,x; scanf("%d",&tt); while(tt--) { long long ans=0; scanf("%d%d",&n,&k); memset(mark,0,sizeof(mark)); for(int i=0;i<n;i++) fa[i]=i; for(int i=0;i<n-1;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z),ans+=e[i].z; sort(e,e+n-1,cmp); while(k--) { scanf("%d",&x); mark[x]=1; } for(int i=0;i<n-1;i++) { int fx,fy; fx=find(e[i].x); fy=find(e[i].y); if(mark[fx]+mark[fy]<=1) { if(fx!=fy) { fa[fx]=fy; mark[fy]+=mark[fx]; } ans-=e[i].z; } } printf("%I64d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4423573.html