题目描述

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. Mac下安装SVN插件javaHL not available的解决方法

    在Mac下安装Eclipse插件svnEclipse插件后,每次打开Eclipse都会弹出如下弹出框: 提示你本机缺少JavaHL Library. 选择Eclipse→偏好设置(preference ...

  2. Vue 插槽详解

    Vue插槽,是学习vue中必不可少的一节,当初刚接触vue的时候,对这些掌握的一知半解,特别是作用域插槽一直没明白. 后面越来越发现插槽的好用. 分享一下插槽的一些知识吧. 分一下几点: 1.插槽内可 ...

  3. 字符串:SAM

    HDU4622:区间查询不同子串个数 用后缀自动机预处理出所有区间的不同子串个数 建立n次后缀自动机 #include <stdio.h> #include <string.h> ...

  4. Lua的工具资源3

    [LuaSrcDiet] (5.0.2) - 通过删除不必要的空白和注释缩减Lua文件的大小. [LuaProfiler] (5.0) - 一个用来查找Lua应用瓶颈的工具time profiler ...

  5. 【BZOJ】1087: [SCOI2005]互不侵犯King

    [算法]状态压缩型DP [题解]http://www.cnblogs.com/xtx1999/p/4620227.html (orz) https://www.cnblogs.com/zbtrs/p/ ...

  6. 【BZOJ】2466: [中山市选2009]树 高斯消元解异或方程组

    [题意]给定一棵树的灯,按一次x改变与x距离<=1的点的状态,求全0到全1的最少次数.n<=100. [算法]高斯消元解异或方程组 [题解]设f[i]=0/1表示是否按第i个点的按钮,根据 ...

  7. 【leetcode 简单】第三十六题 最小栈

    设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. ...

  8. pytesser模块WindowsError错误解决方法

    在使用pytesser做图片文字识别时遇到 WindowsError: [Error 2] 错误,报错内容如下: Traceback (most recent call last): File &qu ...

  9. linux下生成core dump文件方法及设置【转】

    转自:http://blog.csdn.net/mrjy1475726263/article/details/44116289 源自:http://andyniu.iteye.com/blog/196 ...

  10. 匿名函数、lambda表达式

    匿名函数 func = lambda x: y #x是形参,y是返回值 键字lambda表示匿名函数,冒号前面的x表示函数参数,冒号后面的y表示匿名函数的返回值. 例1:返回列表中长度大于等于3的元素 ...