HDU 4750(Count The Pairs)
来源:互联网 发布:彩霸王软件 编辑:程序博客网 时间:2024/06/06 03:36
题意:今天南京网选的一道题,题意是给定一个边权都不一样的图,然后给出10W个询问,每个询问有个w,求图中任意两点路径中满足最大边中的最小边(瓶颈边)大于等于w的(s,t)对数,不懂的看下题理解下样例吧。
思路:这题数据量有点大1W个点,50W条边,暴力不太可能;首先注意到题目给出的边权都不一样,那么有一个性质可以知道了,就是图的最小生成树是唯一的,然后再按kruskal的顺序合并边,每次合并边,可以发现每次合并的那条边,就是连接两个集合的那条瓶颈边,这样就可以求出以这条边为瓶颈边的顶点对数,这也是kruskal中的一个性质。这题其实和12年长春现场赛的那题非常像,也是模拟kruskal的过程。
#include <vector>#include <cstdio>#include <climits>#include <cstring>#include <iostream>#include <algorithm>#define pb push_backusing namespace std;const int N = 10010;const int M = 500010;int n, m, p, check[N], q;int sum[N], tot[N];struct Edge{ int a, b, w; Edge(){} Edge(int a, int b, int w):a(a), b(b), w(w){}}edge[M];inline bool cmp(Edge x, Edge y){ if(x.w != y.w) return x.w < y.w; return x.a < y.a;}void initi(int n){ for(int i = 0;i <= n;i++){ check[i] = i; sum[i] = 1; }}int Find(int a){ if(check[a] != a) check[a] = Find(check[a]); return check[a];}void solve(){ vector<int> V; int idx = 1, i; memset(tot, 0, sizeof tot); for(int i = 0;i < m;i++){ int fa = Find(edge[i].a); int fb = Find(edge[i].b); if(fa != fb){ V.pb(edge[i].w); tot[idx++] = sum[fa] * sum[fb] * 2; sum[fb] += sum[fa]; check[fa] = fb; } } for(i = 1;i < idx;i++) tot[i] += tot[i - 1]; cin >> p; for(i = 0;i < p;i++){ scanf("%d", &q); int index = lower_bound(V.begin(), V.end(), q) - V.begin(); printf("%d\n", tot[idx - 1] - tot[index]); } return ;}int main(){ while(cin >> n >> m){ int i, a, b, w; for(i = 0;i < m;i++){ scanf("%d%d%d", &a, &b, &w); edge[i] = Edge(a, b, w); } initi(n); sort(edge, edge + m, cmp); solve(); } return 0;}
- HDU 4750(Count The Pairs)
- HDU 4750 Count The Pairs
- hdu 4750 Count The Pairs
- HDU 4750 Count The Pairs
- HDU-4750 Count The Pairs
- Count The Pairs HDU
- hdu 4750——Count The Pairs
- hdu 4750 Count The Pairs 并查集+离线
- 并查集+二分-hdu-4750-Count The Pairs
- hdu 4750 Count The Pairs (2013南京网络赛)
- hdu 4750 Count The Pairs 最小生成树
- HDU 4750 Count The Pairs(最小生成树)
- HDU 4750 Count The Pairs (最小生成树)
- Count The Pairs(hdu-4750)(最小生成树)
- HDOJ 4750 Count The Pairs
- hdoj4750 Count The Pairs
- Count The Pairs
- Count The Pairs
- 小明的调查作业 nyist48
- 黑马程序员--Java学习笔记之面向对象思想(多态、内部类、匿名内部类、异常类)
- 【leetcode】Rotate List
- linux 下 android 源码下载 (repo sync)
- Hibernate createSQLQuery与createQuery的区别
- HDU 4750(Count The Pairs)
- 为什么我认为goroutine和channel是把别的平台上类库的功能内置在语言里
- Ecshop微博登录方式与用户账号的绑定
- android 圆角边框及图片
- 十二五中国电信宽带战略解析(三):规模与效益
- Crack the code interview_Q1.2
- sysconf()函数应用举例:查看CPU及内存信息
- socket编程 for PHP
- c++面试宝典补缺