hdu4612——Warm up

62 篇文章 0 订阅
16 篇文章 2 订阅

Warm up

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


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
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:   5065  5064  5062  5061  5060 
 

先 tarjan然后缩点得到一颗树,求树的直径就好了


#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;

const int N = 200010;
const int inf = 0x3f3f3f3f;
const int M = 1000010;

struct node
{
    int next;
    int to;
    int id;
}edge[M], edge2[M];

bool vis[N];
bool flag[N];
int dist[N];
int head[N];
int head2[N];
int low[N];
int block[N];
int DFN[N];
bool instack[N];
stack<int>qu;
int br_cnt, n, m, end_p;
int tot, tot2, maxs, index, top, sccnum;

void addedge(int from, int to, int id)
{
    edge[tot].id = id;
    edge[tot].to = to;
    edge[tot].next = head[from];
    head[from] = tot++;
}

void addedge2(int from, int to)
{
    edge2[tot].to = to;
    edge2[tot].next = head2[from];
    head2[from] = tot++;
}

void init()
{
    index = 0;
    sccnum = 0;
    top = 0;
    tot = 0;
    tot2 = 0;
    maxs = 0;
    br_cnt = 0;
    end_p = 0;
    memset ( head, -1, sizeof(head) );
    memset (head2, -1, sizeof(head2) );
    memset ( low, 0, sizeof(low) );
    memset (DFN, 0, sizeof(DFN) );
    memset ( instack, 0, sizeof(instack) );
    memset ( flag, 0, sizeof(flag) );
    memset (vis, 0, sizeof(vis) );

}

void build()
{
    int u, v;
    while (!qu.empty() )
    {
        qu.pop();
    }
    for (int i = 1; i <= m; ++i)
    {
        scanf("%d%d", &u, &v);
        addedge(u, v, i);
        addedge(v, u, i);
    }
}

void tarjan(int u, int fa)  
{  
    qu.push(u);
    instack[u] = 1;
    DFN[u] = low[u] = ++index;  
    for (int i = head[u]; i != -1; i = edge[i].next)  
    {  
        int v = edge[i].to;  
        if (fa == edge[i].id)  
        {
            continue;
        }   
        if ( !instack[v] )  
        {  
            tarjan(v, edge[i].id);  
            low[u] = min(low[u], low[v]);  
            if (DFN[u] < low[v])
            {
                br_cnt++;
            }
        }  
        else 
        {
            low[u] = min(DFN[v], low[u]);
        }  
    }
    if (DFN[u] == low[u])
    {
        sccnum++;
        int v;
        while(1)
        {
            v = qu.top();
            qu.pop();
            instack[v] = 0;
            block[v] = sccnum;
            if(v == u)
            {
                break;
            }
        }
    }
}

void bfs(int s)
{
    queue<int>qu;
    while ( !qu.empty() )
    {
        qu.pop();
    }
    memset ( vis, 0, sizeof(vis) );
    qu.push(s);
    dist[s] = 0;
    vis[s] = 1;
    maxs = 0;
    while ( !qu.empty() )
    {
        int u = qu.front();
        qu.pop();
        for (int i = head2[u]; i != -1; i = edge2[i].next)
        {
            int v = edge2[i].to;
            if (!vis[v])
            {
                vis[v] = 1;
                dist[v] = dist[u] + 1;
                qu.push(v);
                if (maxs < dist[v])
                {
                    maxs = dist[v];
                    end_p = v;
                }
            }
        }
    }
}

void solve()
{
    for (int i = 1; i <= n; ++i)
    {
        if (DFN[i] == 0)
        {
            tarjan(i, -1);
        }
    } 
    for (int u = 1; u <= n; ++u)
    {
        for (int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to;
            if (block[u] != block[v])
            {
                addedge2(block[u], block[v]);
            }
        }
    }
    bfs(1);
    bfs(end_p);
    printf("%d\n", br_cnt - maxs);
}

int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        if (!n && !m)
        {
            break;
        }
        init();
        build();
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值