hdu 4750 Count The Pairs(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750
代码:
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- const int maxn = ;
- const int maxm = ;
- struct Edge
- {
- int u,v,w;
- Edge(int u=,int v=,int w=): u(u), v(v), w(w) {}
- bool operator < (const Edge& rhs) const
- {
- return w < rhs.w;
- }
- }edges[maxm];
- struct Query
- {
- int id;
- int num;
- bool operator < (const Query& rhs) const
- {
- return num < rhs.num;
- }
- }Q[maxn*];
- int pa[maxn];
- int counts[maxn];
- int ans[maxn*];
- int find(int x)
- {
- return x == pa[x] ? x : pa[x] = find(pa[x]);
- }
- int main()
- {
- //freopen("E:\\acm\\input.txt","r",stdin);
- int n,m,p;
- while(cin>>n>>m)
- {
- for(int i=; i<n; i++)
- {
- pa[i] = i;
- counts[i] = ;
- }
- for(int i=; i<m; i++)
- {
- int a,b,c;
- scanf("%d %d %d",&a,&b,&c);
- edges[i] = Edge(a,b,c);
- }
- sort(edges,edges+m);
- scanf("%d",&p);
- for(int i=; i<p; i++)
- {
- scanf("%d",&Q[i].num);
- Q[i].id = i;
- }
- sort(Q,Q+p);
- int cnt = ;
- int sum = ;
- int tot = n*(n-); //这么做前提是图是连通的,只能样例给的是连通的
- for(int i=; i<p; i++)
- {
- while(cnt<m && edges[cnt].w < Q[i].num)
- {
- int u_fa = find(edges[cnt].u);
- int v_fa = find(edges[cnt].v);
- if(u_fa != v_fa)
- {
- sum = sum + counts[u_fa]*counts[v_fa]*;
- pa[v_fa] = u_fa;
- counts[u_fa] += counts[v_fa];
- }
- cnt++;
- }
- ans[Q[i].id] = tot - sum;
- }
- for(int i=; i<p; i++)
- printf("%d\n",ans[i]);
- }
- }
hdu 4750 Count The Pairs(并查集)的更多相关文章
- hdu 4750 Count The Pairs(并查集+二分)
Problem Description With the 60th anniversary celebration of Nanjing University of Science and Techn ...
- HDU 4750 Count The Pairs(并查集)
题目链接 没有发现那个点,无奈. #include <cstdio> #include <cstring> #include <cmath> #include &l ...
- HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)
Count The Pairs Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...
- HDU 4750 Count The Pairs ★(图+并查集+树状数组)
题意 给定一个无向图(N<=10000, E<=500000),定义f[s,t]表示从s到t经过的每条路径中最长的边的最小值.Q个询问,每个询问一个t,问有多少对(s, t)使得f[s, ...
- 2013南京网赛1003 hdu 4750 Count The Pairs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意:给出一个无向图,f(a,b)表示从点a到点b的所有路径中的每条路径的最长边中的最小值,给出 ...
- HDU 4750 Count The Pairs (离线并查集)
按边从小到大排序. 对于每条边(from, to, dist),如果from和to在同一个集合中,那么这条边无意义,因为之前肯定有比它更小的边连接了from和to. 如果from和to不属于同一个集合 ...
- [2013 ACM/ICPC Asia Regional Nanjing Online C][hdu 4750]Count The Pairs(kruskal + 二分)
http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意: 定义f(u,v)为u到v每条路径上的最大边的最小值..现在有一些询问..问f(u,v)>=t ...
- hdu 4750 Count The Pairs (2013南京网络赛)
n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
随机推荐
- oracle插入数据报错ORA-26026
今天进行数据清理时发现报错ORA-26026,主要是把从交易库提取数据并插入到归档库中. 检查一下发现是归档库的索引问题. 当时为了提高插入速度,所以删除了归档库的索引,可能对主键索引产生了影响. 解 ...
- SQL Server T-SQL高级查询【转】
高级查询在数据库中用得是最频繁的,也是应用最广泛的. Ø 基本常用查询 --select select * from student; --all 查询所有 select all sex from ...
- vector预分配空间溢出
vector 当一个vector预分配的存储空间用完之后,为维护其连续的对象数组,它必须在另外一个地方重新分配大块新的(更大的)存储空间,并把以前已有的对象拷贝到新的存储空间中去. // A clas ...
- 从ZOJ2114(Transportation Network)到Link-cut-tree(LCT)
[热烈庆祝ZOJ回归] [首先声明:LCT≠动态树,前者是一种数据结构,而后者是一类问题,即:LCT—解决—>动态树] Link-cut-tree(下文统称LCT)是一种强大的数据结构,不仅可以 ...
- jquery中mouseout和mouseleave 事件的区别
今天用jQuery写了一个选项卡的效果,用mouseout事件控制了鼠标的移出,结果发现在移出时div会发生闪动,于是网上各种查资料觉得用mouseleave更合适一些,
- Android 中 View移动总结:ViewDragHelper学习及用法详解
如上图简单呈现出两个方块后,提出一个需求: 1.拖动方块时,方块(即子View)可以跟随手指移动. 2.一个方块移动时,另一个方块可以跟随移动. 3.将方块移动到左边区域(右边区域)后放开(即手指离开 ...
- Object之魔术函数__toString() 直接输出对象引用时自动调用
__toString()是快速获取对象的字符串信息的便捷方式 在直接输出对象引用时自动调用的方法. __toString()的作用 当我们调试程序时,需要知道是否得出正确的数据.比如打印一个对象时,看 ...
- WorkerScript QML Type
官方描述:在一个Qt Quick应用程序中可以使用线程了. Import Statement: import QtQuick .属性:source : url信号:message(jsobje ...
- Arduino从基础到实践第三章练习题
先写在这里,还没经过测试. 1. LED两端往中间移动,到中间后向两边返回. // adr301.ino , , , , , , , , , }; ); ; ; unsigned long chang ...
- bzoj 2818: Gcd 歐拉函數
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1633 Solved: 724[Submit][Status] Descript ...