频道栏目
首页 > 资讯 > C++ > 正文

HDOJ 4612 Warm up

14-04-27        来源:[db:作者]  
收藏   我要投稿

缩点+树的直径+扩栈

Warm up

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3140 Accepted Submission(s): 715


Problem Description   N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.
  If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
  Note that there could be more than one channel between two planets.

Input   The input contains multiple cases.
  Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
  (2<=N<=200000, 1<=M<=1000000)
  Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
  A line with two integers '0' terminates the input.
Output   For each case, output the minimal number of bridges after building a new channel in a line.
Sample Input
4 4
1 2
1 3
1 4
2 3
0 0 

Sample Output
0

Source 2013 Multi-University Training Contest 2

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const int maxn=220000;

struct Edge
{
    int from,to,next,id;
}edge[maxn*10],edge2[maxn*10];

int Adj[maxn],Size,n,m;
int Adj2[maxn],Size2;

void init()
{
    Size=0;
    memset(Adj,-1,sizeof(Adj));
}

void init2()
{
    Size2=0;
    memset(Adj2,-1,sizeof(Adj2));
}

void Add_Edge(int u,int v,int id)
{
    edge[Size].from=u;
    edge[Size].id=id;
    edge[Size].to=v;
    edge[Size].next=Adj[u];
    Adj[u]=Size++;
}

void Add_Edge2(int u,int v)
{
    edge2[Size2].from=u;
    edge2[Size2].to=v;
    edge2[Size2].next=Adj2[u];
    Adj2[u]=Size2++;
}

int Low[maxn],DFN[maxn],Stack[maxn],Belong[maxn];
int Index,top,scc;
bool Instack[maxn],vis[maxn],ve[maxn*10];

void Tarjan(int u,int fa)
{
    int v;
    Low[u]=DFN[u]=++Index;
    Stack[top++]=u;
    Instack[u]=true;

    for(int i=Adj[u];~i;i=edge[i].next)
    {
        v=edge[i].to;
        if(v==fa&&ve[edge[i].id]) continue;
        ve[edge[i].id]=true;
        if(!DFN[v])
        {
            Tarjan(v,u);
            Low[u]=min(Low[u],Low[v]);
        }
        else
        {
            Low[u]=min(Low[u],DFN[v]);
        }
    }

    if(Low[u]==DFN[u])
    {
        scc++;
        do
        {
            v=Stack[--top];
            Belong[v]=scc;
            Instack[v]=false;
        }while(v!=u);
    }
}

void scc_solve()
{
    memset(DFN,0,sizeof(DFN));
    memset(Instack,0,sizeof(Instack));

    Index=scc=top=0;
    memset(ve,0,sizeof(ve));
    for(int i=1;i<=n;i++)
    {
        if(!DFN[i]) Tarjan(i,i);
    }
}

struct PT
{
    int p,d;
    PT() {}
    PT(int a,int b):p(a),d(b) {}
};

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;
        init();
        for(int i=0;i q;
        memset(vis,0,sizeof(vis));
        q.push(stp); vis[stp.p]=true;
        while(!q.empty())
        {
            PT u,v;
            u=q.front(); q.pop();
            edp=u;
            for(int i=Adj2[u.p];~i;i=edge2[i].next)
            {
                v.p=edge2[i].to; v.d=u.d+1;
                if(vis[v.p]) continue;
                vis[v.p]=true;
                q.push(v);
            }
        }
        while(!q.empty()) q.pop();
        stp.p=-1,stp.d=-1;
        memset(vis,0,sizeof(vis));
        vis[edp.p]=true; edp.d=0;
        q.push(edp);
        while(!q.empty())
        {
            PT u,v;
            u=q.front(); q.pop();
            stp=u;
            for(int i=Adj2[u.p];~i;i=edge2[i].next)
            {
                v.p=edge2[i].to; v.d=u.d+1;
                if(vis[v.p]) continue;
                vis[v.p]=true;
                q.push(v);
            }
        }
        printf("%d\n",ans-stp.d);
    }
    return 0;
}




相关TAG标签
上一篇:poj2063 Investment 完全背包
下一篇:POJ3253 Fence Repair (二叉堆)
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站