Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 90    Accepted Submission(s): 12

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
 
Source
 
Recommend
zhuyuanchen520
 

问加一条边,最少可以剩下几个桥。

先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥。

本题要处理重边的情况。

如果本来就两条重边,不能算是桥。

还会爆栈,只能C++交,手动加栈了

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std; const int MAXN = ;//点数
const int MAXM = ;//边数,因为是无向图,所以这个值要*2 struct Edge
{
int to,next;
bool cut;//是否是桥标记
bool cong;
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~block
int Index,top;
int block;//边双连通块数
bool Instack[MAXN];
int bridge;//桥的数目 void addedge(int u,int v,bool pp)
{
edge[tot].to = v;edge[tot].next = head[u];edge[tot].cut=false;
edge[tot].cong = pp;
head[u] = tot++;
} void Tarjan(int u,int pre,bool ff)
{
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for(int i = head[u];i != -;i = edge[i].next)
{
v = edge[i].to;
if(v == pre && (!ff))continue;
if( !DFN[v] )
{
Tarjan(v,u,edge[i].cong);
if( Low[u] > Low[v] )Low[u] = Low[v];
if(Low[v] > DFN[u])
{
bridge++;
edge[i].cut = true;
edge[i^].cut = true;
}
}
else if( Instack[v] && Low[u] > DFN[v] )
Low[u] = DFN[v];
}
if(Low[u] == DFN[u])
{
block++;
do
{
v = Stack[--top];
Instack[v] = false;
Belong[v] = block;
}
while( v!=u );
}
}
void init()
{
tot = ;
memset(head,-,sizeof(head));
} int du[MAXN];//缩点后形成树,每个点的度数
vector<int>vec[MAXN];
int dep[MAXN];
void dfs(int u)
{
for(int i = ;i < vec[u].size();i++)
{
int v = vec[u][i];
if(dep[v]!=-)continue;
dep[v]=dep[u]+;
dfs(v);
}
}
void solve(int n)
{
memset(DFN,,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
Index = top = block = ;
Tarjan(,,false);
for(int i = ;i <= block;i++)
vec[i].clear();
for(int i = ;i <= n;i++)
for(int j = head[i];j != -;j = edge[j].next)
if(edge[j].cut)
{
vec[Belong[i]].push_back(Belong[edge[j].to]);
}
memset(dep,-,sizeof(dep));
dep[]=;
dfs();
int k = ;
for(int i = ;i <= block;i++)
if(dep[i]>dep[k])
k = i;
memset(dep,-,sizeof(dep));
dep[k]=;
dfs(k);
int ans = ;
for(int i = ;i <= block;i++)
ans = max(ans,dep[i]);
printf("%d\n",block--ans);
}
struct NN
{
int u,v;
}node[MAXM];
bool cmp(NN a,NN b)
{
if(a.u != b.u)return a.u<b.u;
else return a.v<b.v;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
int u,v;
while(scanf("%d%d",&n,&m)==)
{
if(n== && m==)break;
init();
for(int i = ;i < m;i++)
{
scanf("%d%d",&u,&v);
if(u==v)continue;
if(u>v)swap(u,v);
node[i].u = u;
node[i].v = v;
}
sort(node,node+m,cmp);
for(int i = ;i < m;i++)
{
if(i == || (node[i].u!=node[i-].u || node[i].v != node[i-].v))
{
if(i < m- && (node[i].u==node[i+].u && node[i].v == node[i+].v))
{
addedge(node[i].u,node[i].v,true);
addedge(node[i].v,node[i].u,true);
}
else
{
addedge(node[i].u,node[i].v,false);
addedge(node[i].v,node[i].u,false);
}
}
}
solve(n);
}
return ;
}

HDU 4612 Warm up(2013多校2 1002 双连通分量)的更多相关文章

  1. Hdu 4612 Warm up (双连通分支+树的直径)

    题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然 ...

  2. HDU 4667 Building Fence(2013多校7 1002题 计算几何,凸包,圆和三角形)

    Building Fence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)To ...

  3. 【HDU 4612 Warm up】BCC 树的直径

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 题意:一个包含n个节点m条边的无向连通图(无自环,可能有重边).求添加一条边后最少剩余的桥的数 ...

  4. HDU 4612——Warm up——————【边双连通分量、树的直径】

    Warm up Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  5. HDU 4705 Y (2013多校10,1010题,简单树形DP)

    Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...

  6. HDU 4704 Sum (2013多校10,1009题)

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submi ...

  7. HDU 4699 Editor (2013多校10,1004题)

    Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  8. HDU 4696 Answers (2013多校10,1001题 )

    Answers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  9. HDU 4690 EBCDIC (2013多校 1005题 胡搞题)

    EBCDIC Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total Su ...

随机推荐

  1. 多线程安全的HTTPCLIENT

    private static HttpClient httpClient;     static {           HttpParams params = new BasicHttpParams ...

  2. HDU 1517 (类巴什博奕) A Multiplication Game

    如果n在[2, 9]区间,那么Stan胜. 如果n在[10, 18]区间,那么Ollie胜,因为不管第一次Stan乘上多少,第二次Ollie乘上一个9,必然会得到一个不小于18的数. 如果n在[19, ...

  3. 【转】Qt多线程操作界面---在QThread更新QProgressBar

    #include <QApplication> #include <QThread> #include <QMainWindow> #include <QPr ...

  4. ecshop 模版商品详情页,不同商品调用不同模板

    1.在goods.php中找到以下代码 $smarty->display('goods.dwt', $cache_id); 改为下面的代码 switch ($goods['cat_id']){ ...

  5. (六) 语言模型 Language Madel 与 word2vec

    语言模型简介(Language Model) 简单的说,语言模型 (Language Model) 是用来计算一个句子出现概率的模型,假设句子  ,其中  代表句子中的第  个词语,则语句 W 以该顺 ...

  6. 数据结构——Java实现单向链表

    结点类: /** * @author zhengbinMac * 一个OnelinkNode类的对象只表示链表中的一个结点,通过成员变量next的自引用方式实现线性表中各数据元素的逻辑关系. */ p ...

  7. Android 轻量级ORM数据库开源框架ActiveAndroid 源码分析

    ActiveAndroid 项目地址在https://github.com/pardom/ActiveAndroid 关于他的详细介绍和使用步骤 可以看下面两篇文章: https://github.c ...

  8. Shell教程5-Shell运算符

    Bash 支持很多运算符,包括算数运算符.关系运算符.布尔运算符.字符串运算符和文件测试运算符. 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最 ...

  9. YII Framework学习教程-YII的日志

    日志的作用(此处省略1000字) YII中的日志很好很强大,允许你把日志信息存放到数据库,发送到制定email,存放咋文件中,意见显示页面是,甚至可以用来做性能分析. YII中日志的基本配置:/yii ...

  10. 十六进制字符串转化成字符串输出HexToStr(Delphi版、C#版)

    //注意:Delphi2010以下版本默认的字符编码是ANSI,VS2010的默认编码是UTF-8,delphi版得到的字符串须经过Utf8ToAnsi()转码才能跟C#版得到的字符串显示结果一致. ...