题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5441

Travel

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1173    Accepted Submission(s): 431

Problem Description
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?
Input
The first line contains one integer T,T≤5, which represents the number of test case.

For each test case, the first line consists of three integers n,m and q where n≤20000,m≤100000,q≤5000. 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∈{1,...,n} and d≤100000. 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.

Output
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.

Sample Input
1 5 5 3 2 3 6334 1 5 15724 3 5 5705 4 3 12382 1 3 21726 6000 10000 13000
Sample Output
2 6 12
Source
2015 ACM/ICPC Asia Regional Changchun Online

n个城市,m条路,每条路有一个权值w; q个询问每次输入一个数temp,这个人可以走任何权值<=temp的路,
问这个人能走多少对城市 (城市(a,b)和(b,a)算两对); 就是算这个图有多少连通分量(不符合<=temp的边不加入图中),
对于每个连通分量,例如某连通分量有ABC三个城市,则有AB BA AC CA BC CB六个城市对;即有n个城市的连通分量
有n(n-1)个城市对;每个连通分量的城市数就是该集合根节点的秩(代码中的num[]数组); 
计算结果时,每新加入一条边后的结果ans_now,可以由为加入该条边之前的结果ans递推得到;
例如两个连通分量的节点数为 fu ,fv;则join这两个连通分量后的结果ans_now=ans+(fu+fv)*(fu+fv-1)-fu*(fu-1)-fv*(fv-1)=2*fu*fv;
第一个程序没这么推也过了 就是程序时间复杂度较高
//time 873MS
//memery 2988K#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;struct node
{int u,v;int w;
}st[110000];struct nod
{int val;int id;friend bool operator<(nod a,nod b){return a.val<b.val;}
}qu[6000];
bool cmp(node a,node b)
{return a.w<b.w;
}
int father[30000];
int num[30000];
void initial(int n)
{int i;for(i=0;i<=n;++i){father[i]=i;num[i]=1;}
}
int find(int x)
{int t=x;while(x!=father[x]){x=father[x];}while(t!=father[t]){int temp=t;t=father[t];father[temp]=x;}return x;
}
bool join(int x,int y)
{x=find(x);y=find(y);if(y<x){swap(x,y);}if(x!=y){father[y]=x;num[x]+=num[y];}
}
int ans[6000];
int main()
{int T;scanf("%d",&T);while(T--){int n,m,q;scanf("%d%d%d",&n,&m,&q);int i;for(i=0;i<m;++i){scanf("%d%d%d",&st[i].u,&st[i].v,&st[i].w);}sort(st,st+m,cmp);for(i=0;i<q;++i){scanf("%d",&qu[i].val);qu[i].id=i;}sort(qu,qu+q);int t=0;initial(n);for(i=0;i<q;++i){int temp;temp=qu[i].val;while(t<m&&st[t].w<=temp){int u=st[t].u;int v=st[t].v;join(u,v);t++;}int ans_temp=0;for(int j=1;j<=n;++j){if(father[j]==j&&num[j]>1){ans_temp+=(num[j]*(num[j]-1));}}ans[qu[i].id]=ans_temp;}for(i=0;i<q;++i){printf("%d\n",ans[i]);}}
}
//time 312MS
//memery 2992K#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;struct node
{int u,v;int w;
}st[110000];struct nod
{int val;int id;friend bool operator<(nod a,nod b){return a.val<b.val;}
}qu[6000];bool cmp(node a,node b)
{return a.w<b.w;
}
int father[30000];
int num[30000];
void initial(int n)
{int i;for(i=0;i<=n;++i){father[i]=i;num[i]=1;}
}
int find(int x)
{int t=x;while(x!=father[x]){x=father[x];}while(t!=father[t]){int temp=t;t=father[t];father[temp]=x;}return x;
}
bool join(int x,int y)
{x=find(x);y=find(y);if(y<x){swap(x,y);}if(x!=y){father[y]=x;num[x]+=num[y];return 1;}return 0;
}
int ans[6000];
int main()
{int T;scanf("%d",&T);while(T--){int n,m,q;scanf("%d%d%d",&n,&m,&q);int i;for(i=0;i<m;++i){scanf("%d%d%d",&st[i].u,&st[i].v,&st[i].w);}sort(st,st+m,cmp);for(i=0;i<q;++i){scanf("%d",&qu[i].val);qu[i].id=i;}sort(qu,qu+q);int t=0;initial(n);int ans_temp=0;for(i=0;i<q;++i){int temp;temp=qu[i].val;while(t<m&&st[t].w<=temp){int u=st[t].u;int v=st[t].v;int u_r=find(u);int v_r=find(v);int num_u=num[u_r];int num_v=num[v_r];if(join(u,v)){ans_temp+=(2*num_u*num_v);}t++;}ans[qu[i].id]=ans_temp;}for(i=0;i<q;++i){printf("%d\n",ans[i]);}}
}

HDU5441 Travel 有秩并查集相关推荐

  1. HDU - 5441 Travel 离线处理+并查集

    题意 给我们一个图 给出这个图的n个点 和m个边和权值 然后再分别给我们q个查询 每个查询给一个x 让我们在这个图中找出多少个不同的有序对 使得从x到y的最大权值不超过x n≤20000,m≤1000 ...

  2. 并查集(压缩路径+按秩排序)

    细节 并查集 可进行合并与查找操作,一个集合可以以其中的一个元素为代表 查找: 两元素代表是否相同(路径压缩优化) 按秩合并:秩就是树的高度,合并时,总是将高度小的树的树根指向高度大的树根,防止 树越 ...

  3. BZOJ4668: 冷战 [并查集 按秩合并]

    BZOJ4668: 冷战 题意: 给定 n 个点的图.动态的往图中加边,并且询问某两个点最早什 么时候联通,强制在线. 还可以这样乱搞 并查集按秩合并的好处: 深度不会超过\(O(\log n)\) ...

  4. HDU 1811 Rank of Tetris(并查集按秩合并+拓扑排序)

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. 牛客多校8 - All-Star Game(线段树分治+并查集按秩合并的撤销操作)

    题目链接:点击查看 题目大意:有 n 个球员和 m 个球迷,一个球员可能是多个球迷的粉丝,需要选择最少的球员进行比赛,使得所有的球迷都愿意观看(对于每个球迷来说,都有至少一个其喜欢的球员入选比赛) 对 ...

  6. NOIP2013 D1T3 货车运输 倍增LCA OR 并查集按秩合并

    思路: Kruskal求最大生成树+倍增LCA // by SiriusRen #include <cstdio> #include <cstring> #include &l ...

  7. CCPC-Wannafly Winter Camp Day3 (Div2, onsite) I 石头剪刀布(按秩合并并查集)

    题解:每次有两个事件: y y去挑战xx,如果赢了可以坐在x x的位置,打平或者输了就要被淘汰. 询问在进行所有一类事件后,有多少种情况可以让x x现在还没有被淘汰. 对于第二类事件,我们假设x x挑 ...

  8. 20211229[按秩合并并查集 最小生成树][BZOJ4668]冷战

    20211229[按秩合并并查集.最小生成树][BZOJ4668]冷战 题意:给定N点,动态加边与询问两点最早是哪条边开始连通,强制在线 首先如果离线的话可以直接跑最小生成树,不过代码不好处理. 当然 ...

  9. BZOJ 4668 冷战(按秩合并并查集+LCA)

    4668: 冷战 Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 627  Solved: 303 [Submit][Status][Discuss] ...

最新文章

  1. c语言的发展8个过程,c语言发展过程.pptx
  2. websphere变成英文了
  3. Spring Boot自定义横幅生成
  4. Kafka开发指南之 如何Kafka 事务型生产者,保证生产者exactly once
  5. mysql存储过程实va_mysql-存储过程(二)-逻辑判断语句
  6. matlab 编辑器设置,编辑器设置,包括语言、备份和显示设置
  7. Mac Vmware Fusion在虚拟机中共享文件夹
  8. 零散知识点总结(1) Gradle 使用配置总结
  9. 2013年微软编程之美大赛初赛第二题(博客园居然可以插入代码!!)
  10. rocketmq4.x快速入门指南
  11. 【剑指Offer】15顺时针打印矩阵
  12. 2019杭州云栖大会探营:神龙的秘密
  13. 人工智能机器学习————MINST数据集的简单处理
  14. 华为静态,动态NAT,Easy IP实验!超详细,有手就能学会
  15. 高次osu(重邮第13届ACM程序设计大赛-网络赛)
  16. 在java里四舍五入怎么做_利用java怎么实现一个四舍五入功能
  17. 一个游戏程序员的学习资料(转)
  18. Hadoop与spark性能比较试验
  19. 程序中图片透明 函数
  20. github工具之OA综合利用python

热门文章

  1. 怎么做出可以卖的电路板
  2. Laravel中的多对多关系
  3. 模板 泛化 全特化 偏特化
  4. 【tensorflow】Input to reshape is a tensor with xxx values, but the requested shape requires a multipl
  5. 对‘字典’按照value值进行排序
  6. 迅速成为炙手可热的新一代程序员看《Ajax基础教程》
  7. python-霍兰德人格分析
  8. 【网络教程】同花顺公式编辑的基本语法,帮助说明
  9. 人工智能发展的核心——机器学习
  10. 怎样才算得上是一名优秀的软件测试工程师呢?