题目描述

In country  A, some roads are to be built to connect the cities。However, due to limited funds, only some roads can be built.That is to say,if the limit is 100$, only roads whose cost are no more than 100$ can be built.

Now give you n cities, m roads and the cost  of each road wi (i=1..m). There are q queries, for each query there is a number k, represent the limit, you need to output the maximum number of pairs of cities that can reach each other.

输入描述:

The first line consist of two integers, n,m, n the number of cities and m the number of roads. The next m lines , each line has three integers a,b,w, represent that you can bulid a road between city a and city b with cost w.
The next line an integer q, the number of querries. The next q lines each an integer k ,the limit of the fund.
n<10000, m < 10000, k < 10000, q<10000;

输出描述:

For each querry ,you should output the anwser.
示例1

输入

3 2
1 2 1
2 3 2
1
2

输出

3

题解

并查集。

按边排序做并查集,处理好每一条边加进去之后的答案。每次询问,找到符合要求的最后一条边权加入后的答案。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 500000 + 10;
int f[maxn];
int c[maxn];
long long A;
int n, m;
struct Edge {
int a, b, c;
}e[maxn]; long long ans[maxn]; bool cmp(Edge &a , Edge &b) {
return a.c < b.c;
} int Find(int x) {
if(x != f[x]) f[x] = Find(f[x]);
return f[x];
} int main() {
while(~scanf("%d%d", &n, &m)) {
for(int i = 1; i <= n; i ++) {
f[i] = i;
c[i] = 1;
}
A = 0;
for(int i = 1; i <= m; i ++) {
scanf("%d%d%d", &e[i].a, &e[i].b, &e[i].c);
ans[i] = 0;
}
sort(e + 1, e + 1 + m, cmp);
for(int i = 1; i <= m; i ++) {
int fx = Find(e[i].a);
int fy = Find(e[i].b);
if(fx == fy) {
ans[i] = A;
continue;
}
A = A + c[fx] * c[fy];
f[fx] = fy;
c[fy] += c[fx];
ans[i] = A;
} int Q;
scanf("%d", &Q);
while(Q --) {
int x;
scanf("%d", &x);
int L = 1, R = m, p = 0;
while(L <= R) {
int mid = (L + R) / 2;
if(e[mid].c <= x) p = mid, L = mid + 1;
else R = mid - 1;
}
printf("%lld\n", ans[p]);
} }
return 0;
}

  

湖南大学ACM程序设计新生杯大赛(同步赛)B - Build的更多相关文章

  1. 湖南大学ACM程序设计新生杯大赛(同步赛)J - Piglet treasure hunt Series 2

    题目描述 Once there was a pig, which was very fond of treasure hunting. One day, when it woke up, it fou ...

  2. 湖南大学ACM程序设计新生杯大赛(同步赛)A - Array

    题目描述 Given an array A with length n  a[1],a[2],...,a[n] where a[i] (1<=i<=n) is positive integ ...

  3. 湖南大学ACM程序设计新生杯大赛(同步赛)L - Liao Han

    题目描述 Small koala special love LiaoHan (of course is very handsome boys), one day she saw N (N<1e1 ...

  4. 湖南大学ACM程序设计新生杯大赛(同步赛)I - Piglet treasure hunt Series 1

    题目描述 Once there was a pig, which was very fond of treasure hunting. The treasure hunt is risky, and ...

  5. 湖南大学ACM程序设计新生杯大赛(同步赛)E - Permutation

    题目描述 A mod-dot product between two arrays with length n produce a new array with length n. If array ...

  6. 湖南大学ACM程序设计新生杯大赛(同步赛)D - Number

    题目描述 We define Shuaishuai-Number as a number which is the sum of a prime square(平方), prime cube(立方), ...

  7. 湖南大学ACM程序设计新生杯大赛(同步赛)H - Yuanyuan Long and His Ballons

    题目描述 Yuanyuan Long is a dragon like this picture?                                     I don’t know, ...

  8. 湖南大学ACM程序设计新生杯大赛(同步赛)G - The heap of socks

    题目描述 BSD is a lazy boy. He doesn't want to wash his socks, but he will have a data structure called ...

  9. 湖南大学ACM程序设计新生杯大赛(同步赛)C - Do you like Banana ?

    题目描述 Two endpoints of two line segments on a plane are given to determine whether the two segments a ...

随机推荐

  1. 巧用Javascript将相对路径地址转换为绝对路径

    这里介绍的其实本质上是两种方法,通过创建DOM或通过JavaScript计算: 1)通过新创建的Image, 经测试会发送一个Aborted的请求,并且IE6不支持, 将new Image改成docu ...

  2. 20155117王震宇 2016-2017-2 《Java程序设计》第九周学习总结

    教材学习内容总结 JDBC JDBC API是一个Java API,可以访问任何类型表列数据,特别是存储在关系数据库中的数据.JDBC代表Java数据库连接. JDBC库中所包含的API任务通常与数据 ...

  3. 使用JSON Web Token设计单点登录系统

    用户认证八步走 所谓用户认证(Authentication),就是让用户登录,并且在接下来的一段时间内让用户访问网站时可以使用其账户,而不需要再次登录的机制. 小知识:可别把用户认证和用户授权(Aut ...

  4. /i,/m,/s,/x,/A,/s,/U,/x,/j,/u 等正则修饰符用法~

    i (PCRE_CASELESS) 如果设置了这个修饰符,模式中的字母会进行大小写不敏感匹配. m (PCRE_MULTILINE) 默认情况下,PCRE 认为目标字符串是由单行字符组成的(然而实际上 ...

  5. 【Tomcat】tomcat设置http文件下载,配置文件下载目录

    tomcat作为http的下载服务器,网上有很多办法 但我认为最简单的是:(亲测有效) 1.直接把文件放在 /var/lib/tomcat6/webapps/ROOT 目录下, 2.然后在网址中访问: ...

  6. elasticsearch集群介绍及优化【转】

    elasticsearch用于构建高可用和可扩展的系统.扩展的方式可以是购买更好的服务器(纵向扩展)或者购买更多的服务器(横向扩展),Elasticsearch能从更强大的硬件中获得更好的性能,但是纵 ...

  7. HDU 6194 string string string 2017沈阳网络赛 后缀数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6194 题意:告诉你一个字符串和k , 求这个字符串中有多少不同的子串恰好出现了k 次. 解法:后缀数组 ...

  8. Producer Flow Control 和 vmQueueCursor

    ActiveMQ可以开启或关闭生产者流量控制Producer Flow Control ,基本原理是producer 发送一条消息会收到broker返回的ack响应,当磁盘或内存快满的时候broker ...

  9. MySQL 视图、触发器、函数、存储过程

    1. 视图 1.1 什么是视图 通俗来讲,视图就是一条 select 语句执行后返回的结果集.所有我们在创建视图的时候,主要的工作就落在创建这条SQL查询语句上. 1.2 视图的特性 视图是对若干张基 ...

  10. SVM资料

    解释SMO算法比较好的文档 http://wenku.baidu.com/view/aeba21be960590c69ec3769e.html 参考博客: http://myjuno.blogbus. ...