hdu Portal 离线+并查集★★



 Portal

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


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
/**
题意 给出 n个点 m 条边 询问Q次

每次给出一个价值问有在这个价值范围内能走那条路径

只要是经过的边的和的花费小于 cost 就可以了

对边按照价值排序

每次能走过一个边就将这条边的两个点合并 且一个点num[x](存储这个连通分量包含的点数)+= num[y]

当有价值高的边再经过这个点的时候 a b  ans += (num【Find(a)】*num【Find(b)】)

**/
#include<bits/stdc++.h>
using namespace std;

struct Side
{
    int x,y,cost;
    bool operator < (const Side &a)const
    {
        return cost<a.cost;
    }
} a[100003];
long long output[100003]={0};
int pre[100003];
int num[100003];///包含点的个数
struct Quick
{
    int id,cost;
    bool operator < (const Quick &a)const
    {
        return cost<a.cost;
    }
} q[100003];

void init()
{
    for(int i=0; i<100003; i++)
    {
        pre[i]=i;
        num[i]=1;
    }
}

int Find(int x)
{
    if(pre[x]!=x)
        return pre[x]=Find(pre[x]);
    return x;
}
int Merge(int x,int y)
{
    int X=Find(x);
    int Y=Find(y);
    if(X!=Y)
    {
        int Tmp=num[X]*num[Y];
        num[X]+=num[Y];
        pre[Y]=X;
        num[Y]=0;
        return Tmp;
    }
    return 0;
}
int main()
{
    int n,m,Q;
    while(~scanf("%d%d%d",&n,&m,&Q))
    {
        init();
        for(int i=1; i<=m; i++)
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].cost);
        sort(a+1,a+1+m);
        for(int i=1; i<=Q; i++)
        {
            scanf("%d",&q[i].cost);
            q[i].id=i;
        }
        sort(q+1,q+1+Q);
        long long Tmp=0;
        int pos=1;
        for(int i=1;i<=Q;i++)
        {
            while(pos<=m&&a[pos].cost<=q[i].cost)
            {
                Tmp+=Merge(a[pos].x,a[pos].y);
                pos++;
            }
            output[q[i].id]=Tmp;
        }
        for(int i=1;i<=Q;i++)
            printf("%lld\n",output[i]);
    }
}


Portal

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


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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值