HDU4612 Warm up —— 邊雙聯通分量 + 重邊 + 縮點 + 樹上最長路

題目鏈接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4612

?

Warm up

Time Limit: 10000/5000 MS (Java/Others)????Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 7206????Accepted Submission(s): 1681


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

?

Author
SYSU

?

Source
2013 Multi-University Training Contest 2

?

Recommend
zhuyuanchen520
題解:
1.用Tarjan算法求出每個邊雙聯通分量,由于每一對點之間可以有多條邊,所以在判斷邊是否被重復訪問時,需要依據邊的下標而定?。
2.對每個邊雙聯通分量進行縮點,縮點之后得到的是一棵無根樹。
3.在樹上添加一條邊,使得橋的數目減少最多。最多能減少多少呢?樹上最長路。
vector建樹:
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <vector>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 using namespace std;
 13 typedef long long LL;
 14 const double EPS = 1e-8;
 15 const int INF = 2e9;
 16 const LL LNF = 2e18;
 17 const int MAXN = 2e5+10;
 18 
 19 struct Edge
 20 {
 21     int to, next;
 22 }edge[MAXN*10];
 23 int tot, head[MAXN];
 24 vector<int>g[MAXN];
 25 
 26 void addedge(int u, int v)
 27 {
 28     edge[tot].to = v;
 29     edge[tot].next = head[u];
 30     head[u] = tot++;
 31 }
 32 
 33 int index, dfn[MAXN], low[MAXN];
 34 int top, Stack[MAXN], instack[MAXN];
 35 int block, belong[MAXN];
 36 
 37 void Tarjan(int u, int pre)
 38 {
 39     dfn[u] = low[u] = ++index;
 40     Stack[top++] = u;
 41     instack[u] = true;
 42     for(int i = head[u]; i!=-1; i = edge[i].next)
 43     {
 44         //因為一對點之間可能有多條邊,所以不能根據v是否為上一個點來防止邊是否被重復訪問。而需要根據邊的編號
 45         if((i^1)==pre) continue;
 46         int v = edge[i].to;
 47         if(!dfn[v])
 48         {
 49             Tarjan(v, i);
 50             low[u] = min(low[u], low[v]);
 51         }
 52         else if(instack[v])
 53             low[u] = min(low[u], dfn[v]);
 54     }
 55 
 56     if(low[u]==dfn[u])
 57     {
 58         block++;
 59         int v;
 60         do
 61         {
 62             v = Stack[--top];
 63             instack[v] = false;
 64             belong[v] = block;
 65         }while(v!=u);
 66     }
 67 }
 68 
 69 int diameter, endpoint;
 70 int dfs(int u, int pre, int dep)
 71 {
 72     if(dep>diameter) { endpoint = u; diameter = dep; }
 73     for(int i = 0; i<g[u].size(); i++)
 74         if(g[u][i]!=pre)
 75             dfs(g[u][i], u, dep+1);
 76 }
 77 
 78 void init(int n)
 79 {
 80     tot = 0;
 81     memset(head, -1, sizeof(head));
 82 
 83     index = 0;
 84     memset(dfn, 0, sizeof(dfn));
 85     memset(low, 0, sizeof(low));
 86 
 87     top = 0;
 88     memset(instack, false, sizeof(instack));
 89 
 90     block = 0;
 91     for(int i = 1; i<=n; i++)
 92         belong[i] = i, g[i].clear();
 93 }
 94 
 95 int main()
 96 {
 97     int n, m;
 98     while(scanf("%d%d", &n, &m) && (n||m) )
 99     {
100         init(n);
101         for(int i = 1; i<=m; i++)
102         {
103             int u, v;
104             scanf("%d%d", &u, &v);
105             addedge(u, v);
106             addedge(v, u);
107         }
108 
109         Tarjan(1, -1);
110         for(int u = 1; u<=n; u++)
111         for(int i = head[u]; i!=-1; i = edge[i].next)
112         {
113             int v = edge[i].to;
114             if(belong[u]!=belong[v])
115                 g[belong[u]].push_back(belong[v]);
116         }
117 
118         endpoint = 1, diameter = 0;
119         dfs(1,  -1, 0);
120         dfs(endpoint,  -1, 0);
121         printf("%d\n", block-1-diameter);
122     }
123 }
View Code

?

前向星建樹:
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <vector>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 using namespace std;
 13 typedef long long LL;
 14 const double EPS = 1e-8;
 15 const int INF = 2e9;
 16 const LL LNF = 2e18;
 17 const int MAXN = 2e5+10;
 18 
 19 struct Edge
 20 {
 21     int from, to, next;
 22 }edge[MAXN*10];
 23 int tot, head[MAXN];
 24 
 25 void addedge(int u, int v)
 26 {
 27     edge[tot].from = u;
 28     edge[tot].to = v;
 29     edge[tot].next = head[u];
 30     head[u] = tot++;
 31 }
 32 
 33 int index, dfn[MAXN], low[MAXN];
 34 int top, Stack[MAXN], instack[MAXN];
 35 int block, belong[MAXN];
 36 
 37 void Tarjan(int u, int pre)
 38 {
 39     dfn[u] = low[u] = ++index;
 40     Stack[top++] = u;
 41     instack[u] = true;
 42     for(int i = head[u]; i!=-1; i = edge[i].next)
 43     {
 44         //因為一對點之間可能有多條邊,所以不能根據v是否為上一個點來防止邊是否被重復訪問。而需要根據邊的編號
 45         if((i^1)==pre) continue;
 46         int v = edge[i].to;
 47         if(!dfn[v])
 48         {
 49             Tarjan(v, i);
 50             low[u] = min(low[u], low[v]);
 51         }
 52         else if(instack[v])
 53             low[u] = min(low[u], dfn[v]);
 54     }
 55 
 56     if(low[u]==dfn[u])
 57     {
 58         block++;
 59         int v;
 60         do
 61         {
 62             v = Stack[--top];
 63             instack[v] = false;
 64             belong[v] = block;
 65         }while(v!=u);
 66     }
 67 }
 68 
 69 int diameter, endpoint;
 70 int dfs(int u, int pre, int dep)
 71 {
 72     if(dep>diameter) { endpoint = u; diameter = dep; }
 73     for(int i = head[u]; i!=-1; i = edge[i].next)
 74         if(edge[i].to!=pre)
 75             dfs(edge[i].to, u, dep+1);
 76 }
 77 
 78 void init(int n)
 79 {
 80     tot = 0;
 81     memset(head, -1, sizeof(head));
 82 
 83     index = 0;
 84     memset(dfn, 0, sizeof(dfn));
 85     memset(low, 0, sizeof(low));
 86 
 87     top = 0;
 88     memset(instack, false, sizeof(instack));
 89 
 90     block = 0;
 91     for(int i = 1; i<=n; i++)
 92         belong[i] = i;
 93 }
 94 
 95 int main()
 96 {
 97     int n, m;
 98     while(scanf("%d%d", &n, &m) && (n||m) )
 99     {
100         init(n);
101         for(int i = 1; i<=m; i++)
102         {
103             int u, v;
104             scanf("%d%d", &u, &v);
105             addedge(u, v);
106             addedge(v, u);
107         }
108 
109         Tarjan(1, -1);
110         tot = 0;
111         memset(head, -1, sizeof(head));
112         for(int i = 0; i<2*m; i++)
113         {
114             int u = edge[i].from, v = edge[i].to;
115             if(belong[u]!=belong[v])
116                 addedge(belong[u], belong[v]);
117         }
118 
119         endpoint = 1, diameter = 0;
120         dfs(1,  -1, 0);
121         dfs(endpoint,  -1, 0);
122         printf("%d\n", block-1-diameter);
123     }
124 }
View Code

?

?

?

?

轉載于:https://www.cnblogs.com/DOLFAMINGO/p/7689173.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391087.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391087.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391087.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android sqlite load_extension漏洞解析

路人甲 2015/09/25 14:540x01 sqlite load_extensionSQLite從3.3.6版本&#xff08;http://www.sqlite.org/cgi/src/artifact/71405a8f9fedc0c2&#xff09;開始提供了支持擴展的能力&#xff0c;通過sqlite_load_extension API&#xff08;或者load_extensionSQL語句&#xf…

去除Java字符串中的空格

問題&#xff1a;去除Java字符串中的空格 俺有一個像這樣的字符串 mysz "namejohn age13 year2001";我想要去除字符串里面的空格。我嘗試使用 trim() &#xff0c;但是呢它只去除了字符串前后的空格。我也嘗試用 ("\W", “”)&#xff0c;但是它把也給搞…

谷歌瀏覽器bug調試快捷鍵_Bug壓榨初學者指南:如何使用調試器和其他工具查找和修復Bug

谷歌瀏覽器bug調試快捷鍵As web developers, it often feels like we spend more time fixing bugs and trying to solve problems than we do writing code. In this guide well look at some common debugging techniques, so lets get stuck in.作為Web開發人員&#xff0c;…

吳恩達神經網絡1-2-2_圖神經網絡進行藥物發現-第1部分

吳恩達神經網絡1-2-2預測溶解度 (Predicting Solubility) 相關資料 (Related Material) Jupyter Notebook for the article Jupyter Notebook的文章 Drug Discovery with Graph Neural Networks — part 2 圖神經網絡進行藥物發現-第2部分 Introduction to Cheminformatics 化學…

再利用Chakra引擎繞過CFG

xlab 2015/12/24 15:00Author:[email protected]0x00 前言本文源自一次與TK閑聊&#xff0c;期間得知成功繞過CFG的經過與細節(參考&#xff1a;[利用Chakra JIT繞過DEP和CFG])。隨即出于對技術的興趣&#xff0c;也抽出一些時間看了相關的東西&#xff0c;結果發現了另一處繞…

論文搜索源

中國科學院文獻情報中心 見下圖 中國計算機學會推薦國際學術會議和期刊目錄 EI學術會議中心,        engieer village 轉載于:https://www.cnblogs.com/cxy-941228/p/7693097.html

重學TCP協議(10)SYN flood 攻擊

1.SYN flood 攻擊 SYN Flood&#xff08;半開放攻擊&#xff09;是一種拒絕服務&#xff08;DDoS&#xff09;攻擊&#xff0c;其目的是通過消耗所有可用的服務器資源使服務器不可用于合法流量。通過重復發送初始連接請求&#xff08;SYN&#xff09;數據包&#xff0c;攻擊者能…

大數據入門課程_我根據數千個數據點對互聯網上的每門數據科學入門課程進行了排名...

大數據入門課程by David Venturi大衛文圖里(David Venturi) A year ago, I dropped out of one of the best computer science programs in Canada. I started creating my own data science master’s program using online resources. I realized that I could learn everyt…

python 數據框缺失值_Python:處理數據框中的缺失值

python 數據框缺失值介紹 (Introduction) In the last article we went through on how to find the missing values. This link has the details on the how to find missing values in the data frame. https://medium.com/kallepalliravi/python-finding-missing-values-in-…

Spring Cloud 5分鐘搭建教程(附上一個分布式日志系統項目作為參考) - 推薦

http://blog.csdn.net/lc0817/article/details/53266212/ https://github.com/leoChaoGlut/log-sys 上面是我基于Spring Cloud ,Spring Boot 和 Docker 搭建的一個分布式日志系統. 目前已在我司使用. 想要學習Spring Cloud, Spring Boot以及Spring 全家桶的童鞋,可以參考學習,如…

51nod1832(二叉樹/高精度模板+dfs)

題目鏈接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId1832 題意: 中文題誒~ 思路: 若二叉樹中有 k 個節點只有一個子樹, 則答案為 1 << k. 詳情參見:http://blog.csdn.net/gyhguoge01234/article/details/77836484 代碼: 1 #include <iostream&g…

重學TCP協議(11)TFO(Tcp Fast Open)

1. TFO 為了改善web應用相應時延&#xff0c;google發布了通過修改TCP協議利用三次握手時進行數據交換的TFO(TCP fast open&#xff0c;RFC 7413)。 TFO允許在TCP握手期間發送和接收初始SYN分組中的數據。如果客戶端和服務器都支持TFO功能&#xff0c;則可以減少建立到同一服…

[網絡安全] 遠程登錄

遠程登錄方式: 1.圖像化遠程登錄 做法: 運行"窗口"輸入 "mstsc " 輸入ip地址 注意: 被遠程計算機&#xff0c;必須打開遠程登錄服務: 信息面板–系統–允許遠程訪問。被遠程計算機&#xff0c;必須存在擁有遠程桌面權限的用戶。 2.命令行遠程登錄 teln…

外星人圖像和外星人太空船_衛星圖像:來自太空的見解

外星人圖像和外星人太空船By Christophe Restif & Avi Hoffman, Senior Software Engineers, Crisis Response危機應對高級軟件工程師Christophe Restif和Avi Hoffman Editor’s note: In 2019, we piloted a new feature in Search SOS Alerts for major California wild…

chrome恐龍游戲_如何玩沒有互聯網的Google Chrome恐龍游戲-在線和離線

chrome恐龍游戲Several years ago, Google added a fun little Easter egg to Chrome: if your internet went down and you tried to visit a web page, youd see the message "Unable to connect to the Internet" or "No internet" with a little pixi…

Hotpatch潛在的安全風險

屎蛋 2016/06/22 10:11author:[email protected]0x00 “Hotpatch”簡介IOS App的開發者們經常會出現這類問題&#xff1a;當一個新版本上線后發現存在一個嚴重的bug&#xff0c;有可能因為一個邏輯問題導致支付接口存在被薅羊毛的風險&#xff0c;這個時候能做的只能是趕快修復…

spring中@Inject和@Autowired的區別?分別在什么條件下使用呢?

問題&#xff1a;spring中Inject和Autowired的區別&#xff1f;分別在什么條件下使用呢&#xff1f; 我在瀏覽SpringSource上的一些博客&#xff0c;在其他一個博客中&#xff0c;那個作者用了Inject&#xff0c;但是我覺得他用Autowired也行 下面是一部分代碼&#xff1a; …

Objective-C語言的動態性

Objective-C具有相當多的動態特性&#xff0c;基本的&#xff0c;也是經常被提到和用到的有動態類型&#xff08;Dynamic typing&#xff09;&#xff0c;動態綁定&#xff08;Dynamic binding&#xff09;和動態加載&#xff08;Dynamic loading&#xff09; 一、編譯時和運行…

內存泄漏和內存溢出的區別

原文地址https://www.zhihu.com/question/40560123 簡單來說&#xff0c;操作系統就像資源分配人員&#xff0c;你要使用內存的時候分給你&#xff0c;你用完了還給它。如果你使用了沒有分配給你的內存就是內存溢出&#xff0c;如果你用完了沒有還就是內存泄漏。會引起的問題&a…

怎么注銷筆記本icloud_如何在筆記本電腦或臺式機的Web瀏覽器中在線查看Apple iCloud照片

怎么注銷筆記本icloudPicture this: you just returned from a beautiful vacation and want to show all those gorgeous photos to your family. But your phone just died. And since youre at a family dinner your laptop is nowhere to be found.想象一下&#xff1a;您剛…