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轴.. 然后本题数据 ...
 
随机推荐
- iOS开发-简单的循环结构分析
			
1.while循环 while (循环条件) { 循环体: } // 1.定义循环变量 ; // 2.循环条件 ) { // 3.循环体 printf("%d\n" ...
 - pandas 使用
			
ss = [['xx','m',22],['cc','w',33],['jj','w',44],['qq','m',11]] import pandas as pd df = pd.DataFrame ...
 - Telnet、SSH和VNC 区别
			
Telnet Telnet是进行远程登录的标准协议,它是当今Internet上应用最广泛的协议之一.它把用户正在使用的终 端或计算机变成网络某一远程主机的仿真终端,使得用户可以方便地使用远程主机上的软 ...
 - Docker学习笔记 - Docker容器与外部网络的连接
			
学习目的: ip_forward 包过滤防护墙 iptables 允许端口映射访问 限制ip访问容器 1.ip_forward 控制系统是否会转发流量 检查linux系统转发是否开启命令:sysctl ...
 - Mybatis自动生成Xml文件,针对字段类型为text等会默认产生XXXXWithBlobs的方法问题
			
默认情况下产生的Mapper.xml里面存在: 需要修改generatorConfiguration.xml,里面的table加属性,如: <table domainObjectName=&qu ...
 - vue中简单的小插曲
			
我们现在来学习一下vue中一些简单的小东西: 首先我们必须要引入vue.js文件哦! 1.有关文本框里的checkbox js代码: new Vue({ el:"#app", da ...
 - hive:数据库“行专列”操作---使用collect_set/collect_list/collect_all & row_number()over(partition by 分组字段 [order by 排序字段])
			
方案一:请参考<数据库“行专列”操作---使用row_number()over(partition by 分组字段 [order by 排序字段])>,该方案是sqlserver,orac ...
 - C# Hex编码和解码
			
/// 从字符串转换到16进制表示的字符串 /// 编码,如"utf-8","gb2312" /// 是否每字符用逗号分隔 public static stri ...
 - 算法题丨Next Permutation
			
描述 Implement next permutation, which rearranges numbers into the lexicographically next greater perm ...
 - 三,前端---JS最基本的创建对象的方法
			
1:工厂模式 function createPerson(name, job){ var person = new Object(); person.name = name; person.job = ...