HDU 4005 the war

Source: Internet
Author: User

Question:

Adding an edge to an undirected graph maximizes the edge weight of all bridges.


Ideas:

First, perform edge dual-connectivity and then scale down the point. Because there is no bridge point in the dual-connectivity component, the graph becomes a tree.

Build the tree and use the tree-like DP to process the answer. The solution is to find the minimum edge in the tree and disconnect it to form a two-pronged structure.

Now we need to maintain a path in each of the two trees so that the answers are always directed to the Child tree with the smallest Edge Weight.

Make DP [I] to indicate that the path will appear after the minimum edge weight in the sub-tree with I as the root, and then take the Dp value that is not in the path to the minimum value.


Note:

The variable names in the graph and tree should be as much different as possible (I just wrote the variable name wrong for dozens of times...

If the graph is not connected, you need to output-1 directly (this is obviously a poor question... In principle, the minimum value of all edges should be output.

The entire graph is a dual-connected component that requires special output-1


Code:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 10010#define M 100010#define inf 1000000struct edge{    int u,v,w,next,flag,bridge;}ed[M*2];struct tree{    int u,v,w,next;}te[M*2];int head[N],dfn[N],low[N],belong[N],hte[N],dp[N],to[N];int n,m,tot,idx,cnt,tol,ans;void init(){    for(int i=1;i<=n;i++)    {        head[i]=dfn[i]=hte[i]=-1;        belong[i]=to[i]=0;        dp[i]=inf;    }    tot=idx=cnt=tol=0;    ans=inf;}void add(int u,int v,int w){    ed[tot].u=u;    ed[tot].v=v;    ed[tot].w=w;    ed[tot].next=head[u];    head[u]=tot;    ed[tot].flag=0;    ed[tot].bridge=0;    tot++;}void tarjan(int u){    int i,v,num=0;    dfn[u]=low[u]=++idx;    for(i=head[u];~i;i=ed[i].next)    {        v=ed[i].v;        if(ed[i].flag) continue;        ed[i].flag=ed[i^1].flag=1;        if(dfn[v]==-1)        {            tarjan(v);            low[u]=min(low[u],low[v]);            if(dfn[u]<low[v])            {                ed[i].bridge=1;                ed[i^1].bridge=1;            }        }        else low[u]=min(low[u],dfn[v]);    }}bool solve(){    int i;    tarjan(1);    for(i=1;i<=n;i++)    {        if(dfn[i]==-1) return false;    }    return true;}void color(int u){    int i;    belong[u]=cnt;    for(i=head[u];~i;i=ed[i].next)    {        if(ed[i].bridge) continue;        if(!belong[ed[i].v]) color(ed[i].v);    }}void addedge(int u,int v,int w){    te[tol].u=u;    te[tol].v=v;    te[tol].w=w;    te[tol].next=hte[u];    hte[u]=tol++;}int maketree(){    int i,u,v,res=inf;    for(i=1;i<=n;i++)    {        if(!belong[i])        {            cnt++;            color(i);        }    }    for(i=0;i<tot;i+=2)    {        if(ed[i].bridge)        {            u=belong[ed[i].u];            v=belong[ed[i].v];            addedge(u,v,ed[i].w);            addedge(v,u,ed[i].w);            res=min(res,ed[i].w);        }    }    return res;}void makedp(int u,int fa){    int i,v,tmp;    for(i=hte[u];~i;i=te[i].next)    {        v=te[i].v;        if(v==fa) continue;        makedp(v,u);        tmp=min(dp[v],te[i].w);        if(dp[u]>tmp)        {            dp[u]=tmp;            to[u]=v;        }    }}void findans(int u,int fa){    int i,v,tmp;    for(i=hte[u];~i;i=te[i].next)    {        v=te[i].v;        if(v==fa||v==to[u]) continue;        tmp=min(dp[v],te[i].w);        ans=min(ans,tmp);    }    if(to[u]) findans(to[u],u);}int main(){    int i,u,v,w,minw;    while(~scanf("%d%d",&n,&m))    {        init();        for(i=1;i<=m;i++)        {            scanf("%d%d%d",&u,&v,&w);            add(u,v,w);            add(v,u,w);        }        if(!solve())        {            puts("-1");            continue;        }        minw=maketree();        if(minw==inf)        {            puts("-1");            continue;        }        for(i=0;i<tol;i+=2)        {            if(te[i].w==minw)            {                makedp(te[i].u,te[i].v);                makedp(te[i].v,te[i].u);                findans(te[i].u,te[i].v);                findans(te[i].v,te[i].u);                break;            }        }        if(ans!=inf) printf("%d\n",ans);        else puts("-1");    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.