题目描述

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. (转) 使用vivado创建工程 3

    Create a Hello World application In this experiment we will use Xilinx SDK to create a simple Hello ...

  2. oracle,sqlserver,mysql常见数据库jdbc连接

    发现JDBC连接字符串总是容易忘记,特此整理一下常用的几种数据的连接 ORACLE: /** * ORACLE * */ public static Connection getOracleConne ...

  3. [DeeplearningAI笔记]卷积神经网络2.2经典网络

    4.2深度卷积网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 [LeNet]--Lécun Y, Bottou L, Bengio Y, et al. Gradient-bas ...

  4. spoj 1825 Free tour II

    http://www.spoj.com/problems/FTOUR2/ After the success of 2nd anniversary (take a look at problem FT ...

  5. poj 1067 取石子游戏 (威佐夫博弈)

    取石子游戏 http://poj.org/problem?id=1067 Time Limit: 1000MS   Memory Limit: 10000K       Description 有两堆 ...

  6. Codeforces 148 D Bag of mice

    D. Bag of mice http://codeforces.com/problemset/problem/148/D time limit per test 2 seconds memory l ...

  7. js和jquery中的遍历对象和数组(forEach,map,each)

    arr[].forEach(function(value,index,array){ //do something }) 参数:value数组中的当前项,index当前项的索引,array原始数组: ...

  8. [php]referer应用--http防盗链技术

    1.防盗链的理解 所谓防盗链是防止其他的网站引用自己网站的资源连接,比如图片.视频等等,但是并不会阻碍从自己网站上享受资源的用户,这就要求能够将其他网站的连接请求阻止 2.防盗链的原理 其实从自己网站 ...

  9. Node.js的开源博客系统Ghost搭建教程

    准备工作 Node.js版本:0.10.x.0.12.x.4.2.x.安装步骤可参考:Node.js环境搭建 Ghost版本:0.7.4:中文集成版(33.6M),中文标准版(3.39M),英文原版( ...

  10. 24、CSS定位

    CSS定位方法 driver.find_element_by_css_selector() 1.CSS定位常用策略(方式) 1.id选择器 说明:根据元素id属性来选择 格式:#id 如:#userA ...