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 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
随机推荐
- poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)
题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...
- 基于XMPP的即时通信系统的建立(五)— openfire
现决定使用Openfire作为服务端,Openfire采用Java开发,基于XMPP的实时开源协作服务器.单台可支持上万并发用户. Openfire体系结构 Openfire体系由其提供的服务器端.客 ...
- UVa 536 Tree Recovery
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后序遍历 用杭电1710的代码改一点,就可以了. #include<iostream> #include<cstdio> #in ...
- crtmpserver流媒体服务器的介绍与搭建
crtmpserver流媒体服务器的介绍与搭建 (2012-02-29 11:28) 标签: crtmpserver C++ RTMP Server rtmp Adobe FMS(Flash ...
- poj 3352 Road Construction
// 只能说这题和上题一模一样// 我就直接贴上题代码了.. #include <iostream> #include <algorithm> #include <que ...
- ZOJ 1455 Schedule Problem(差分约束系统)
// 题目描述:一个项目被分成几个部分,每部分必须在连续的天数完成.也就是说,如果某部分需要3天才能完成,则必须花费连续的3天来完成它.对项目的这些部分工作中,有4种类型的约束:FAS, FAF, S ...
- 通过Instant Client包来使用SQL*PLUS
1.首先下载两个程序包: Instant Client Package - Basic(或Instant Client Package - Basic Lite)包 Instant Client Pa ...
- How to Download APK Files from Google Play Store
Evozi, an Android app developer, offers a one-click online APK download app that lets you download t ...
- gtid
GTID的全称为 global transaction identifier,可以翻译为全局事务标示符,GTID在原始master上的事务提交时被创建.GTID需要在全局的主-备拓扑结构中保持唯一性, ...
- HDU 5387 Clock
题意:给一个时间,求三个时针之间的夹角,分数表示. 解法:算算算.统一了一下分母. 代码: #include<stdio.h> #include<iostream> #incl ...