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. CPP链表示例

    #include<iostream> #include<stdlib.h> using namespace std; typedef struct Student_data { ...

  2. STL之queue

    描述 使用STL中的queue,完成入队.出队.获取队首.获取队尾等基本操作. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { queue<int> ...

  3. JAVA_SE基础——4.path的临时配置&Classpath的配置

    这次,我来写下关于path的临时配置的心的 我来说个有可能的实例:如果你去到别人的电脑 又想写代码 又不想改乱别人的path配置的话  再说别人愿意你在别人的电脑上瞎配吗? 那该怎么办呢? 那没问题 ...

  4. Python爬虫之urllib模块1

    Python爬虫之urllib模块1 本文来自网友投稿.作者PG,一个待毕业待就业二流大学生.玄魂工作室未对该文章内容做任何改变. 因为本人一直对推理悬疑比较感兴趣,所以这次爬取的网站也是平时看一些悬 ...

  5. Python内置函数(16)——ord

    英文文档: ord(c) Given a string representing one Unicode character, return an integer representing the U ...

  6. redis命令详解

      redis中添加key value元素:set key value;       获取元素:get key ;   redis中添加集合:lpush key value1 value2 value ...

  7. gradle入门(1-7)eclipse和gradle集成插件的安装和使用

    一.安装gradle插件:buildship 1.安装插件 gradle默认的本地缓存库在c盘user目录下的.gradle文件夹下,安装好gradle后,可以添加环境变量GRADLE_USER_HO ...

  8. Linux之Shell命令

    开始接触Linux命令行,学习Linux文件系统导航以及创建.删除.处理文件所需的命令.  注:文末有福利! 几个快捷键: Linux发行版通常使用Ctrl+Alt组合键配合F1~F7进入要使用的控制 ...

  9. ssh整合之四单独搭建struts的运行环境

    1.导入struts的基本jar包 2.在web.xml中配置我们struts的核心控制器StrutsPrepareAndExecuteFilter <?xml version="1. ...

  10. python/基础输出输入用法

    输出及输入的简单用法 print print,中文意思是打印,在python里它不是往纸上打印,而是打印在命令行,或者叫终端.控制台里面.print是python里很基本很常见的一个操作,它的操作对象 ...