hdu3394 Railway

Subject to the effect that a undirected graph is determined how many cutting edges and the number of conflicts sides

A side edge of the conflict, the instructions to two or more rings connected component of the presence of a double edge belongs. That is, if two or more rings in the presence of a dual communication component, while the component of the dual communication conflicts all sides.

Cutting edge good demand, now the problem is that the processing side of the conflict.

Later found that "more than two rings connected component in the presence of a double" condition can be translated as "double-side communication component is greater than the number of points" (Reference https://blog.csdn.net/u014141559/article/details/45371373)

After seeking dual-connected components and board which we use stack deposit side, each connected component found double stack pop side of it is double the number of edges connected component.

Then statistics point only when the number of edges> points ans + = sum_edge

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=10010;
struct Edge
{
    you and, v;
    Edge(){}
    Edge(int u,int v):u(u),v(v){}
}s[100010];
int M,N,dfn[maxn],low[maxn],cnt=0,ans1=0,ans2=0,Index=0,belong[maxn],cnt_bcc=0;
vector<int> edges[maxn];
void addedge(int u,int v)
{
    edges[u].push_back(v);
    edges[v].push_back(u);
}
void tarjan ( you and, you fa)
{
    dfn[u]=low[u]=++cnt;
    for(int i=0;i<edges[u].size();i++)
    {
        int v=edges[u][i];
        if(!dfn[v])
        {
            s[++Index]=Edge(u,v);
            tarjan (v, u);
            low[u]=min(low[u],low[v]);
            if(low[v]>=dfn[u])
            {
                if(low[v]>dfn[u]) ans1++;
                int sum_edge=0,sum_node=0;
                cnt_bcc++;
                for(Edge e;;)
                {
                    e=s[Index--];
                    sum_edge++;
                    if(belong[e.u]!=cnt_bcc){belong[e.u]=cnt_bcc;sum_node++;}
                    if(belong[e.v]!=cnt_bcc){belong[e.v]=cnt_bcc;sum_node++;}
                    if(e.u==u&&e.v==v) break;
                }
                if(sum_node<sum_edge) ans2+=sum_edge;
            }
        }
        else if(dfn[v]<dfn[u]&&fa!=v)
        {
            s[++Index]=Edge(u,v);
            low[u]=min(low[u],dfn[v]);
        }
    }
}
void init()
{
    for(int i=1;i<=N;i++)
        edges[i].clear();
    memset(dfn,0,sizeof dfn);
    memset(belong,0,sizeof belong);
    cnt=0;cnt_bcc=0;ans1=ans2=0;
}
int main ()
{
    while(true)
    {
        scanf("%d%d",&N,&M);
        if(N==0&&M==0) break;
        init();
        for(int i=1;i<=M;i++)
        {
            int u,v;scanf("%d%d",&u,&v);
            addedge (u + 1 , v + 1 );
        }
        for(int i=1;i<=N;i++)
            if(!dfn[i]) tarjan(i,0);
        printf("%d %d\n",ans1,ans2);
    }
}

There is a pit, that is, the side from 0- (N-1) for this purpose tle several times (Why not wa)

Guess you like

Origin www.cnblogs.com/tmzengbi/p/11769573.html