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 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
随机推荐
- Android开发学习笔记:浅谈GridView
GridView(网格视图)是按照行列的方式来显示内容的,一般用于显示图片,图片等内容,比如实现九宫格图,用GridView是首选,也是最简单的.主要用于设置Adapter. GridView常用的X ...
- Jquery 弹出新窗体
开始先用css将这个DIV设好位置,并且隐藏 function winshow() { var winNode = $(".win"); winNode.show("sl ...
- Azure SQL 数据库最新版本现已提供预览版
Tiffany Wissner 数据平台营销高级总监 我们之前在11月宣布将提供新的预览版,在该预览版中我们引入了接近完整的 SQL Server 引擎兼容性和更为高级的性能,这些都代表了下一代的 ...
- 【二叉树、堆】15轻院校赛-J-堆
原题:http://acm.zzuli.edu.cn/problem.php?cid=1099&pid=9 [描述] [输入] [输出] Sample Input 3 1 10 3 10 5 ...
- Inxi:获取Linux系统和硬件信息的神器
导读 在这篇文章里,我们将看到如何使用inxi来获取这些详情信息.在论坛技术支持中,它可以作为调试工具,迅速确定用户的系统配置和硬件信息. Inxi是一个可以获取完整的系统和硬件详情信息的命令行工具, ...
- 抽屉显示控件SlidingDrawer入门
SlidingDrawer是一个抽屉控件,代码具体路径为:android.widget.SlidingDrawer,该控件从API Level3引入,在API 17及之后的版本将不再被支持.具体效果 ...
- DbHelper第三版, 数据库通吃
using System;using System.Collections;using System.Data;using System.Data.Common;using System.Config ...
- cocos2d-x 详解之 CCSprite(精灵)- “CCSpriteBatchNode”和“CCSpriteFrameCache”
帧动画-手动切换帧-批次渲染处理动画-纹理图片的本质 ------------------------------------------------------------------------- ...
- g++安装 Gnome/Gtk+开发库 的 环境安装(安装widgets的必要条件)
sudo yum install gcc-c++ 我在更新软件源后,看了看最新版的GTK开发版是3.0的. 所以,基本可以照搬. sudo yum -t install gtk3 sudo yum - ...
- Rust 中的继承与代码复用
在学习Rust过程中突然想到怎么实现继承,特别是用于代码复用的继承,于是在网上查了查,发现不是那么简单的. C++的继承 首先看看c++中是如何做的. 例如要做一个场景结点的Node类和一个Sprit ...