HDU5441 Travel 有秩并查集
题目链接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
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.
Note that (a,b) and (b,a) are counted as different pairs and a and b must be different cities.
n个城市,m条路,每条路有一个权值w; q个询问每次输入一个数temp,这个人可以走任何权值<=temp的路,
//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 有秩并查集相关推荐
- HDU - 5441 Travel 离线处理+并查集
题意 给我们一个图 给出这个图的n个点 和m个边和权值 然后再分别给我们q个查询 每个查询给一个x 让我们在这个图中找出多少个不同的有序对 使得从x到y的最大权值不超过x n≤20000,m≤1000 ...
- 并查集(压缩路径+按秩排序)
细节 并查集 可进行合并与查找操作,一个集合可以以其中的一个元素为代表 查找: 两元素代表是否相同(路径压缩优化) 按秩合并:秩就是树的高度,合并时,总是将高度小的树的树根指向高度大的树根,防止 树越 ...
- BZOJ4668: 冷战 [并查集 按秩合并]
BZOJ4668: 冷战 题意: 给定 n 个点的图.动态的往图中加边,并且询问某两个点最早什 么时候联通,强制在线. 还可以这样乱搞 并查集按秩合并的好处: 深度不会超过\(O(\log n)\) ...
- HDU 1811 Rank of Tetris(并查集按秩合并+拓扑排序)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 牛客多校8 - All-Star Game(线段树分治+并查集按秩合并的撤销操作)
题目链接:点击查看 题目大意:有 n 个球员和 m 个球迷,一个球员可能是多个球迷的粉丝,需要选择最少的球员进行比赛,使得所有的球迷都愿意观看(对于每个球迷来说,都有至少一个其喜欢的球员入选比赛) 对 ...
- NOIP2013 D1T3 货车运输 倍增LCA OR 并查集按秩合并
思路: Kruskal求最大生成树+倍增LCA // by SiriusRen #include <cstdio> #include <cstring> #include &l ...
- CCPC-Wannafly Winter Camp Day3 (Div2, onsite) I 石头剪刀布(按秩合并并查集)
题解:每次有两个事件: y y去挑战xx,如果赢了可以坐在x x的位置,打平或者输了就要被淘汰. 询问在进行所有一类事件后,有多少种情况可以让x x现在还没有被淘汰. 对于第二类事件,我们假设x x挑 ...
- 20211229[按秩合并并查集 最小生成树][BZOJ4668]冷战
20211229[按秩合并并查集.最小生成树][BZOJ4668]冷战 题意:给定N点,动态加边与询问两点最早是哪条边开始连通,强制在线 首先如果离线的话可以直接跑最小生成树,不过代码不好处理. 当然 ...
- BZOJ 4668 冷战(按秩合并并查集+LCA)
4668: 冷战 Time Limit: 10 Sec Memory Limit: 256 MB Submit: 627 Solved: 303 [Submit][Status][Discuss] ...
最新文章
- c语言的发展8个过程,c语言发展过程.pptx
- websphere变成英文了
- Spring Boot自定义横幅生成
- Kafka开发指南之 如何Kafka 事务型生产者,保证生产者exactly once
- mysql存储过程实va_mysql-存储过程(二)-逻辑判断语句
- matlab 编辑器设置,编辑器设置,包括语言、备份和显示设置
- Mac Vmware Fusion在虚拟机中共享文件夹
- 零散知识点总结(1) Gradle 使用配置总结
- 2013年微软编程之美大赛初赛第二题(博客园居然可以插入代码!!)
- rocketmq4.x快速入门指南
- 【剑指Offer】15顺时针打印矩阵
- 2019杭州云栖大会探营:神龙的秘密
- 人工智能机器学习————MINST数据集的简单处理
- 华为静态,动态NAT,Easy IP实验!超详细,有手就能学会
- 高次osu(重邮第13届ACM程序设计大赛-网络赛)
- 在java里四舍五入怎么做_利用java怎么实现一个四舍五入功能
- 一个游戏程序员的学习资料(转)
- Hadoop与spark性能比较试验
- 程序中图片透明 函数
- github工具之OA综合利用python