CF609E. Minimum spanning tree for each edge

题解:随便构造一颗最小生成树 然后对于其他不在树上的边  考虑到 删除这条链上的最大值在把这条边加上去 能得到这条边所在的最小生成树 可以LCT维护 但是明显这个题是静态的树就没必要LCT 当然我觉得最优的是树剖以后ST nlogn的的复杂度 也可以树剖+线段树nlog^2的复杂度

#include <bits/stdc++.h>
const int MAXN=2e5+10;
#define ll long long
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch==‘-‘)f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-‘0‘,ch=getchar();
    return f*x;
}
typedef struct Edge{
	int u,v,vul,id;
	friend bool operator<(Edge aa,Edge bb){
		return aa.vul<bb.vul;
	}
}Edge;
Edge ed[MAXN];int mu[MAXN],ma[21];
int f[MAXN];ll ans[MAXN];
int n,m;
int find1(int x){
	if(f[x]==x)return x;
	return f[x]=find1(f[x]);
}
vector<Edge>v1;
vector<Edge>v2;
vector<pair<int,int> >vec[MAXN];
int fa[MAXN],num[MAXN],dep[MAXN],son[MAXN];
int dp[MAXN][21],vul[MAXN];
void dfs(int v,int pre,int deep){
	fa[v]=pre;num[v]=1;dep[v]=deep+1;
	for(int i=0;i<vec[v].size();i++){
		if(vec[v][i].first!=pre){
			//F[vec[v][i].first][0]=pre;
			vul[vec[v][i].first]=vec[v][i].second;
			dfs(vec[v][i].first,v,deep+1);
			num[v]+=num[vec[v][i].first];
			if(son[v]==-1||num[vec[v][i].first]>num[son[v]])son[v]=vec[v][i].first;
		}
	}
}
int tp[MAXN],p[MAXN],fp[MAXN],cnt;
void dfs1(int v,int td){
	tp[v]=td;p[v]=++cnt;fp[p[v]]=v;
	if(son[v]!=-1)dfs1(son[v],td);
	for(int i=0;i<vec[v].size();i++){
		if(vec[v][i].first!=fa[v]&&vec[v][i].first!=son[v])dfs1(vec[v][i].first,vec[v][i].first);
	}
}
void st(){
	for(int i=1;i<=n;i++)dp[i][0]=vul[fp[i]];
	for(int j=1;ma[j]<=n;j++){
		for(int i=1;i<=n;i++){
			dp[i][j]=max(dp[i][j-1],dp[i+ma[j-1]][j-1]);
		}
	}
}
int rmq(int l,int r){
	int k=mu[r-l+1];
	return max(dp[l][k],dp[r-ma[k]+1][k]);
}
int Lca(int u,int v){
	int maxx=-1;
	int uu=tp[u];int vv=tp[v];
	while(uu!=vv){
		if(dep[uu]<dep[vv])swap(uu,vv),swap(u,v);
		maxx=max(maxx,rmq(p[uu],p[u]));
		u=fa[uu];uu=tp[u];
	}
	if(dep[u]>dep[v])swap(u,v);
	if(u!=v)maxx=max(maxx,rmq(p[son[u]],p[v]));
	return maxx;
}
int main(){
	ma[0]=1;
	for(int i=1;i<21;i++)ma[i]=ma[i-1]<<1;
	mu[0]=-1;
	for(int i=1;i<MAXN;i++){
    if((i&(i-1))==0)mu[i]=mu[i-1]+1;
    else mu[i]=mu[i-1];
    }
	n=read();m=read();
	for(int i=1;i<=n;i++)f[i]=i,son[i]=-1;
	for(int i=1;i<=m;i++)ed[i].id=i,ed[i].u=read(),ed[i].v=read(),ed[i].vul=read();
	sort(ed+1,ed+m+1);ll sum=0;
	for(int i=1;i<=m;i++){
		int t1=find1(ed[i].u);int t2=find1(ed[i].v);
		if(t1!=t2){
			f[t1]=t2;sum+=ed[i].vul;
			v2.push_back(ed[i]);
			vec[ed[i].u].push_back(make_pair(ed[i].v,ed[i].vul));
			vec[ed[i].v].push_back(make_pair(ed[i].u,ed[i].vul));
		}
		else v1.push_back(ed[i]);
	}
	dfs(1,0,0);dfs1(1,1);
	st();
	for(int i=0;i<v2.size();i++)ans[v2[i].id]=sum;
	for(int i=0;i<v1.size();i++){
		int key=Lca(v1[i].u,v1[i].v);
	//	cout<<key<<endl;
		ans[v1[i].id]=sum-key+v1[i].vul;
	}
	for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
	return 0;
}

  

              E. Minimum spanning tree for each edge

                  time limit per test

                  2 seconds

                  memory limit per test

                  256 megabytes

input

standard input

output

standard output

Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains n vertices and m edges.

For each edge (u,?v) find the minimal possible weight of the spanning tree that contains the edge (u,?v).

The weight of the spanning tree is the sum of weights of all edges included in spanning tree.

Input

First line contains two integers n and m (1?≤?n?≤?2·105,?n?-?1?≤?m?≤?2·105) — the number of vertices and edges in graph.

Each of the next m lines contains three integers ui,?vi,?wi (1?≤?ui,?vi?≤?n,?ui?≠?vi,?1?≤?wi?≤?109) — the endpoints of the i-th edge and its weight.

Output

Print m lines. i-th line should contain the minimal possible weight of the spanning tree that contains i-th edge.

The edges are numbered from 1 to m in order of their appearing in input.

Examples

input

Copy

5 71 2 31 3 11 4 52 3 22 5 33 4 24 5 4

output

Copy

98118889

原文地址:https://www.cnblogs.com/wang9897/p/9195843.html

时间: 2024-11-10 13:34:17

CF609E. Minimum spanning tree for each edge的相关文章

Minimum spanning tree for each edge

Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains n vertices and m edges. For each edge (u,?v) find the minimal possible weight of the spanning tree that contains the edge (u,?v). The weight of the spa

Educational Codeforces Round 3 E (609E) Minimum spanning tree for each edge

题意:一个无向图联通中,求包含每条边的最小生成树的值(无自环,无重边) 分析:求出这个图的最小生成树,用最小生成树上的边建图 对于每条边,不外乎两种情况 1:该边就是最小生成树上的边,那么答案显然 2:该边不在最小生成树上,那么进行路径查询,假设加入这条边,那么形成一个环,删去这个环上除该边外的最大权值边,形成一棵树 树的权值即为答案.(并不需要真正加入这条边) 注:其实用树链剖分和LCA都可以,选择自己熟悉的写就行,我写的树链剖分 #include<cstdio> #include<c

cf 609E.Minimum spanning tree for each edge

最小生成树,lca(树链剖分(太难搞,不会写)) 问存在这条边的最小生成树,2种情况.1.这条边在原始最小生成树上.2.加上这条半形成一个环(加上),那么就找原来这条边2端点间的最大边就好(减去).(sum+val-max) (代码冗长) 1 #include<bits/stdc++.h> 2 #define LL long long 3 #define N 100005 4 using namespace std; 5 inline int ra() 6 { 7 int x=0,f=1; c

【HDU 4408】Minimum Spanning Tree(最小生成树计数)

Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX finds that there might be multiple solutions. Given an undirected weighted graph with n (1<=n<=100) vertex

HDOJ 题目4408 Minimum Spanning Tree(Kruskal+Matrix_Tree)

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1408    Accepted Submission(s): 450 Problem Description XXX is very interested in algorithm. After learning the Prim algori

Geeks : Kruskal’s Minimum Spanning Tree Algorithm 最小生成树

寻找图中最小连通的路径,图如下: 算法步骤: 1. Sort all the edges in non-decreasing order of their weight. 2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it. 3. Repeat st

HDU 4408 Minimum Spanning Tree 最小生成树计数

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX

[思维]Minimum Spanning Tree

题目描述 In the mathematical discipline of graph theory, the line graph of a simple undirected weighted graph G is another simple undirected weighted graph L(G) that represents the adjacency between every two edges in G. Precisely speaking, for an undire

【算法】关于图论中的最小生成树(Minimum Spanning Tree)详解

喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 本节纲要 什么是图(network) 什么是最小生成树 (minimum spanning tree) 最小生成树的算法 什么是图(network)? 这里的图当然不是我们日常说的图片或者地图.通常情况下,我们把图看成是一种由"顶点"和"边"组成的抽象网络.在各个"顶点"间可以由"边"连接起来,使两个顶点间相互关联起来.图的结构可以描述多种复杂的数据对象,