POJ 2832 How Many Pairs?
Description
You are given an undirected graph G with N vertices and M edges. Each edge has a length. Below are two definitions.
- Define max_len(p) as the length of the edge with the maximum length of p where p is an arbitrary non-empty path in G.
- Define min_pair(u, v) as min{max_len(p) | p is a path connecting the vertices u and v.}. If there is no paths connecting u and v, min_pair(u, v) is defined as infinity.
Your task is to count the number of (unordered) pairs of vertices u and v satisfying the condition that min_pair(u, v) is not greater than a given integer A.
Input
The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of vertices, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c < 108) describing an edge connecting the vertices a and b with length c. Each of the following Q lines gives a query consisting of a single integer A (0 ≤ A < 108).
Output
Output the answer to each query on a separate line.
Sample Input
4 5 4
1 2 1
2 3 2
2 3 5
3 4 3
4 1 4
0
1
3
2
Sample Output
0
1
6
3
题解:
将边和询问都按从小到大排序,然后对于一组询问,我们枚举所有小于当前询问的边,然后把边的两个端点对应的集合进行计算,并查集合并维护
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int N=,M=,QM=;
typedef long long ll;
struct node{
int x,y,dis;
bool operator <(const node &pp)const{
return dis<pp.dis;
}
}e[M];
int gi(){
int str=;char ch=getchar();
while(ch>'' || ch<'')ch=getchar();
while(ch>='' && ch<='')str=(str<<)+(str<<)+ch-,ch=getchar();
return str;
}
int n,m,Q,size[N],fa[N];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
struct Question{
int id,x;ll sum;
}q[QM];
bool compone(const Question &pp,const Question &qq){
return pp.x<qq.x;
}
bool comptwo(const Question &pp,const Question &qq){
return pp.id<qq.id;
}
void work(){
int x,y,dis;
n=gi();m=gi();Q=gi();
for(int i=;i<=m;i++){
e[i].x=gi();e[i].y=gi();e[i].dis=gi();
}
for(int i=;i<=Q;i++)q[i].id=i,q[i].x=gi();
for(int i=;i<=n;i++)fa[i]=i,size[i]=;
sort(e+,e+m+);
sort(q+,q+Q+,compone);
int cnt=,sum=,p=;
for(int i=;i<=Q;i++){
while(e[p].dis<=q[i].x && cnt<n- && p<=m){
x=e[p].x;y=e[p].y;
if(find(x)==find(y)){
p++;continue;
}
sum+=(ll)size[find(y)]*size[find(x)];
size[find(x)]+=size[find(y)];
fa[find(y)]=find(x);
p++;cnt++;
}
q[i].sum=sum;
}
sort(q+,q+Q+,comptwo);
for(int i=;i<=Q;i++)
printf("%lld\n",q[i].sum);
}
int main()
{
work();
return ;
}
POJ 2832 How Many Pairs?的更多相关文章
- POJ 1117 Pairs of Integers
Pairs of Integers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4133 Accepted: 1062 Des ...
- POJ 1987 Distance Statistics 树分治
Distance Statistics Description Frustrated at the number of distance queries required to find a ...
- [双连通分量] POJ 3694 Network
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9434 Accepted: 3511 Descripti ...
- POJ 2828 线段树(想法)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 15422 Accepted: 7684 Desc ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj 2239 Selecting Courses (二分匹配)
Selecting Courses Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8316 Accepted: 3687 ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- 【POJ】1269 Intersecting Lines(计算几何基础)
http://poj.org/problem?id=1269 我会说这种水题我手推公式+码代码用了1.5h? 还好新的一年里1A了---- #include <cstdio> #inclu ...
- 【POJ】2187 Beauty Contest(旋转卡壳)
http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...
随机推荐
- nyoj 寻找最大数
寻找最大数 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大, 比如当n=920813467185 ...
- python的模块和包
==模块== python语言的组织结构层次: 包->模块->代码文件->类->函数->代码块 什么是模块呢 可以把模块理解为一个代码文件的封装,这是比类更高一级的封装层 ...
- LeetCode & Q66-Plus One-Easy
Array Description: Given a non-negative integer represented as a non-empty array of digits, plus one ...
- JavaScript查找数组中最大的值
// 查找一个数组中最大的数 // 定义一个方法 searchMax function searchMax(arr) { // 声明一个变量MaxNumber假设为数组中最大的值arr[0]; var ...
- ORA-12514:TNS:lisntener does not currently know of service requested in connect descriptor
在使用工具连接oracle库的时候出现了异常 根据理解初步估计是服务或者监听器没有启动 于是链接到数据库服务器进行查看 服务都已经开启,重启后链接依旧出现上述问题 使用lsnrctl status ...
- javascript学习(2)修改html元素和提示对话框
一.修改html元素 1.修改p元素 1.1.源代码 1.2.执行前 1.3.执行后 2.修改div元素的className 2.1.源代码 1.2.执行前 1.3.执行后 3.直接在当前位置输出内容 ...
- 开发技巧(3-1)Eclipse查找关键字
1.选择资源目录->选择search-file菜单 2.在弹出的对话框中, 输入要[搜索的字符串],选择[selected resources],点击[search]按钮 3.搜索结果
- [CodeForces 11D] A Simple Task - 状态压缩入门
状态压缩/Bitmask 在动态规划问题中,我们会遇到需要记录一个节点是否被占用/是否到达过的情况.而对于一个节点数有多个甚至十几个的问题,开一个巨型的[0/1]数组显然不现实.于是就引入了状态压缩, ...
- CSS属性操作
CSS属性操作 1 属性选择器 Elenment(元素) E[att] 匹配所有具有att属性的E元素,不考虑它的值.(注意:E在此处可以省略)(推荐使用) 例如:[po]{ font-size: 5 ...
- 新手创建Vue项目
======================安装vue=============================(参考网址:http://www.bubuko.com/infodetail-21320 ...