[Kuangbin take you to fly] thematic nine connected graph E POJ 3177 Redundant Paths

I think this question is the beginning, direct point shrinkage of Double connected component, connect these two-component Unicom is not on line yet?

But is not correct, double double Unicom internal communication, if any of us an edge connection between these two-component Unicom, there is no bridge between them do not really know.

I should be seeking points after shrinking the number of leaf nodes, because it leaves for the festival itself, there is only one bridge connected it, we can connect two leaf nodes. Then the two regions to synthesize a two component Unicom region. Meanwhile, in order to reduce the use side, we can connect the two points are the leaf nodes. Finally +1, and the rest should be a connection. This allows very easy to FIG calculated into double communication, the number of edges required.

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int N = 1e4+5,M = 1e5+5;
int head[N],Next[M],ver[M],low[M],dfn[M],stack[M];
int block;
int belong[M],deg[M];
bool brige[M],ins[M];
int c[M],dcc,top;
int tot,n,m,num,cnt;
void add(int x,int y)
{
    ver[++tot]=y;
    Next[tot]=head[x];
    head[x]=tot;
}
void tarjan(int u,int pre)
{
    int v;
    dfn[u]=low[u]=++num;
    stack[++top]=u;
    ins[u]=1;
    for (int i=head[u]; i; i=Next[i])
    {
         v=ver[i];
        if (v==pre)continue;
        if (!dfn[v])
        {
            tarjan(v,u);
            Low [U]=min (Low [U], Low [V]);
             IF (Low [V]> DFN [U]) 
            { 
                brige [I] = . 1 ; // marked bridge 
                brige [I ^ . 1 ] = . 1 ; 
                CNT ++ ; 
            } 
        } 
        the else Low [U] = min (Low [U], DFN [V]); 
    } 
    IF (Low [U] == DFN [U]) { // if the current node is a root that is, we DFS connected components after this 
        CNT ++ ;
         do 
        { 
            V = Stack [top--]; // to all points of the connected component are inside out
            ins[v]=0;
            c[v]=cnt;//标记上所在的连通分量
        }while(u!=v);
    }
}
void init(){
   memset(Next,0,sizeof(Next));
   memset(low,0,sizeof(low));
   memset(brige,0,sizeof(brige));
   memset(head,0,sizeof(head));
   memset(ver,0,sizeof(ver));
   memset(dfn,0,sizeof(dfn));
   memset(c,0,sizeof(c));
   tot=1;
   cnt=0;
   num=0;
   dcc=0;
   top=0;
}
int main()
{
    int u,v;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for (int i=1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        for (int i=1;i<=n;i++){
            if (!dfn[i]){
                tarjan(i,i);
            }
        }
        for (int i=1;i<=n;i++){
            for (int j=head[i];j;j=Next[j])
            {
                  if (brige[j])   //缩点后把桥两边的缩点的度++
                    deg[c[i]]++; 
            }
        } 
        Int ANS = 0 ;
         for ( int I = 1 ; I <= CNT; I ++ ) {
             IF (deg [I] == 1 ) { // We find the condensing point of the point 1, i.e. leaves node, 
                ANS ++ ; 
            } 
        } 
        the printf ( " % D \ n- " , (+ ANS . 1 ) / 2 ); // (+1 leaf node number after condensing point) then the desired double side view of the connector component Unicom = / 2
         // because it is simple I put the leaf nodes connected two by two up, then this edge is consumed the least. So that we can make drawing
         // turns into a double Unicom 
     }
     return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/bluefly-hrbust/p/11229408.html