【Codeforces 609E】【Stree】【最小生成树】
来源:互联网 发布:java泛型编程最全总结 编辑:程序博客网 时间:2024/04/27 23:47
题目大意
给出n个点,m条有权边,现对于每一条边,你需要回答出包含这条边的最小生成树的总边权值。
题解
一个显然的结论,无论怎么样,生成树与最小生成树不同的边最多为一条。求出最小生成树,枚举加入哪条边,用倍增算法求出所加边在树上路径边权的最大值,更改答案即可。
code
#include<set>#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>#define fo(i,j,k) for(int i=j;i<=k;i++)#define fd(i,j,k) for(int i=j;i>=k;i--)using namespace std;int const maxn=200000,maxm=200000;int n,m,gra,to[maxn*2+10],val[maxn*2+10],next[maxn*2+10],begin[maxn+10],dep[maxn+10], father[maxn+10][20],mx[maxn+10][20],fa[maxn+10],queue[maxn+10];bool inqueue[maxn+10];long long ans[maxm+10];struct rec{int u,v,w,num;};rec edge[maxm+10];bool cmp(rec i,rec j){ return (i.w<j.w)||((i.w==j.w)&&(i.u<j.u))||((i.w==j.w)&&(i.u==j.u)&&(i.v<j.v));}int getfather(int x){ if(!fa[x])return x; return fa[x]=getfather(fa[x]);}void insert(int u,int v,int w){ to[++gra]=v; val[gra]=w; next[gra]=begin[u]; begin[u]=gra;}void bfs(int s){ int head=0,tail=0; inqueue[queue[++tail]=s]=1; for(;head!=tail;){ int now=queue[++head]; for(int i=begin[now];i;i=next[i]) if(!inqueue[to[i]]){ dep[to[i]]=dep[now]+1; father[to[i]][0]=now; mx[to[i]][0]=val[i]; inqueue[queue[++tail]=to[i]]=1; } }}int lc(int u,int v){ int ans=0; if(dep[u]<dep[v])swap(u,v); fd(i,log(n)/log(2),0) if(dep[father[u][i]]>=dep[v]){ ans=max(ans,mx[u][i]); u=father[u][i]; } if(u==v)return ans; fd(i,log(n)/log(2),0) if(father[u][i]!=father[v][i]){ ans=max(ans,max(mx[u][i],mx[v][i])); u=father[u][i]; v=father[v][i]; } return max(ans,max(mx[u][0],mx[v][0]));}int main(){ //freopen("street.in","r",stdin); //freopen("street.out","w",stdout); freopen("d.in","r",stdin); freopen("d.out","w",stdout); scanf("%d%d",&n,&m); fo(i,1,m) scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w),edge[i].num=i; sort(edge+1,edge+m+1,cmp); fo(i,1,m){ int fu=getfather(edge[i].u),fv=getfather(edge[i].v); if(fu!=fv){ ans[0]+=edge[i].w; ans[edge[i].num]=-1; fa[fu]=fv; insert(edge[i].u,edge[i].v,edge[i].w); insert(edge[i].v,edge[i].u,edge[i].w); } } dep[1]=1; bfs(1); fo(j,1,log(n)/log(2)) fo(i,1,n){ father[i][j]=father[father[i][j-1]][j-1]; mx[i][j]=max(mx[i][j-1],mx[father[i][j-1]][j-1]); } fo(i,1,m) if(ans[edge[i].num]==-1)ans[edge[i].num]=ans[0]; else ans[edge[i].num]=ans[0]+edge[i].w-lc(edge[i].u,edge[i].v); fo(i,1,m)printf("%lld\n",ans[i]); return 0;}
0 0
- 【Codeforces 609E】【Stree】【最小生成树】
- Codeforces 892/E Envy 最小生成树的query
- E--最小生成树
- E-最小生成树
- CodeForces 609E Minimum spanning tree for each edge (lca+最小生成树+倍增)
- Codeforces 609E Minimum spanning tree for each edge 树链剖分+RMQ(st算法)+最小生成树
- CodeForces 609 E.Minimum spanning tree for each edge(最小生成树-Kruskal+在线倍增LCA)
- [Codeforces 125E] MST Company (单度限制最小生成树)
- Codeforces Round #303 (Div. 2) E. Paths and Trees (最短路+变形最小生成树)
- Codeforces#303-E - Paths and Treesg-最短路+最小生成树
- Codeforces 125E MST Company (单度限制最小生成树)
- Codeforces Round #446 (Div. 2) E. Envy 最小生成树(kruscal原理)(离线查询)
- E - Network(最小生成树)
- Stree
- Stree
- [LCT维护最小生成树 || CDQ分治 || 线段树 并查集 dfs树] Codeforces 603E #334 (Div. 1) E. Pastoral Oddities
- codeforces 76A Gift 最小生成树
- 【最小生成树】Codeforces 707B Bakery
- 浅谈Java设计模式(三)单例模式(Singleton)
- SET IDENTITY_INSERT 选项使用
- iframe 自适应高度
- 高通平台MSM8916LCM模块移植(一)-bootloader部分
- 吉林敦化黑石电站来我公司考察
- 【Codeforces 609E】【Stree】【最小生成树】
- 第一个linux编译
- 卷积神经网络
- easyUI SubGrid 学习
- hdu 1800 Flying to the Mars 贪心算法
- Java编译和Tomcat启动遇到的一些问题
- Python 语言及其应用 Chapter_10 Note 3 时间time
- Android中的Interpolator
- 在IntelliJ IDEA上将WSDL生成Java代码