1. 程式人生 > >Codeforces 827D (Round #423 Div. 1) D. Best Edge Weight 樹上倍增

Codeforces 827D (Round #423 Div. 1) D. Best Edge Weight 樹上倍增

D. Best Edge Weight time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

You are given a connected weighted graph with n vertices and m edges. The graph doesn't contain loops nor multiple edges. Consider some edge with id i. Let's determine for this edge the maximum integer weight we can give to it so that it is contained in all minimum spanning trees of the graph if we don't change the other weights.

You are to determine this maximum weight described above for each edge. You should calculate the answer for each edge independently, it means there can't be two edges with changed weights at the same time.

Input

The first line contains two integers n and m (2 ≤ n ≤ 2·105n - 1 ≤ m ≤ 2·105), where n

 and m are the number of vertices and the number of edges in the graph, respectively.

Each of the next m lines contains three integers uv and c (1 ≤ v, u ≤ nv ≠ u1 ≤ c ≤ 109) meaning that there is an edge between vertices u and v with weight c.

Output

Print the answer for each edge in the order the edges are given in the input. If an edge is contained in every minimum spanning tree with any weight, print -1

 as the answer.

Examples input
4 4
1 2 2
2 3 2
3 4 2
4 1 3
output
2 2 2 1 
input
4 3
1 2 2
2 3 2
3 4 2
output
-1 -1 -1 

給了一個圖,問你當每條邊的長度最大為多少的時候可以成為最小生成樹中的一條邊。

先求最小生成樹。

求完之後,分兩種情況討論:

1.若一條邊不在生成樹上,這條邊肯定與生成樹上的邊共同構成了一個環。

若這條邊替代環上的一條邊,則權值最大必須小於環上的邊的最大權值。

2.若一條邊在生成樹上,則還是看剛才每個不在生成樹上的邊和生成樹構成的那個環:

對於每個不在生成樹上的邊,假設從u到v,那麼它能替代的邊就為生成樹上u到v路徑上的所有邊。

這時,u到v的生成樹路徑上的所有邊,權值必須小於這條邊,否則就要被替代。

最大和最小權值,都可以利用倍增演算法在樹上更新。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=200005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);  
int f[maxn],mxlen[maxn][20],mnlen[maxn][20],dep[maxn],fa[maxn][20],ans[maxn];
bool use[maxn],visit[maxn];
int num;
vector<int> v[maxn];
vector<int> d[maxn];

struct Edge {
	int from,to,dist,id;
};
Edge edge[maxn*2];

bool cmp(Edge a,Edge b) {
	return a.dist<b.dist; 
} 

int find(int x) {
	if (f[x]==x) return x; else {
		f[x]=find(f[x]);
		return f[x];
	}
}

void kruskal(int n,int m) {
	int cnt=0,i;
	for (i=1;i<=n;i++) f[i]=i;
	mem0(use);
	for (i=1;i<=m;i++) {
		int from=edge[i].from,to=edge[i].to;
		int fa=find(from),fb=find(to);
		if (fa!=fb) {
			cnt++;use[i]=1;
			v[from].push_back(to);
			v[to].push_back(from);
			d[from].push_back(edge[i].dist);
			d[to].push_back(edge[i].dist);
			if (cnt==n-1) return;
			f[fa]=fb;
		}
	}
}

void dfs(int now,int step) {
	visit[now]=1;dep[now]=step;
	for (int i=0;i<v[now].size();i++) {
		int to=v[now][i];
		if (!visit[to]) {
			fa[to][0]=now;mxlen[to][0]=d[now][i];
			dfs(to,step+1);
		}
	}
}

int getmaxlen(int x,int y) {
	if (dep[x]<dep[y]) swap(x,y);
	int i,ans=-inf;
	for (i=18;i>=0;i--) {
		if (dep[fa[x][i]]>=dep[y]) {
			ans=max(ans,mxlen[x][i]);
			x=fa[x][i]; 
		}
	}
	if (x==y) return ans;
	for (i=18;i>=0;i--) {
		if (fa[x][i]!=fa[y][i]) {
			ans=max(ans,max(mxlen[x][i],mxlen[y][i]));
			x=fa[x][i];y=fa[y][i];
		}
	}
	ans=max(ans,max(mxlen[x][0],mxlen[y][0]));
	return ans;
}

void update(int x,int y,int val) {
	if (dep[x]<dep[y]) swap(x,y);
	int i;
	for (i=18;i>=0;i--) {
		if (dep[fa[x][i]]>=dep[y]) {
			mnlen[x][i]=min(val,mnlen[x][i]);
			x=fa[x][i]; 
		}
	}
	if (x==y) return;
	for (i=18;i>=0;i--) {
		if (fa[x][i]!=fa[y][i]) {
			mnlen[x][i]=min(mnlen[x][i],val);
			mnlen[y][i]=min(mnlen[y][i],val);
			x=fa[x][i];y=fa[y][i];
		}
	}
	mnlen[x][0]=min(mnlen[x][0],val);
	mnlen[y][0]=min(mnlen[y][0],val);
}

int main() {
	num=0;
	int n,m,i,j,x,y,d;
	scanf("%d%d",&n,&m);
	for (i=1;i<=m;i++) {
		scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].dist);
		edge[i].id=i;
	}
	sort(edge+1,edge+m+1,cmp);
	kruskal(n,m);
	mem0(visit);
	fa[1][0]=0;dep[0]=-1;
	dfs(1,0);    //binary lifts
	for (j=1;j<=18;j++) {
		for (i=1;i<=n;i++) {
			fa[i][j]=fa[fa[i][j-1]][j-1];
			mxlen[i][j]=max(mxlen[i][j-1],mxlen[fa[i][j-1]][j-1]);
		}
	}
	meminf(ans);meminf(mnlen);
	for (i=1;i<=m;i++) {
		if (!use[i]) {
			ans[edge[i].id]=getmaxlen(edge[i].from,edge[i].to)-1;
			update(edge[i].from,edge[i].to,edge[i].dist-1);
		}
	}
	for (j=17;j>=0;j--) {
		for (i=1;i<=n;i++) {
			mnlen[i][j]=min(mnlen[i][j],mnlen[i][j+1]);
			mnlen[fa[i][j]][j]=min(mnlen[fa[i][j]][j],mnlen[i][j+1]);
		}
	}
	for (i=1;i<=m;i++) {
		if (use[i]) 
		    if (fa[edge[i].from][0]==edge[i].to) 
		        ans[edge[i].id]=mnlen[edge[i].from][0];
		    else 
		        ans[edge[i].id]=mnlen[edge[i].to][0];
	}
	for (i=1;i<=m;i++) {
		if (ans[i]==inf) printf("-1 "); else printf("%d ",ans[i]);
	}
	return 0;
}