日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU4612 Warm up

發布時間:2023/12/3 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU4612 Warm up 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 11184 Accepted Submission(s): 2573

HDU4612 Warm up

文章目錄

    • Problem Description
    • 題意:
    • 題解:
    • 代碼:

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

題意:

n個點,m個邊,問在添加一個邊后,輸出最小數量的橋
(橋:如果去掉這個邊,會使得一些點與其他點隔離)

題解:

tarjan求出每個邊雙連通分量后,對每個雙連通分量進行縮點,縮點就就是一顆無根樹,在樹上添加一個邊,使得樹上的橋最少,該怎么做?
一個樹,所有邊都是橋(因為你去掉任何一個邊都會使得點與點分離),而我們只能加一個邊,那我們就盡可能保留最多點,這樣就可以最大程度減少橋的數量,最多能減少多少?其實就是樹上的最長路,也就是從一個葉子節點到另外一個葉子節點,連接后,這個就形成閉環,因為是最長路,所有保留的最多。問題就轉變成求樹上最長路,即求樹的直徑
求樹的直徑可以用兩邊dfs來實現(原理我也忘了)
從任意一點x開始,dfs找與x最遠的點y,然后從y開始找與y最遠的點z,x與z的距離就是

代碼:

代碼參考
本人對代碼詳細注釋

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <map> #include <string> #include <set> using namespace std; typedef long long LL; const double EPS = 1e-8; const int INF = 2e9; const LL LNF = 2e18; const int MAXN = 2e5+10;struct Edge {int to, next; }edge[MAXN*10]; int tot, head[MAXN]; vector<int>g[MAXN];void addedge(int u, int v) {edge[tot].to = v;edge[tot].next = head[u];head[u] = tot++; }int index, dfn[MAXN], low[MAXN]; int top, Stack[MAXN], instack[MAXN]; int block, belong[MAXN];void Tarjan(int u, int pre) {dfn[u] = low[u] = ++index;Stack[top++] = u;instack[u] = true;for(int i = head[u]; i!=-1; i = edge[i].next){//因為一對點之間可能有多條邊,所以不能根據v是否為上一個點來防止邊是否被重復訪問。而需要根據邊的編號if((i^1)==pre) continue;int v = edge[i].to;if(!dfn[v]){Tarjan(v, i);low[u] = min(low[u], low[v]);}else if(instack[v])low[u] = min(low[u], dfn[v]);}if(low[u]==dfn[u]){block++;int v;do{v = Stack[--top];instack[v] = false;belong[v] = block;}while(v!=u);} }//常規的tarjan縮點操作int diameter, endpoint; int dfs(int u, int pre, int dep)//當前節點 前節點 深度 {if(dep>diameter)//深度越深時,長度越長 {endpoint = u; //記錄第一個端點 diameter = dep; //最深深度 }for(int i = 0; i<g[u].size(); i++)if(g[u][i]!=pre)dfs(g[u][i], u, dep+1); }void init(int n) {tot = 0;memset(head, -1, sizeof(head));index = 0;memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));top = 0;memset(instack, false, sizeof(instack));block = 0;for(int i = 1; i<=n; i++)belong[i] = i, g[i].clear(); }int main() {int n, m;while(scanf("%d%d", &n, &m) && (n||m) ){init(n);for(int i = 1; i<=m; i++){int u, v;scanf("%d%d", &u, &v);addedge(u, v);addedge(v, u);}Tarjan(1, -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(belong[u]!=belong[v])//如果不在同一個強連通量 g[belong[u]].push_back(belong[v]);//生成樹 }endpoint = 1, diameter = 0;dfs(1, -1, 0);dfs(endpoint, -1, 0);printf("%d\n", block-1-diameter);//block(分塊數量)-1表示最初橋的數量 //diameter表示最長距離,也就是取消的橋數量 } }

總結

以上是生活随笔為你收集整理的HDU4612 Warm up的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。