Time Limit: 1500/1000 MS (Java/Others)

 Memory Limit: 131072/131072 K (Java/Others)

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,T≤5, which represents the number of test case.

For each test case, the first line consists of three integers n,m and q where n≤20000,m≤100000,q≤5000. The Undirected Kingdom has n cities and mbidirectional 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 d≤100000. 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
 
Source
 
 
 
 
题意:给出一个图,有n个节点,m条边,q个询问
每一个询问给出x
问,图中有多少点对,点对有一条路径的边长中最大的一条边的权值<=x
注意:(a,b)和(b,a)算是不同的点对
 
在线的话,想不出什么好的解法
 
离线的话,先把询问的x和图中边的权值小到大排序
然后遍历一次询问
 
用并查集
w[i]表示若i是这个集合的代表节点,则w[i]=这个集合的点数,否则w[i]=0
 
当询问x时,我们要知道目前有多少个集合,集合的代表节点是谁,每一个集合的点数
则此时的点对数=各个集合的点数a*(a-1)的和
 
要快速找出每一个集合的点数,有数组w
那要怎么快速有多少个集合和集合的代表节点呢?
我刚开始的想法是用一个set保存每一个集合的代表节点,若一个节点不是代表节点了,就删除该节点,更行w
这样每次询问的时候需要遍历一遍set,还是太慢了
 
后来一想,不用啊,只要再有一个变量sum,每次合并集合的时候就更新sum就可以啦
sum+=w[fau]*w[fav]*2
这样对于每一个询问x,直接记录print=sum就可以了
 
 
 
 
 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<set> using namespace std; struct Edge
{
int u,v,w;
};
Edge edge[+]; struct Query
{
int x,id;
};
Query qry[]; int w[+];
long long print[];
int fa[+]; void init(int n)
{
for(int i=;i<=n;i++){
fa[i]=i;
w[i]=;
}
} bool cmp1(Edge z,Edge y)
{
return z.w<y.w;
}
bool cmp2(Query z,Query y)
{
return z.x<y.x;
} int find_fa(int a)
{
if(fa[a]==a)
return a;
else{
fa[a]=find_fa(fa[a]);
w[fa[a]]+=w[a];
w[a]=;
return fa[a];
}
} void solve(int ,int ,int ); int main()
{
int test;
scanf("%d",&test);
while(test--){
int n,m,Q;
scanf("%d %d %d",&n,&m,&Q);
init(n);
for(int i=;i<=m;i++){
scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w);
}
for(int i=;i<=Q;i++){
scanf("%d",&qry[i].x);
qry[i].id=i;
}
solve(n,m,Q);
}
return ;
} void solve(int n,int m,int Q)
{
sort(edge+,edge+m+,cmp1);
sort(qry+,qry+Q+,cmp2); int cnt=;
long long sum=;
for(int i=;i<=Q;i++){
while(cnt<=m&&edge[cnt].w<=qry[i].x){
int fau=find_fa(edge[cnt].u);
int fav=find_fa(edge[cnt].v);
if(fau!=fav){
sum+=w[fau]*w[fav]*;
fa[fau]=fav;
w[fav]+=w[fau];
w[fau]=;
}
cnt++;
}
print[qry[i].id]=sum;
} for(int i=;i<=Q;i++){
printf("%I64d\n",print[i]);
} return ;
}
 
 

hdu 5441 travel 离线+带权并查集的更多相关文章

  1. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  2. Hdu 2047 Zjnu Stadium(带权并查集)

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. How Many Answers Are Wrong (HDU - 3038)(带权并查集)

    题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...

  4. hdu 2818 Building Block (带权并查集,很优美的题目)

    Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...

  5. hdu 3635 Dragon Balls (带权并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. How Many Answers Are Wrong HDU - 3038 (经典带权并查集)

    题目大意:有一个区间,长度为n,然后跟着m个子区间,每个字区间的格式为x,y,z表示[x,y]的和为z.如果当前区间和与前面的区间和发生冲突,当前区间和会被判错,问:有多少个区间和会被判错. 题解:x ...

  7. hdu 3047–Zjnu Stadium(带权并查集)

    题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...

  8. HDU 3047 Zjnu Stadium(带权并查集)

    题意:有一个环形体育场,有n个人坐,给出m个位置关系,A B x表示B所在的列在A的顺时针方向的第x个,在哪一行无所谓,因为假设行有无穷个. 给出的座位安排中可能有与前面矛盾的,求有矛盾冲突的个数. ...

  9. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

随机推荐

  1. Codeforces Round #131 (Div. 2)

    A. System of Equations \(a\)的范围在\(\sqrt n\)内,所以暴力枚举即可. B. Hometask 需要被2.5整除,所以末位必然为0,如果0没有出现,则直接返回-1 ...

  2. HDU-4276 The Ghost Blows Light (树形DP+背包)

    题目大意:在一个n个节点的树形迷宫中,1为起点,n为出口.每个节点上有一定价值的珠宝,在节点之间移动的时间已知,问在能走出迷宫的前提下并且不超过m的时间内能收集的最多珠宝是多少? 题目分析:在树上,从 ...

  3. VPS搭建VPN(pptpd)

    环境:Ubuntu Server 12.04 下载FQ程序 wget http://cdxf.yun.ftn.qq.com/ftn_handler/40ad8a2875adf1f7b5193f54a5 ...

  4. 论文笔记之:Multiple Feature Fusion via Weighted Entropy for Visual Tracking

    Multiple Feature Fusion via Weighted Entropy for Visual Tracking ICCV 2015 本文主要考虑的是一个多特征融合的问题.如何有效的进 ...

  5. MySQL 5.5: InnoDB Change Buffering

    To speed up bulk loading of data, InnoDB implements an insert buffer, a special index in the InnoDB ...

  6. Unicode基本概念

    Unicode是计算机可以支持这个星球上多种语言的秘密武器.通过使用一个或者多个字节来表示一个字符的方法突破了ASCII的限制.Unicode可以表示超过90000个字符. 使用方式:a=u'hell ...

  7. SQL Server中数据库文件的存放方式,文件和文件组

    原文地址:http://www.cnblogs.com/CareySon/archive/2011/12/26/2301597.html   SQL Server中数据库文件的存放方式,文件和文件组 ...

  8. javascript保留两位小数

      原文地址http://blog.csdn.net/he20101020/article/details/8503308   <script type="text/javascrip ...

  9. ASP.NET读取EXCEL文件的三种经典方法

      1.方法一:采用OleDB读取EXCEL文件:   把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下: public DataSet ExcelToDS(string Path) { ...

  10. Oracle数据库—— 存储过程与函数的创建

    一.涉及内容 1.掌握存储过程与函数的概念. 2.能够熟练创建和调用存储过程与函数. 二.具体操作 1.创建存储过程,根据职工编号删除scott.emp表中的相关记录. (1)以scott 用户连接数 ...