Caocao's Bridges-- bridge template title

Topic Link

Meaning of the questions:

M n points gives no edges minimum value to FIG bridge, output right

answer:

Bridge template title

If no communication has been directed graph is not itself, and direct the output to 0 (disjoint-set with a judgment)

Because of a weight of zero, so if the right to seek out the bridge minimum weight value is 0, meaning of the questions, the output 1.

Note handling heavy side

Code:

#include <stdio.h> 
#include < String .h> 
#include <the iostream> 
#include <algorithm> 
#include <Vector> 
#include <Map>
 the using  namespace STD; 
typedef Long  Long LL;
 const  int INF = 0x3f3f3f3f ;
 const  int MAXN = + 1E4 . 5 ; // points 
const  int MAXM = 2E6 + . 5 ; // number of sides, as is the undirected graph, so this value for 2 * 

struct edge 
{ 
    int to, Next;
    int W;
     BOOL Cut; // whether the flag bridge 
} Edge [MAXM];
 int head [MAXN], TOT;
 int Low [MAXN], DFN [MAXN], Stack [MAXN], belong [MAXN]; // belong array values are the SCC ~. 1 
int Index, Top;
 int SCC; // number double the number of sides of the communication blocks / the strongly connected components 
BOOL Instack [MAXN];
 int bridge; // number of bridges 
int Cut [MAXN];
 int NUM [MAXN];
 void addedge ( int U, int V, int W) 
{ 
    Edge [TOT] .to = V; 
    Edge [TOT] .next= head[u];
    edge[tot].cut=false;
    edge[tot].w=w;
    head[u] = tot++;
}

void Tarjan(int u,int pre)
{
    int v;
    low[u] = dfn[u] = ++Index;
    Stack[top++] = u;
    Instack[u] = true;
    int son=0;
    int flag=0;
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].to;
        if(v == pre && !flag)
        {
            flag++;
            continue;
        }

        if( !dfn[v] )
        {
            son++;
            Tarjan(v,u);
            if( low[u] > low[v] )low[u] = low[v];

            if(low[v] > dfn[u])
            {
                bridge++;
                edge[i].cut = true;
                edge[i^1].cut = true;
            }

            if(u == pre && son > 1)cut[u] = true;
            if(u != pre && low[v] >= dfn[u])cut[u] = true;

        }
        else if( Instack[v] && low[u] > dfn[v] )
            low[u] = dfn[v];
    }

    if(low[u] == dfn[u])
    {
        scc++;
        do
        {
            v = Stack[--top];
            Instack[v] = false;
            belong[v] = scc;
            num[scc]++;

        }
        while( v!=u );
    }

}

void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}
int solve(int n)
{
    memset(dfn,0,sizeof(dfn));
    memset(Instack,false,sizeof(Instack));
    memset(cut,0,sizeof cut);
    memset(num,0,sizeof num);
    Index = top = scc = 0;
    bridge = 0;
    for(int i = 1; i <= n; i++)
        if(!dfn[i])
            Tarjan(i,i);
    int res=inf;
    for(int u=1;u<=n;u++)
    {
        for(int i=head[u];~i;i=edge[i].next)
            if(edge[i].cut)res=min(res,edge[i].w);
    }
    if(res==inf)return -1;
    if(res==0)return 1;
    return res;

}
int pre[maxn];
int Find(int x)
{
    return x==pre[x]?x:pre[x]=Find(pre[x]);
}
int join(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)pre[fx]=fy;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m) && (n&&m))
    {
        init();
        for(int i=1; i<=n; i++)pre[i]=i;
        for(int i=1; i<=m; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
            join(u,v);
        }

        int flag=0;
        for(int i=1; i<=n; i++)
            if(Find(i)!=Find(1))flag=1;
        if(flag)printf("0\n");
        else
        {
            printf("%d\n",solve(n));
        }

    }
    return 0;
}
/*
3 3
1 2 7
2 3 4
3 1 4
3 2
1 2 7
2 3 4
0 0
*/
View Code

 

Guess you like

Origin www.cnblogs.com/j666/p/11688796.html