湖南大学ACM程序设计新生杯大赛(同步赛)B - Build
题目描述
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.
输入
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的更多相关文章
- 湖南大学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 ...
- 湖南大学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 ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)L - Liao Han
题目描述 Small koala special love LiaoHan (of course is very handsome boys), one day she saw N (N<1e1 ...
- 湖南大学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 ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)E - Permutation
题目描述 A mod-dot product between two arrays with length n produce a new array with length n. If array ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)D - Number
题目描述 We define Shuaishuai-Number as a number which is the sum of a prime square(平方), prime cube(立方), ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)H - Yuanyuan Long and His Ballons
题目描述 Yuanyuan Long is a dragon like this picture? I don’t know, ...
- 湖南大学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 ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)C - Do you like Banana ?
题目描述 Two endpoints of two line segments on a plane are given to determine whether the two segments a ...
随机推荐
- MVC网站发布到 IIS
接下来将发布成功的站点部署到iis7.0. 步骤如下: 1. 安装 Microsoft .net FrameWork 4.0安装包(网站开发时候使用的就是.net framework4.0框架); 2 ...
- web启动@Autowired不能自动注入
使用struts2,下面为action代码 Java代码 package com.edar.web.platform; import org.apache.struts2.convention.ann ...
- AJAX获取服务器文件
写一个按钮,点击后在指定的div里显示本地txt文件内容 在本地新建一个test.txt,里面随便写点内容就好. <!DOCTYPE html> <html> <head ...
- 怎样用javascript关闭本窗口
大家都知道window.close()是用来关闭窗口的,而且ie和firefox都是支持的. 为了实现用户对浏览器的绝对控制,ie中用close关闭非open打开的窗口时回弹出一个对话框询问用户,怎么 ...
- JQuery 中三十一种选择器的应用
选择器(selector)是CSS中很重要的概念,所有HTML语言中的标记都是通过不同的CSS选择器进行控制的.用户只需要通过选择器对不同的HTML标签进行控制,并赋予各种样式声明,即可实现各种效果. ...
- 【POJ】3233 Matrix Power Series
[算法]二分+矩阵快速幂 [题意]给定矩阵A和整数k,MOD,求A^0+A^1+A^2+...+A^k. [题解] 定义题目要求的答案为f(n),即: $$f_n=\sum_{i=0}^{n}A^i$ ...
- eclipse运行Android项目出现“The connection to adb is down, and a severe error has occured. You must restart adb and Eclipse. ”
重启eclipse之后仍然出现同样错误,此时可以尝试一下方法: cmd打开命令窗口: 之后重启eclipse,基本可以解决问题!
- 【译】第三篇 SQL Server代理警报和操作员
本篇文章是SQL Server代理系列的第三篇,详细内容请参考原文. 正如这一系列的上一篇所述,SQL Server代理作业是由一系列的作业步骤组成,每个步骤由一个独立的类型去执行,除了步骤中执行的工 ...
- sg函数&&子状态的讨论
题目链接:https://cn.vjudge.net/contest/269933#problem/H 具体思路:首先,这是一个公平的比赛,并且是两个人参与,两个人都足够聪明,并且可以通过有限步结束比 ...
- php遍历路径——php经典实例
php遍历路径——php经典实例 代码: <html> <head> <title>遍历目录</title> <meta charset=&quo ...