hdu 5441 Travel(并查集)
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?
The first line contains one integer T,T≤, which represents the number of test case. For each test case, the first line consists of three integers n,m and q where n≤,m≤,q≤. 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∈{,...,n} and d≤. 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.
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.
比赛的时候坑在这题了,一直TLE,统计个数的方法不对,导致一直在这题徘徊,否则就能去思考其它的题了,可能就不是这种破成绩了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 600000
#define inf 1e12
int n,m,q;
struct Node{
int u,v,d;
}edge[N];
struct Node1{
int num;
int idd;
int ans;
}query[];
bool cmp1(Node a,Node b){
return a.d<b.d;
}
bool cmp2(Node1 a,Node1 b){
return a.num<b.num;
}
bool cmp3(Node1 a,Node1 b){
return a.idd<b.idd;
}
/////////////////////////////////////////////
int fa[];
int cnt[];
void init(){
for(int i=;i<;i++){
fa[i]=i;
cnt[i]=;
}
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
} ////////////////////////////////////////////////////// int main()
{
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].d);
}
for(int i=;i<q;i++){
scanf("%d",&query[i].num);
query[i].idd=i;
query[i].ans=;
}
sort(edge,edge+m,cmp1);
sort(query,query+q,cmp2);
int w=;
int ans=;
for(int i=;i<q;i++){
//printf("+=====");
while(w<m && edge[w].d<=query[i].num){ int root1=find(edge[w].u);
int root2=find(edge[w].v); if(root1!=root2){
ans+=cnt[root1]*cnt[root2]; //printf("***%d\n",ans);
cnt[root2]+=cnt[root1];
fa[root1]=root2;
}
w++;
}
//printf("%d\n",ans*2);
query[i].ans=ans*;
}
sort(query,query+q,cmp3);
for(int i=;i<q;i++){
printf("%d\n",query[i].ans);
}
}
return ;
}
hdu 5441 Travel(并查集)的更多相关文章
- 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 (并查集+数学+计数)
题意:给你一个带权的无向图,然后q(q≤5000)次询问,问有多少对城市(城市对(u,v)与(v,u)算不同的城市对,而且u≠v)之间的边的长度不超过d(如果城市u到城市v途经城市w, 那么需要城市u ...
- 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 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
随机推荐
- 《Java程序员面试笔试宝典》之Static关键字有哪些作用
static关键字主要有两种作用:第一,只想为某特定数据类型或对象分配单一的存储空间,而与创建对象的个数无关.第二,希望某个方法或属性与类而不是对象关联在一起,也就是说,在不创建对象的情况下就可以通过 ...
- 人物角色群体攻击判定(三)Physics.OverlapSphere(群体攻击)
使用Physics.OverlapSphere来检测不方便调试, 其他都可以. 核心代码: //检测敌人 public void CheckEnemy() { Collider[] cols = ...
- 常用JS代码整理
1: function request(paras) { 2: var url = location.href; 3: var paraString = url.substring(url.index ...
- 在Ubuntu上为Android系统编写Linux内核驱动程序
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6568411 在智能手机时代,每个品牌的手机都有 ...
- Dalvik虚拟机的启动过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8885792 在Android系统中,应用程序进 ...
- fuser:用文件或者套接口表示进程
fuser:用文件或者套接口表示进程 作用:fuser命令用文件或者套接口表示进程. 用法:fuser [-a | -s | -c] [-4 | -6] [-n space] [-k [-i] [-s ...
- CPU性能测试
用计算圆周率的办法来测试cpu性能 4*a(1) 是 bc 主動提供的一個計算 pi 的函數,至於 scale 就是要 bc 計算幾個小數點下位數的意思.當 scale 的數值越大, 代表 pi 要被 ...
- 父元素高度为auto,子元素使用top:-50%没有效果的问题
无意间在实现元素垂直居中的一种方式测试到,当一个元素高度没有指定的情况下,其 postion:relative;top:-50%;无效 后来查阅w3c看到这样一句话: <percentage&g ...
- jquery模拟checkbox效果,以及background-size在jquery中的使用。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- VB短信猫开发包,支持超长短信
一.短信猫开发包(长短信/异步调用)说明: 短信猫开发包以OCX控件的形式提供,支持Windows平台下常用的开发工具:如VB.VB.net.VC++.Power Builder.C#.DELPH ...