【NOIP模拟】从我背后出现
Description
给定n个点m条边的无向连通图,对于每条边求出强制选这条边后的最⼩⽣成树⼤⼩。
\(n\leq 10^5,m\leq 2*10^5\)
Input Format
第 1 行包含两个整数 n,m,表示点数和路径数。第 2~m+1 行每行三个整数 ui,vi,wi,表示有一条可以修的路连接 ui 和 vi ,费用为 wi ,wi ≤ 1e9。
Output Format
输出 m 行,每行一个整数,表示选择第i条路径的前提下的最小花费。
路径按输入的顺序编号为 1~m 。
Solution
原图构造最小生成树,对于一条边,如果他是最小生成树上的边,那么答案不变,
那如果不在上面,例如一条边u to v,
那么肯定是在原先树上u到v的路径上删去一条边,然后把这条边添加上去,可以发现删去的是最大的边
然后倍增预处理一下树上最大边即可
Code
#include <cstdio>
#include <algorithm>
#include <cmath>
#define N 100010
#define ll long long
using namespace std;
struct info{
int fr,to,w,nex,id;
friend bool operator < (info a,info b){
return a.w<b.w;
}
}ke[N*2],e[N*4];
int n,m,_log,tot,head[N*4],dep[N],f[N][20];
ll mx[N][20],Ans,Que[N*2];
bool used[N*2];
inline int read() {
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch = getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch = getchar();}
return x*f;
}
inline void Link(int u,int v,int w){
e[++tot].to=v;
e[tot].w=w;
e[tot].nex=head[u];
head[u]=tot;
}
void dfs(int u,int fa){
for(int i=1;i<=_log;++i){
f[u][i]=f[f[u][i-1]][i-1];
mx[u][i]=max(mx[u][i-1],mx[f[u][i-1]][i-1]);
}
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
if(v==fa) continue;
dep[v]=dep[u]+1;
mx[v][0]=e[i].w;
f[v][0]=u;
dfs(v,u);
}
}
int fa[N];
int Find(int u){
return (fa[u]==u)?u:fa[u]=Find(fa[u]);
}
inline void kruskal(){
for(int i=1;i<=n;++i) fa[i]=i;
sort(ke+1,ke+m+1);
int cnt=0;
for(int i=1;i<=m;++i){
int px=Find(ke[i].fr),py=Find(ke[i].to);
if(px!=py){
fa[px]=py;
used[i]=1;
Link(ke[i].fr,ke[i].to,ke[i].w);
Link(ke[i].to,ke[i].fr,ke[i].w);
cnt++;
Ans+=ke[i].w;
}
if(cnt==n-1) break;
}
}
inline ll LCA(int u,int v){
ll r=0;
if(dep[u]>dep[v]) swap(u,v);
int d=dep[v]-dep[u];
for(int i=0;i<=_log;++i)if(d&(1<<i)) r=max(r,mx[v][i]),v=f[v][i];
if(u==v) return r;
for(int i=_log;i>=0;--i)
if(f[u][i]!=f[v][i]) r=max(r,max(mx[u][i],mx[v][i])),u=f[u][i],v=f[v][i];
return max(r,max(mx[v][0],mx[u][0]));
}
int main(){
n=read(),m=read();
_log=log(n)/log(2);
for(int i=1;i<=m;++i)ke[i].fr=read(),ke[i].to=read(),ke[i].w=read(),ke[i].id=i;
kruskal();
dfs(1,0);
for(int i=1;i<=m;++i)
if(used[i]) Que[ke[i].id]=Ans;
else Que[ke[i].id]=Ans-LCA(ke[i].fr,ke[i].to)+ke[i].w;
for(int i=1;i<=m;++i)
printf("%lld\n",Que[i]);
return 0;
}
【NOIP模拟】从我背后出现的更多相关文章
- 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) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的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 #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
- CH Round #52 - Thinking Bear #1 (NOIP模拟赛)
A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...
- CH Round #49 - Streaming #4 (NOIP模拟赛Day2)
A.二叉树的的根 题目:http://www.contesthunter.org/contest/CH%20Round%20%2349%20-%20Streaming%20%234%20(NOIP 模 ...
随机推荐
- PHP编辑器PhpStrom运行缓慢问题
最近在使用phpstorm开发项目的时候,在加载文件运行时,不管有多大,如果项目文件数据比较多的话,都能够让phpstorm卡到死机.其中调整过内存设置,关闭过动态提示,使用过phpstorm的安全模 ...
- C# 跨平台的支付类库ICanPay
随着微软的开源,越来越多的项目支持跨平台,但是各种支付平台提供的类库,又老又不支持跨平台,吐槽下,尤其是微信,还有好多坑,于是ICanPay诞生了,今天就来讲ICanPay是什么,怎么使用? ICan ...
- mysql 5.7.13 安装配置方法图文教程(linux) (转)
http://www.jb51.net/article/87160.htm ************************************************ linux环境Mysql ...
- NSNotification
1.什么是NSNotification 每个运行中的application都有一个NSNotificationCenter的成员变量,它的功能就类似与公共栏,对象在这里注册关注每个确定Notifica ...
- python网络编程之单线程之间的并发
单线程之间的并发就是利用一个线程实现并发的效果,也就是利用了cup遇到阻塞的那段时间去做别的事情,从而提高了cup的利用率,使之在单个线程中就实现了并发的效果. 下面就是一个简单的服务端单个线程实现并 ...
- 解释器模式(Interpreter)
解释器模式(Interpreter)解释器模式是我们暂时的最后一讲,一般主要应用在OOP开发中的编译器的开发中,所以适用面比较窄. Context类是一个上下文环境类,Plus和Minus分别是用来计 ...
- 浅谈IM(InstantMessaging) 即时通讯/实时传讯
一.IM简要概述 IM InstantMessaging(即时通讯,实时传讯)的缩写是IM,互动百科大致解释是一种可以让使用者在网络上建立某种私人聊天(chatroom)的实时通讯服务. 大部 ...
- Python Requests 库学习笔记
概览 实例引入 import requests response = requests.get('https://www.baidu.com/') print(type(response)) prin ...
- MongoDB优化与一些需要注意的细节
这里总结下这段时间使用mongo的心得,列出了几个需要注意的地方. 1. 系统参数及mongo参数设置 mongo参数主要是storageEngine和directoryperdb,这两个参数一开始不 ...
- 原生javascript选项卡
js选项卡是一个常用的实现.这里我们将用原生js来将其给予实现. 首先html代码: <div id="container"> <input type=" ...