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 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
随机推荐
- BindService总结
一.整体工程图 二.activity_bind.xml <?xml version="1.0" encoding="utf-8"?> <Lin ...
- PHP连接Mysql服务器的操作
我们的数据存储在数据库中以后,要把数据和网页联系起来的话,要通过web服务器的解释器进行读取数据,再传递给客户端网页.如图: 这里,我选择了PHP作为学习的解释器.下面就具体来总结一下PHP连接MYS ...
- android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法
最近项目开发中用到了android:launchMode="singleTask" 和 onNewIntent(Intent intent)两个特性,现总结一下经验: androi ...
- 用函数式的 Swift 实现图片转字符画的功能
今天整理 Pocket 中待看的文章,看到这篇<Creating ASCII art in functional Swift>,讲解如何用 Swift 将图片转成 ASCII 字符.具体原 ...
- Linux文件权限管理
一.设置文件所属的用户以及所属的组(chown,chgrp) chgrp用来更改文件的组拥有者,其一般格式为:chgrp [option] group file(1)把文件test的组拥有者改为zfs ...
- [Python学习笔记][Python内置函数]
Python 常用内建函数 比较基础的列表 abs(x) 求绝对值 pow(x,y) 返回x的y次方,等同于x**y round(x[,小数位数]) 对x进行四舍五入,若不指定位数,则返回整数 chr ...
- DE2带的IP核ISP12362报错问题解决 Error:avalon_slave_1_irq: associatedAddressablePoint out of range
问题来源与对友晶提供的ISP1362 IP核的使用,由于Quartus II版本问题,它提供的IP基于7.0版本,而我用的版本为11.1,在SOPC Builder中重新加载IP,就出现了上述的错误报 ...
- spring mvc + mybatis + spring aop声明式事务管理没有作用
在最近的一个项目中,采用springMVC.mybatis,发现一个很恼人的问题:事务管理不起作用!!网上查阅了大量的资料,尝试了各种解决办法,亦未能解决问题! spring版本:3.0.5 myba ...
- Javascript进阶篇——浏览器对象—JavaScript计时器
---恢复内容开始--- JavaScript 计时器在JavaScript中,我们可以在设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行.计时器类型:一次性计时器:仅在指定的延迟时间之后触 ...
- Volley报错!!!No address associated with hostname
年轻人检查你的网络去吧,这是没有网络导致的原因