hdu3938(离线并查集)
来源:互联网 发布:jsp中引用js文件 编辑:程序博客网 时间:2024/05/22 17:40
好题!!!
题意:给你一个能量,问你能这些能量能修多少种路,修一条路所需要的能量是这条路的起点和终点所有路径中最长的一条边的最小值。
既然是最长边的最小值,那么确定一条边以后后来出现的在路径上的长的边就没有用了。所以可以按照克鲁斯卡尔的思想对边进行排序。
用并查集判环(类似最小生成树),当新加入一条边时,以这条边为花费的点对就是两个未合并的并查集里面的点的数量的乘积。然后用前缀和保存答案。
因为边的长度为1E8,所以要先把问题保存起来,再去算(我的理解是 类似离散化)。
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int n,m,q;int par[10005];struct node{ int from; int to; int w; bool operator < (const node& T) const { return w<T.w; }}edge[50004];int sum[10005];struct answer{ int l; int pos; int ans ; bool operator < (const answer& T) const { return l<T.l; }}query[10005];int find(int u){ if(u==par[u]) return u; return par[u] = find(par[u]);}int unite(int u,int v){ int fa = find(u); int fb = find(v); if(fa==fb) return 0; int tmp=0; if(fa<fb) { par[fb] = fa; tmp = sum[fa]*sum[fb]; sum[fa] += sum[fb]; } else { par[fa] = fb; tmp = sum[fa] * sum[fb]; sum[fb] += sum[fa]; } return tmp;}bool cmp(answer a,answer b){ return a.pos<b.pos;}int main(){ int u,v,w; while(scanf("%d%d%d",&n,&m,&q)!=EOF) { for(int i=0;i<=n;i++) par[i] = i,sum[i] = 1; for(int i=0;i<m;i++) scanf("%d%d%d",&edge[i].from ,&edge[i].to,&edge[i].w); sort(edge,edge+m); for(int i=0;i<q;i++) { scanf("%d",&query[i].l); query[i].pos = i; query[i].ans = 0; } int cnt = 0; sort(query,query+q); for(int i=0;i<q;i++) { while(edge[cnt].w<=query[i].l&&cnt<m) { int fa = find(edge[cnt].from); int fb = find(edge[cnt].to); if(fa==fb) { cnt++; continue; } else { query[i].ans += unite(edge[cnt].from,edge[cnt].to); cnt++; } } if(i>=1) query[i].ans += query[i-1].ans; } sort(query,query+q,cmp); for(int i=0;i<q;i++) { printf("%d\n",query[i].ans); } } return 0;}
阅读全文
0 0
- hdu3938(并查集+离线)
- hdu3938(离线并查集)
- hdu3938 并查集+离线化
- hdu3938(Portal)并查集
- HDU3938 Portal 并查集
- HDU3938 并查集 并查集
- hdu 3938(离线并查集)
- 离线的并查集
- 离线+并查集 Portal
- HDU 3938 Portal(离线+Kruskal+并查集)
- 3938 Portal(离线型的并查集)
- hdu 3938 Portal(离线并查集)
- hdu 3938(离线的并查集)
- 3938 HDU Portal(离线型并查集)
- HDOJ 题目3938 Portal(带权值并查集+离线)
- 【LCA】Tarjan离线算法(并查集+dfs)模板
- HDU-3938 Portal (并查集+离线输出)
- LCT+最小生成树+并查集+离线(BZOJ2594)
- 白话经典算法系列之六 快速排序 快速搞定
- 【数论-莫比乌斯】hdu 6053 TrickGCD
- 异步任务
- 2017多校第4场 HDU 6071 Lazy Running 同余最短路
- 关于使用markdown的一点心得
- hdu3938(离线并查集)
- 三分-HDU3714
- 小白、教你学C++(8)异常处理结构层次
- (CodeChef
- 旋转变换(一)旋转矩阵
- 烦人的ppt
- j2me 游戏开发详解
- poj 1163 DP
- nobone-sync ......js后watched一次后自动终止的原因