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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈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;您刚…