Gym - 100712H Bridges

沉迷于連通分量了
最后一道了 ,o((>ω< ))o
不知道為什么乍看這道題感覺還挺簡(jiǎn)單,結(jié)果又是一個(gè)邊連通分量+樹的直徑問題
樹的直徑已經(jīng)被虐過一道了,真的難受T.T~

H. Bridges
An edge in an undirected graph is a ? bridge? if after removing it the graph will be disconnected.
Given an undirected connected graph, you are allowed to add one edge between any pair of nodes so that the total number of bridges in the graph is minimized.

Input
The first line of input contains ? T (1 ≤ T ≤ 64) that represents the number of test cases.
The first line of each test case contains two integers: ? N (3 ≤ N ≤ 100,000) and ? M (N-1 ≤ M ≤ 100,000), where ? N is the number of nodes, and ? M is the number of edges.
Each of the following ? M lines contains two space-separated integers: ? X Y (1 ≤ X, Y ≤ N)(X ≠ Y) , which means that there is an edge between node ? X
? and node ? Y.
It is guaranteed that each pair of nodes is connected by at most one edge.
Test cases are separated by a blank line.
Output
For each test case, print a single line with the minimum possible number of bridges after adding one edge.

Sample Input
2
7 7
1 2
2 3
3 1
3 4
4 5
4 6
6 7

3 3
1 2
2 3
3 1

Sample Output

1
0

這個(gè)題其實(shí)比上一道還要簡(jiǎn)單一點(diǎn)
只需要求出縮點(diǎn)后點(diǎn)的數(shù)量 - 樹的直徑的長(zhǎng)度

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<stack>
#define LL long long
using namespace std;
const int maxn = 100010;
const LL inf = 0xffffffffff;
int n,m;
int dfn[maxn],low[maxn],belong[maxn];
int cnt,index;
struct node
{
    int u,v,w;
}s[maxn];
vector<int >G[maxn];
vector<node> g[maxn];
stack<int >S;
void init()
{
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(belong,0,sizeof(belong));
    for(int i=0;i<=n;i++)
    {
        G[i].clear();g[i].clear();
    }
    cnt = index = 0;
}
void Tarjan(int u,int pre)
{
    dfn[u] = low[u] = ++index;
    S.push(u);
    for(int i=0;i<G[u].size();i++)
    {
        int v = G[u][i];
        if(v == pre)  continue;
        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])
    {
        int gg;
        ++cnt;
        while(1)
        {
            gg = S.top();
            S.pop();
            belong[gg] = cnt;
            if(gg==u)  break;
        }
    }
}



int max_len,st;

void TreeDia(int u,int pre,int len)
{
    if(len > max_len) st = u,max_len = len;
    for(int i=0;i<g[u].size();i++)
    {
        int v = g[u][i].v;
        if(v==pre)  continue;
        TreeDia(v,u,len+g[u][i].w);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i = 0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            s[i].u = a,s[i].v = b,s[i].w = 1;
            G[a].push_back(b);
            G[b].push_back(a);
        }
        Tarjan(1,-1);

        for(int i=0;i<m;i++)
        {

                int u = belong[s[i].u];
                int v = belong[s[i].v];
                int w = s[i].w;
                if(u!=v)
                {
                    //printf("u = %d,v = %d\n",u,v);
                    g[u].push_back((node){0,v,w});
                    g[v].push_back((node){0,u,w});
                }

        }
        max_len = -1;
        TreeDia(1,-1,0);
        TreeDia(st,-1,0);
        //printf("cnt = %d , max_len = %d\n",cnt,max_len);
        printf("%d\n",cnt-max_len-1);
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 不知道為什么,想起你的第一反應(yīng)就是你身上的味道。低沉但又有一層清新,浮在你的皮膚表面,嗅到就會(huì)覺得無比放松。聞多了...
    小小A醬閱讀 315評(píng)論 0 0
  • 這幾天身體不適,在客棧修養(yǎng)生息,也沒怎么出去溜達(dá),客棧有很多朋友,和她們一起聊天,有的是剛從尼泊爾回來,有的正...
    壓縮水果閱讀 306評(píng)論 2 1
  • 01 今天下午跑到書店去閑晃,看到《斷舍離》。 最初接觸到這幾個(gè)字,是在微信公眾號(hào)里偶心態(tài)爾幾篇文章的標(biāo)題,但我一...
    蒸餃閱讀 449評(píng)論 3 1

友情鏈接更多精彩內(nèi)容