Description

You are given an undirected graph G with N vertices and M edges. Each edge has a length. Below are two definitions.

  1. 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.
  2. Define min_pair(uv) as min{max_len(p) | p is a path connecting the vertices u and v.}. If there is no paths connecting u and vmin_pair(uv) 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(uv) is not greater than a given integer A.

Input

The first line of input contains three integer NM 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 ab, and c (1 ≤ ab ≤ 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?的更多相关文章

  1. POJ 1117 Pairs of Integers

    Pairs of Integers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4133 Accepted: 1062 Des ...

  2. POJ 1987 Distance Statistics 树分治

    Distance Statistics     Description Frustrated at the number of distance queries required to find a ...

  3. [双连通分量] POJ 3694 Network

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9434   Accepted: 3511 Descripti ...

  4. POJ 2828 线段树(想法)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15422   Accepted: 7684 Desc ...

  5. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  6. poj 2239 Selecting Courses (二分匹配)

    Selecting Courses Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8316   Accepted: 3687 ...

  7. POJ 1456 Supermarket 区间问题并查集||贪心

    F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  8. 【POJ】1269 Intersecting Lines(计算几何基础)

    http://poj.org/problem?id=1269 我会说这种水题我手推公式+码代码用了1.5h? 还好新的一年里1A了---- #include <cstdio> #inclu ...

  9. 【POJ】2187 Beauty Contest(旋转卡壳)

    http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...

随机推荐

  1. NetFPGA-1G-CML点亮 LED

    前言 用vivado建立工程的时候选择的型号为:XC7K325tffg676-1 在以下代码文件中,仿真与设计都没有问题.在xdc文件中的时钟约束与锁相环配置中还存在问题,没有寻找到解决办法 使用手册 ...

  2. js 获取 最近七天 30天 昨天的方法 -- 转

    自己用到了 找了下  先附上原作的链接  http://www.cnblogs.com/songdongdong/p/7251254.html 原谅我窃取你的果实  谢谢你谢谢你 ~ 先附上我自己用到 ...

  3. 【技巧】Java工程中的Debug信息分级输出接口及部署模式

    也许本文的标题你们没咋看懂.但是,本文将带大家领略输出调试的威力. 灵感来源 说到灵感,其实是源于笔者在修复服务器的ssh故障时的一个发现. 这个学期初,同袍(容我来一波广告产品页面,同袍官网)原服务 ...

  4. 第三章 jQuery中的事件与动画

    第三章jQuery中的事件与动画 一. jQuery中的事件 jQuery事件是对javaScript事件的封装. 1.基础事件 在javaScript中,常用的基础事件有鼠标事件.键盘事件.wind ...

  5. Mock API是如何在开发中发光发热的?

    在长期的服务过程中,我们经常会遇到前来咨询的用户与我们反馈以下这种情况:咨询者是一个前端人员,在项目开发的过程中需要与后端进行对接,遇到后端还没完成数据输出的情况下,他只好写静态模拟数据,在遇到大型项 ...

  6. 安装 docker-compose

    安装Docker-Compose之前,请先安装 python-pip,安装好pip之后,就可以安装Docker-Compose了. 一.检查是否已经安装 二.安装 docker-compose 1.安 ...

  7. [机器学习实战]K-近邻算法

    1. K-近邻算法概述(k-Nearest Neighbor,KNN) K-近邻算法采用测量不同的特征值之间的距离方法进行分类.该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近 ...

  8. 项目实战15—企业级堡垒机 jumpserver

    本文收录在Linux运维企业架构实战系列 环境准备 系统:CentOS 7 IP:192.168.10.101 关闭selinux 和防火墙 # CentOS $ setenforce # 可以设置配 ...

  9. python 杂货铺

    python 杂货铺之不知道的python操作 1.交互模式下的神奇的_ windos中cmd交互模式中下(python2,python3),最近一个表达式的值赋给变量 _.这样我们就可以把它当作一个 ...

  10. angular-单页面应用程序

    我们都知道angularjs是单一页面应用程序,那什么是单一页面应用程序呢?单一页面应用程序到底有什么好处呢? 下面我们来看一下: 首先我觉得可以把页面的响应模式分成这样大概3个阶段: 1. 最传统的 ...