NOIP模拟 - 树
题目描述
给出一张n个点,m条边的无向图,摧毁每条边都需要一定的体力,并且花费的体力值各不相同,给定图中两个点x,y(x≠y),每当(x,y)之间存在路径,就需要不断摧毁当前图中花费体力最少的一条边,直到该路径不联通。他定义cost(x,y)为摧毁(x,y)之间路径花费的体力和。
他想要求出以下这个结果:
其中 i,j∈n,并且i<j 。
输入格式
第一行两个整数 n,m ,表示点数和边数。
接下来 m 行,每行三个整数 x,y,z,表示 x 和 y 之间存在一条花费体力为 z 的无向边。
输出格式
输出一个整数表示所求结果。
样例数据 1
输入
6 7
1 2 10
2 3 2
4 3 5
6 3 15
3 5 4
4 5 3
2 6 6
输出
256
备注
数据范围
对 50% 的输入数据 :1≤n≤100;1≤m≤1000
对 100% 的输入数据 :1≤n,m≤100000;1≤z≤100000
题目分析
先考虑选定的两点,置图为空,把边按权值从大到小加入,每次都检查选定的两点是否连通,若加入的这条边将其联通了,也就说明要是这两点断开,需要把权值小于当前边的边和当前边删掉,也就是一个前缀和。
再换个思路,每次添加一条边,它会将两个联通块合并,也就是让联通块1的点与联通块2的点分开需要删掉权值小于等于当前边的边和当前边(需要预处理边权的前缀和),则这条边的贡献是sze[联通块1] * sze[联通块2] * sum[小于等于这条边的边的边权总和]。
code
#include<iostream>
#include<cstdio>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 100000;
int n, m, anc[N + 5], sze[N + 5];
typedef long long ll;
ll sum[N + 5], ans;
struct node{
int from, to, weight;
node(){}
node(int _x, int _y, int _c):from(_x), to(_y), weight(_c){}
}edges[N + 5];
inline int read(){
int i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0'&& ch <= '9'; ch = getchar())
i = (i << 1) + (i << 3) + (ch - '0');
return i * f;
}
inline void wr(ll x){
if(x < 0) x = -x, putchar('-');
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
inline int getAnc(int x){
return anc[x] == x ? x : (anc[x] = getAnc(anc[x]));
}
inline bool Larger(const node &a, const node &b){
return a.weight > b.weight;
}
inline bool Smaller(const node &a, const node &b){
return a.weight < b.weight;
}
int main(){
n = read(), m = read();
for(int i = 1; i <= m; i++){
int x = read(), y = read(), w = read();
edges[i] = node(x, y, w);
}
sort(edges + 1, edges + m + 1, Smaller);
for(int i = 1; i <= m; i++) sum[i] = sum[i - 1] + edges[i].weight;
sort(edges + 1, edges + m + 1, Larger);
for(int i = 1; i <= n; i++) anc[i] = i, sze[i] = 1;
for(int i = 1; i <= m; i++){
int x = edges[i].from, y = edges[i].to;
int fx = getAnc(x), fy = getAnc(y);
if(fx != fy){
ans = (ans + sze[fy] * sze[fx] * sum[m - i + 1]) % 1000000000;
sze[fy] += sze[fx];
anc[fx] = fy;
}
}
wr(ans % 1000000000);
return 0;
}
NOIP模拟 - 树的更多相关文章
- 2018.11.03 NOIP模拟 树(长链剖分优化dp)
传送门 考虑直接推式子不用优化怎么做. 显然每一个二进制位分开计算贡献就行. 即记录fi,jf_{i,j}fi,j表示距离iii这个点不超过jjj的点的每个二进制位的0/10/10/1个数. 但直接 ...
- 【BZOJ 2957】楼房重建&&Codechef COT5 Count on a Treap&&【NOIP模拟赛】Weed 线段树的分治维护
线段树是一种作用于静态区间上的数据结构,可以高效查询连续区间和单点,类似于一种静态的分治.他最迷人的地方在于“lazy标记”,对于lazy标记一般随我们从父区间进入子区间而下传,最终给到叶子节点,但还 ...
- 6.17考试总结(NOIP模拟8)[星际旅行·砍树·超级树·求和]
6.17考试总结(NOIP模拟8) 背景 考得不咋样,有一个非常遗憾的地方:最后一题少取膜了,\(100pts->40pts\),改了这么多年的错还是头一回看见以下的情景... T1星际旅行 前 ...
- 【noip模拟赛7】足球比赛 树
描述 在2009的中国城市足球比赛中,在2^N支队中,有一些队在开赛前宣布了退出比赛.比赛采取的是淘汰赛.比如有4支队伍参加,那么1队和2队比赛,3队和4队赛,然后1队和2队的胜者与3队和4队的胜者争 ...
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
随机推荐
- [D3] Animate Chart Axis Transitions in D3 v4
When the data being rendered by a chart changes, sometimes it necessitates a change to the scales an ...
- [Angular] Implement a custom form component by using control value accessor
We have a form component: <label> <h3>Type</h3> <workout-type formControlName=& ...
- cocos2dx--vs2012+lua开发环境搭建
cocos2dx版本号:cocos2dx2.2.3 lua插件:babelua 1.5.3 下载地址:http://pan.baidu.com/s/1i3mPD8h 第一步:先关闭vs,双击下载下来 ...
- Linux字符界面安装图形界面XWindow
https://jingyan.baidu.com/article/219f4bf790f4c7de442d3825.html
- Ext常用控件
多选下拉框 var workname = new Ext.form.MultiSelect({ store: pointComboBoxStore, fieldLabel: '工作面', labelS ...
- 用static 创建类的单例
1.0 说明 通过函数调用new的static 类对象,由于static 对象只能初始化一次,由此构成单例运行. 2.0 直接代码 代码为windows,win32工程,因为只有一个文件,不上传工程 ...
- Oracle Web链接客户端
TreeSoft数据库管理系统 http://www.treesoft.cn
- Linux下的lds链接脚本简介(二)
七. SECTIONS命令 SECTIONS命令告诉ld如何把输入文件的sections映射到输出文件的各个section: 如何将输入section合为输出section; 如何把输出section ...
- js进阶 12-6 监听鼠标滚动事件和窗口改变事件怎么写
js进阶 12-6 监听鼠标滚动事件和窗口改变事件怎么写 一.总结 一句话总结:滚动事件scroll(),浏览器窗口调整监听resize(),思考好监听对象. 1.滚动事件scroll()的监听对象是 ...
- MySQL启动关闭添加到 /etc/init.d/mysqld
cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld 然后就可以使用此命令启动/关闭 mysql: /etc/ini ...