[JZOJ4596]street
来源:互联网 发布:3m易清洁涂层 知乎 编辑:程序博客网 时间:2024/06/05 07:17
题目大意
给出
题目分析
根据
倍增解决(当然你也可以打树剖
时间复杂度
代码实现
#include <algorithm>#include <iostream>#include <cstdio>#include <cctype>#include <cmath>#include <queue>using namespace std;typedef long long LL;int read(){ int x=0,f=1; char ch=getchar(); while (!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar(); while (isdigit(ch)) x=x*10+ch-'0',ch=getchar(); return x*f;}const int N=200500;const int LGN=18;const int E=N<<1;const int M=200500;struct edge{ int x,y,l;}es[M],og[M];bool operator<(edge a,edge b){return a.l<b.l;}int next[E],tov[E],len[E];int last[N],fa[N],high[N];int f[N][LGN],g[N][LGN];int n,m,lgn,tot;LL ans;int getfather(int son){return fa[son]==son?son:fa[son]=getfather(fa[son]);}void insert(int x,int y,int z){tov[++tot]=y,len[tot]=z,next[tot]=last[x],last[x]=tot;}void Kruscal(){ sort(es+1,es+1+m); for (int i=1;i<=n;i++) fa[i]=i; for (int i=1,fx,fy;i<=m;i++) { int x=es[i].x,y=es[i].y,l=es[i].l; if ((fx=getfather(x))!=(fy=getfather(y))) { fa[fy]=fx; insert(x,y,l),insert(y,x,l); ans+=l; } }}queue<int> q;void bfs(){ q.push(1); int x,i,y; for (;!q.empty();) { x=q.front(),q.pop(); for (i=last[x];i;i=next[i]) if ((y=tov[i])!=f[x][0]) f[y][0]=x,g[y][0]=len[i],high[y]=high[x]+1,q.push(y); }}void pre(){ lgn=trunc(log(n)/log(2))+1; for (int j=1;j<=lgn;j++) for (int i=1;i<=n;i++) f[i][j]=f[f[i][j-1]][j-1],g[i][j]=max(g[i][j-1],g[f[i][j-1]][j-1]);}int query(int x,int y){ int ret=0; if (high[y]<high[x]) swap(x,y); for (int i=lgn;i>=0;i--) if (high[f[y][i]]>=high[x]) ret=max(ret,g[y][i]),y=f[y][i]; if (x==y) return ret; for (int i=lgn;i>=0;i--) if (f[x][i]!=f[y][i]) ret=max(ret,max(g[x][i],g[y][i])),x=f[x][i],y=f[y][i]; ret=max(ret,max(g[x][0],g[y][0])); return ret;}int main(){ freopen("street.in","r",stdin),freopen("street.out","w",stdout); n=read(),m=read(); for (int i=1;i<=m;i++) es[i].x=read(),es[i].y=read(),es[i].l=read(),og[i]=es[i]; Kruscal(),bfs(),pre(); for (int i=1;i<=m;i++) { int cut=query(og[i].x,og[i].y); printf("%lld\n",ans-cut+og[i].l); } fclose(stdin),fclose(stdout); return 0;}
0 0
- [JZOJ4596]street
- 【JZOJ4596】Stree
- Street
- [JZOJ4596] Stree(t)
- Street Numbers
- Street Race
- Seattle Street
- Street Lamps
- 关于itil_03_State Street
- Ford Street Racing 2006
- Ford Street Racing (Reloaded)
- usaco 4.3 Street Race
- Section 4.3 Street Race
- 铁拳6 & Street fight4
- PKU1172 street race
- uva 138 - Street Numbers
- uva 138 street numbers
- UVA 138 Street Numbers
- clearfix清除浮动方案
- Centos6.5使用yum安装mysql
- android系统文件的权限
- 李葆文教授出席第二届全国设备管理创新成果交流大会并做专题报告
- Android通讯录字母排序城市列表展示效果
- [JZOJ4596]street
- python flask框架 app.debug=True时,启动脚本被执行了2次
- 打印机的工作原理
- 齐次坐标问题
- NodeJS中REPL 命令
- cannot find -luuid
- [android]手把手通过一个类实现侧滑退出activity功能
- JAVA设计模式之原型模式
- EasyUI ComboGrid 集成分页、按键示例