HDU 5441 Travel (并查集+数学+计数)
题意:给你一个带权的无向图,然后q(q≤5000)次询问,问有多少对城市(城市对(u,v)与(v,u)算不同的城市对,而且u≠v)之间的边的长度不超过d(如果城市u到城市v途经城市w,
那么需要城市u到城市w的长度e1≤d,同时城市w到城市v的长度e2≤d)。
析:一开始的时候,题意都读错了,怎么看都不对,原来是只要最大的d小于等于x就可以,过了好几天才知道是这样。。。。。
这个题是并查集的应用,先从d小的开始遍历,然后去判断有几个连通块,在连通块中计数,用一个数组即可,运用排列组合的知识可以知道一个连通块中, 一共有
n * (n-1)对,然后不断更新连通块中结点的数量即可,先排序再输出。其实并不难。要注意加结点的时候,谁是谁是父结点,这个是很重要的。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 2e4 + 5;
const int mod = 1e9 + 7;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct node{
int u, v, w;
bool operator < (const node &p) const{
return w < p.w;
}
};
node a[maxn*5];
struct node1{
int id, w;
bool operator < (const node1 &p) const{
return w < p.w;
}
};
node1 x[5005];
int p[maxn], w[maxn], ans[maxn]; int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } int main(){
int T; cin >> T;
while(T--){
int q;
scanf("%d %d %d", &n, &m, &q);
for(int i = 1; i <= n; ++i) p[i] = i, w[i] = 1;
for(int i = 0; i < m; ++i) scanf("%d %d %d", &a[i].u, &a[i].v, &a[i].w);
for(int i = 0; i < q; ++i) scanf("%d", &x[i].w), x[i].id = i;
sort(a, a+m);
sort(x, x+q); int j = 0, num = 0;
for(int i = 0; i < q; ++i){
while(j < m && a[j].w <= x[i].w){
int xx = Find(a[j].u);
int yy = Find(a[j].v);
if(xx != yy){
num -= w[xx] * (w[xx]-1) + w[yy] * (w[yy]-1);
w[xx] += w[yy];
w[yy] = 0;
num += w[xx] * (w[xx]-1);
p[yy] = xx;//注意父结点的选取
}
++j;
}
ans[x[i].id] = num;
} for(int i = 0; i < q; ++i)
printf("%d\n", ans[i]);
}
return 0;
}
HDU 5441 Travel (并查集+数学+计数)的更多相关文章
- hdu 5441 Travel(并查集)
Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...
- 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- HDU 5441 Travel(并查集+统计节点个数)
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
- HDU 5441——Travel——————【并查集+二分查界限】
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- hdu 5441 Travel (2015长春网赛)
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题目大意是给一个n个城市(点)m条路线(边)的双向的路线图,每条路线有时间值(带权图),然后q个询问,每个 ...
- HDU 2818 (矢量并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...
- hdu 1116 欧拉回路+并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
随机推荐
- JPA和Hibernate的区别
JPA Java Persistence API,是Java EE 5的标准ORM接口,也是ejb3规范的一部分. Hibernate,当今很流行的ORM框架,是JPA的一个实现,但是其功能是JPA的 ...
- linux 压缩/解压命令大全
.tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gun ...
- 基于XMPP的即时通信系统的建立(三)— 程序设计概览
XMPP与HTTP的比较 XMPP的优势 1. 推送数据 HTTP只能从服务器哪里请求数据,除非服务器正在响应客户端请求,否则不能向客户端发送数据.但XMPP连接是双向的,任何一方在任何时候都 ...
- bzoj3252
简单题,每次取出最长链,然后对于练上每个点x,终点在其子树内的链都要减去a[x] 这显然可以用dfs序+线段树维护 显然每个点只要删一次即可,复杂度是O(nlogn) type node=record ...
- C#中结构体的声明
定义: 结构是用户自定义的值类型 代码样式:struct Pair{ public int X, Y; //公有变量名单词的首字母大写(PascalCase规则)}struct Pa ...
- IIS与ASP.NET中的队列
一.IIS:应用程序池队列(Application pool queue,位于HTTP.SYS) 这是请求到达IIS后遇到的第一个队列,http.sys收到请求后会将请求放入对应的应用程序池队列,这样 ...
- windows2003 IIS6网络负载平衡设置
问题 随着计算机技术的不断发展,单台计算机的性能和可靠性越来越高.但现实中还是有许多应用是单台计算机难以达到,例如: 1.银行存储用户数据的数据库服务器必须保证24小时不间断的运转,并在发生严重硬件故 ...
- 为PHP增加PDO-Mysql驱动
一.问题 公司有一台老的Linux服务器,Apache+MySQL+Php结构的, 要把最近做的一个PHP项目部署到上面,做为测试环境, 由于新项目是用PHP的YII框架开发的,而YII框架的数据访问 ...
- Spring 事务管理原理探究
此处先粘贴出Spring事务需要的配置内容: 1.Spring事务管理器的配置文件: 2.一个普通的JPA框架(此处是mybatis)的配置文件: <bean id="sqlSessi ...
- Think Python Glossary
一.The way of the program problem solving: The process of formulating a problem, finding a solution, a ...