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.mzph.cn/news/391087.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

吴恩达神经网络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 化学…

论文搜索源

中国科学院文献情报中心 见下图 中国计算机学会推荐国际学术会议和期刊目录 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;攻击者能…

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;则可以减少建立到同一服…

外星人图像和外星人太空船_卫星图像:来自太空的见解

外星人图像和外星人太空船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…

棒棒糖 宏_棒棒糖图表

棒棒糖 宏AKA: lollipop plot又名&#xff1a;棒棒糖情节 WHY: a lollipop chart (LC) is a handy variation of a bar chart where the bar is replaced with a line and a dot at the end. Just like bar graphs, lollipop plots are used to make comparisons between diff…

ubuntu上如何安装tomcat

1. 在官网下载linux里面的tomcat 2. 放到DownLoads下面--把tomcat的压缩包放到DownLoads3. sudo mkdir /usr/local/tomcat/ -在usr/local/路径下新建一个tomcat的文件夹4 sudo tar zxvf tomcat。。。。tar.gz -C /usr/local/tomcat/---把解压后的tomcat放到usr/local/下的tomca…

ZooKeeper3.4.5-最基本API开发

2019独角兽企业重金招聘Python工程师标准>>> package cn.itcast.bigdata.zk;import java.io.IOException; import java.util.List;import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEven…

nlp自然语言处理_不要被NLP Research淹没

nlp自然语言处理自然语言处理 (Natural Language Processing) 到底是怎么回事&#xff1f; (What is going on?) NLP is the new Computer VisionNLP是新的计算机视觉 With enormous amount go textual datasets available; giants like Google, Microsoft, Facebook etc have…

opencv 随笔

装环境好累&#xff0c;python3.6&#xff0c;opencv3.4 好不容易装好了&#xff0c;结果 addweight的时候总是报错 The operation is neither array op array (where arrays have the same size and the same number of channels), nor array op scalar, nor scalar op array …

中小型研发团队架构实践三要点(转自原携程架构师张辉清)

如果你正好处在中小型研发团队…… 中小型研发团队很多&#xff0c;而社区在中小型研发团队架构实践方面的探讨却很少。中小型研发团队特别是 50 至 200 人的研发团队&#xff0c;在早期的业务探索阶段&#xff0c;更多关注业务逻辑&#xff0c;快速迭代以验证商业模式&#xf…

时间序列预测 预测时间段_应用时间序列预测:美国住宅

时间序列预测 预测时间段1.简介 (1. Introduction) During these COVID19 months housing sector is rebounding rapidly after a downtime since the early months of the year. New residential house construction was down to about 1 million in April. As of July 1.5 mi…

zabbix之web监控

Web monitoring(web监控)是用来监控Web程序的&#xff0c;可以监控到Web程序的下载速度&#xff0c;返回码以及响应时间&#xff0c;还支持把一组连续的Web动作作为一个整体进行监控。 1.Web监控的原理 Web监控即对HTTP服务的监控&#xff0c;模拟用户去访问网站&#xff0c;对…

经验主义 保守主义_为什么我们需要行动主义-始终如此。

经验主义 保守主义It’s been almost three months since George Floyd was murdered and the mass protests. Three months since the nationwide protests, looting and riots across America.距离乔治弗洛伊德(George Floyd)被谋杀和大规模抗议活动已经快三个月了。 全国抗议…

redis介绍以及安装

一、redis介绍 redis是一个key-value存储系统。和Memcached类似&#xff0c;它支持存储的values类型相对更多&#xff0c;包括字符串、列表、哈希散列表、集合&#xff0c;有序集合。 这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作&#xff0c;而且…

python机器学习预测_使用Python和机器学习预测未来的股市趋势

python机器学习预测Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works withou…

Python数据结构之四——set(集合)

Python版本&#xff1a;3.6.2 操作系统&#xff1a;Windows 作者&#xff1a;SmallWZQ 经过几天的回顾和学习&#xff0c;我终于把Python 3.x中的基础知识介绍好啦。下面将要继续什么呢&#xff1f;让我想想先~~~嗯&#xff0c;还是先整理一下近期有关Python基础知识的随笔吧…