Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 931 Accepted Submission(s): 466
1 #define LOCAL 2 #include<cstdio> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<cstdlib> 7 using namespace std; 8 const int maxn=10005; 9 /*for point*/ 10 struct node{ 11 int a,b,c; 12 bool operator <(const node &bn)const { 13 return c<bn.c; 14 } 15 }sac[maxn*5]; 16 /*for query*/ 17 struct query{ 18 int id,val; 19 bool operator <(const query &bn)const { 20 return val<bn.val; 21 } 22 }qq[maxn]; 23 24 int father[maxn],rank[maxn]; 25 int key[maxn]; 26 27 void init(int n){ 28 for(int i=0;i<=n;i++){ 29 father[i]=i; 30 rank[i]=1; 31 } 32 } 33 34 int fin(int x){ 35 while(x!=father[x]) 36 x=father[x]; 37 return x; 38 } 39 40 int unin(int x,int y) 41 { 42 x=fin(x); 43 y=fin(y); 44 int ans=0; 45 if(x!=y){ 46 ans=rank[x]*rank[y]; 47 if(rank[x]<rank[y]){ 48 rank[y]+=rank[x]; 49 father[x]=y; 50 } 51 else{ 52 rank[x]+=rank[y]; 53 father[y]=x; 54 } 55 } 56 return ans; 57 } 58 59 int main() 60 { 61 #ifdef LOCAL 62 freopen("test.in","r",stdin); 63 freopen("test1.out","w",stdout); 64 #endif 65 int n,m,q; 66 while(scanf("%d%d%d",&n,&m,&q)!=EOF) 67 { 68 init(n); 69 for(int i=0;i<m;i++){ 70 scanf("%d%d%d",&sac[i].a,&sac[i].b,&sac[i].c); 71 } 72 73 for(int i=0;i<q;i++){ 74 scanf("%d",&qq[i].val); 75 qq[i].id=i; 76 } 77 sort(sac,sac+m); 78 sort(qq,qq+q); 79 int i,j,res=0; 80 for(j=0,i=0;i<q;i++){ 81 while(j<m&&sac[j].c<=qq[i].val){ 82 res+=unin(sac[j].a,sac[j].b); 83 ++j; 84 } 85 key[qq[i].id]=res; 86 } 87 for(i=0;i<q;i++){ 88 printf("%d\n",key[i]); 89 } 90 } 91 // system("comp"); 92 93 return 0; 94 }