Travel 并查集
来源:互联网 发布:ida如何找到数据 编辑:程序博客网 时间:2024/05/17 06:04
题意:给一个图,若干询问,每次询问只经过边权<=w的边,x能到达的点数
并查集啊,对询问和边排序,直接合并,维护size,查询
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>#define N 105000using namespace std;int fa[N],size[N],n,m,k;struct data{ int u,v,w;}d[4*N];bool cmpd(data a,data b){return a.w<b.w;}struct query{ int u,w,ans,id;}q[N];bool cmpq(query a,query b){return a.w<b.w;}bool back(query a,query b){return a.id<b.id;}int find(int x){ if(fa[x]==x)return x; fa[x]=find(fa[x]); return fa[x];}void hb(int x,int y){ x=find(x);y=find(y); if(x==y)return; fa[y]=x;size[x]+=size[y];}int main(){ scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++){fa[i]=i;size[i]=1;} for(int i=1;i<=m;i++) scanf("%d%d%d",&d[i].u,&d[i].v,&d[i].w); sort(d+1,d+m+1,cmpd); for(int i=1;i<=k;i++){ scanf("%d%d",&q[i].u,&q[i].w); q[i].id=i; } sort(q+1,q+k+1,cmpq); q[k+1].w=q[k].w+1; int now=q[1].w,ee=1,pos=1; while(now<=q[k].w){ for(;ee<=m&&d[ee].w<=now;ee++) hb(d[ee].u,d[ee].v); for(;q[pos].w==now;pos++) q[pos].ans=size[find(q[pos].u)]; now=q[pos].w; } sort(q+1,q+k+1,back); for(int i=1;i<=k;i++) printf("%d\n",q[i].ans); return 0;}
阅读全文
0 0
- Travel 并查集
- 并查集 Travel
- HDU5441 Travel 并查集
- HDU5441 Travel 并查集
- HDU 5441 Travel 并查集
- hdu 5441 Travel(离线+并查集)
- 离线+并查集 hdu5441 Travel
- hdu 5441 Travel(并查集应用)
- HDU5441 Travel 有秩并查集
- HDU 5441 Travel (并查集)
- HDU 5441 Travel(并查集)
- hdu 5441 Travel 排序 并查集
- [HDU 5441]Travel[并查集]
- HDU 5441 Travel 并查集
- HDU-5441 Travel(并查集)
- HDU 5441 Travel(并查集)
- HDU 5441 Travel (并查集)
- Hdu 5441 Travel(并查集)
- LinuxC简谈之文件读写的一些研究和总结
- Ionic3环境配置
- 二分排序(搜索)树
- Mysql数据库操作常用命令
- C++怎么传递一个数组到LUA
- Travel 并查集
- oracle数据闪回
- 工作3年后的自己
- 怎么编译lua源代码成库
- CodeForces
- Android系统应用层App启动过程分析
- 正则
- Cloudera Manager CDH 5.12 大数据处理平台部署指南
- LeetCode