HDU 5441.Travel【2015 ACM/ICPC Asia Regional Changchun Online】【并查集】9月18

Travel

Problem Description
Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are  n  cities and  m  bidirectional roads connecting the cities. Jack hates waiting too long on the bus, but he can rest at every city. Jack can only stand staying on the bus for a limited time and will go berserk after that. Assuming you know the time it takes to go from one city to another and that the time Jack can stand staying on a bus is  x  minutes, how many pairs of city  (a,b)  are there that Jack can travel from city  a  to  b  without going berserk?
 

Input
The first line contains one integer  T,T5 , which represents the number of test case.

For each test case, the first line consists of three integers  n,m  and  q  where  n20000,m100000,q5000 . The Undirected Kingdom has  n  cities and  m bidirectional roads, and there are  q  queries.

Each of the following  m  lines consists of three integers  a,b  and  d  where  a,b{1,...,n}  and  d100000 . It takes Jack  d  minutes to travel from city  a  to city  b and vice versa.

Then  q  lines follow. Each of them is a query consisting of an integer  x  where  x  is the time limit before Jack goes berserk.

 

Output
You should print  q  lines for each test case. Each of them contains one integer as the number of pair of cities  (a,b)  which Jack may travel from  a  to  b  within the time limit  x .

Note that  (a,b)  and  (b,a)  are counted as different pairs and  a  and  b  must be different cities.
 

Sample Input
   
   
   
   
1 5 5 3 2 3 6334 1 5 15724 3 5 5705 4 3 12382 1 3 21726 6000 10000 13000
 

Sample Output
   
   
   
   
2 6 12
 
给定n个节点,m个连通,q和询问。问不超过d的节点有多少两两连通。数据大了,所以说搜索是不行的。并查集,代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int node[20010],ans[5005],num[20010];//num数组记录根节点为i的连通块的节点个数
struct ss{
    int fr,to,len;
}c[100010];
bool cmp1(ss x,ss y){
    if(x.len<y.len) return true;
    else return false;
}
struct Ask{
    int s,id;
}ask[5010];
bool cmp2(Ask x,Ask y){
    if(x.s<y.s) return true;
    else return false;
}
int fi(int x){//寻找x节点的根节点
    if(node[x]==x) return x;
    else return node[x]=fi(node[x]);
}
int main()
{
    int T,n,m,q;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&n,&m,&q);
        for(int i=1;i<=n;i++){//初始化
            node[i]=i;
            num[i]=1;
        }
        for(int i=0;i<m;i++)
            scanf("%d%d%d",&c[i].fr,&c[i].to,&c[i].len);
        sort(c,c+m,cmp1);
        for(int i=0;i<q;i++){
            scanf("%d",&ask[i].s);
            ask[i].id=i;
        }
        sort(ask,ask+q,cmp2);
        int j=0,cnt=0;
        for(int i=0;i<q;i++){
            while(j<m&&c[j].len<=ask[i].s){
                int x=fi(c[j].fr);
                int y=fi(c[j].to);
                if(x!=y){
                    node[y]=x;//将其连通,根节点为x
                    cnt+=(num[x]*num[y]);
                    num[x]+=num[y];
                }
                j++;
            }
            ans[ask[i].id]=2*cnt;
        }
        for(int i=0;i<q;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}



你可能感兴趣的:(C++,ACM,HDU,并查集)