hdu 3938 离线+并查集

Portal

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 657    Accepted Submission(s): 332

Problem Description
ZLGG found a magic theory that the bigger banana the bigger banana peel .This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals will cost min{T} energies. T in a path between point V and point U is the length of the longest edge in the path. There may be lots of paths between two points. Now ZLGG owned L energies and he want to know how many kind of path he could make.
 

 

Input
There are multiple test cases. The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of points, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c ≤ 10^8) describing an edge connecting the point a and b with cost c. Each of the following Q lines contain a single integer L (0 ≤ L ≤ 10^8).
 

 

Output
Output the answer to each query on a separate line.
 

 

Sample Input
10 10 10 7 2 1 6 8 3 4 5 8 5 8 2 2 8 9 6 4 5 2 1 5 8 10 5 7 3 7 7 8 8 10 6 1 5 9 1 8 2 7 6
 
Sample Output
36 13 1 13 36 1 36 2 16 13
 
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
struct node
{
        int u,v,d;
}a[100010];
bool cmp1(node a,node b)
{
        return a.d<b.d;
}
struct NODE
{
        int id,l;
}b[100000];
bool cmp2(NODE a,NODE b)
{
        return a.l<b.l;
}
int fa[10010],num[10010],out[100000];
int find(int x)
{
        if(fa[x]!=x)fa[x]=find(fa[x]);
        return fa[x];
}
int bin(int x,int y)
{
    int px=find(x);
    int py=find(y),t=0;
    if(px!=py)
    {
        fa[py]=px;
        t=num[px]*num[py];
        num[px]+=num[py];
    }
    return t;
}

int main()
{
        int i,j,k,m,n,p,q;
        while(~scanf("%d%d%d",&n,&m,&q))
        {
                for(i=0;i<=n;i++)
                {
                        fa[i]=i;
                        num[i]=1;
                }
                for(i=0;i<m;i++)scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].d);
                for(i=0;i<q;i++)scanf("%d",&b[i].l),b[i].id=i;
                sort(a,a+m,cmp1);
                sort(b,b+q,cmp2);
                j=0;
                int ans=0;
                for(i=0;i<q;i++)
                {
                        while(j<m&&a[j].d<=b[i].l)
                        {
                                ans+=bin(a[j].u,a[j].v);
                                j++;
                        }
                        out[b[i].id]=ans;
                }
                for(i=0;i<q;i++)printf("%d\n",out[i]);
        }
        return 0;
}
View Code

 

posted @ 2013-09-21 19:20  线性无关  阅读(131)  评论(0)    收藏  举报