Codeforces 827 D 最小生成树+倍增 解题报告

来源:互联网 发布:极乐净土道长数据 编辑:程序博客网 时间:2024/06/05 20:59

D. Best Edge Weight

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·105, n - 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 u, v and c (1 ≤ v, u ≤ n, v ≠ u, 1 ≤ 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.若一条边不在生成树上,这条边的两端肯定在树上构成一条链。
若这条边替代树上的一条链,则权值最大必须小于链上的边的最大权值。
那么显然这个点的最大权值就是链上最大边权-1。
2.若一条边在生成树上,则还是看刚才每个不在生成树上的边和生成树构成的那个环:
对于每个不在生成树上的边,假设从u到v,那么它能替代的边就为生成树上u到v路径上的所有边。
这时,u到v的生成树路径上的所有边,权值必须小于这条边,否则就要被替代。

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

代码如下:

#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;#define N 200010#define inf 0x3f3f3f3fint n,m,num;int f[N],mxlen[N][20],mnlen[N][20],dep[N],fa[N][20],ans[N];bool use[N],visit[N];vector <int> v[N];vector <int> d[N];struct Edge {    int from,to,dist,id;    friend bool operator < (const Edge &a,const Edge &b)    {return a.dist<b.dist;}}edge[N<<1];int find(int x) {    return(f[x]==x)?x:f[x]=find(f[x]);}void kruskal(int n,int m) {    int cnt=0;    memset(use,0,sizeof(use));    for(int i=1;i<=n;++i) f[i]=i;    for(int 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() {//  freopen("tree.in","r",stdin);//  freopen("tree.out","w",stdout);    num=0;    scanf("%d%d",&n,&m);    for(int 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);    kruskal(n,m);    memset(visit,0,sizeof(visit));    fa[1][0]=0;dep[0]=-1;    dfs(1,0);    for(int j=1;j<=18;++j)     for(int 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]);    }    memset(ans,inf,sizeof(ans));    memset(mnlen,inf,sizeof(mnlen));    for(int 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(int j=17;j>=0;--j)     for(int 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(int 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(int i=1;i<=m;++i)     {        if(ans[i]==inf) printf("-1 ");        else printf("%d ",ans[i]);    }    return 0;}
原创粉丝点击