Description

给出一个图,求添加一条边使得添加后的图的桥(割边)最少.

Sol

Tarjan.

一遍Tarjan求割边.

我们发现连接的两个点一定是这两个点之间的路径上的桥最多,然后就可以贪心的搞.

Tarjan的同时记录一下到该点的桥个数的最大值和次大值,然后统计答案就可以.

注意要满足这两个点不能再DFS路径上有公共边,意思就是说在每次回溯的时候统计一下最大值和次大值.

Code

#include<cstdio>
#include<vector>
#include<iostream>
using namespace std; const int N = 10005; int n,m,cnt,ans,fr=1,to=1;
vector<int> h[N];
int dfsn[N],low[N];
int f[N][3],g[N][3]; inline int in(int x=0,char ch=getchar()){ while(ch>'9' || ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x; }
void Tarjan(int u,int fa){
dfsn[u]=low[u]=++cnt,g[u][1]=u;
int cf=0;
for(int i=0,v;i<h[u].size();i++) if((v=h[u][i])!=fa||(v==fa && cf>0)){
if(!dfsn[v]){
Tarjan(v,u);
if(dfsn[u] < low[v]){
if(f[v][1]+1 > ans) ans=f[v][1]+1,fr=u,to=g[v][1];
if(f[u][1] < f[v][1]+1) f[u][2]=f[u][1],g[u][2]=g[u][1],f[u][1]=f[v][1]+1,g[u][1]=g[v][1];
else if(f[u][2] < f[v][1]+1) f[u][2]=f[v][1]+1,g[u][2]=g[v][1];
}else{
if(f[u][1] < f[v][1]) f[u][2]=f[u][1],g[u][2]=g[u][1],f[u][1]=f[v][1],g[u][1]=g[v][1];
else if(f[u][2] < f[v][1]) f[u][2]=f[v][1],g[u][2]=g[v][1];
}low[u]=min(low[u],low[v]);
}else low[u]=min(low[u],dfsn[v]);
}else cf++;
if(f[u][1] > ans) ans=f[u][1],fr=u,to=g[u][1];
if(f[u][1]+f[u][2] > ans) ans=f[u][1]+f[u][2],fr=g[u][1],to=g[u][2];
}
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=in(),m=in();
for(int i=1,u,v;i<=m;i++){
u=in(),v=in();
h[u].push_back(v),h[v].push_back(u);
}
Tarjan(1,0);
// cout<<ans<<endl;
cout<<fr<<" "<<to<<endl;
return 0;
}

  

Codeforces Gym 100114 J. Computer Network的更多相关文章

  1. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  2. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  3. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  4. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)

    [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...

  5. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  6. Codeforces Gym 100114 H. Milestones 离线树状数组

    H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...

  7. Codeforces GYM 100114 B. Island 水题

    B. Island Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description O ...

  8. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  9. Codeforces GYM 100114 C. Sequence 打表

    C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description ...

随机推荐

  1. Bootstrap基本类和组件学习二

    一.联系方式:(自带鼠标的移动动画) 1.头文件CSS <link rel="shortcut icon" href="favicon.ico"> ...

  2. Linux常用服务部署与优化之Samba篇

    关于Samba的简介概述在此略过,开始搭建Samba服务. 1.安装Samba yum install -y samba samba-client 2.编辑Samba配置文件 首先共享一个目录,任何人 ...

  3. Ubuntu PostgreSQL安装和配置

    一.安装 1.安装 使用如下命令,会自动安装最新版,这里为9.5 sudo apt-get install postgresql 安装完成后,默认会: (1)创建名为"postgres&qu ...

  4. Deep Learning in a Nutshell: History and Training

    Deep Learning in a Nutshell: History and Training This series of blog posts aims to provide an intui ...

  5. CentOS6.x安装配置nginx

    nginx安装 nginx的官网:http://nginx.org/   相应下载页面:http://nginx.org/en/download.html   我这里使用nginx的yum在线安装 w ...

  6. yourphp的edit,updata,dele

    参考文件Yourphp\Lib\Action\User\PostAction.class.php public function add() { $form=new Form(); $form-> ...

  7. js无间隙滚动

    代码一: ; //设置文字滚动速度 dome2.innerHTML=dome1.innerHTML //复制dome1为dome2 function Marquee(){ ) //当滚动至dome1与 ...

  8. jquery满屏滚动代码

    //获取显示器宽度 .高度 $screenW = document.body.clientWidth; $screenH = ; $BTimer = ; // 时间周期 $liSize = $(&qu ...

  9. PHP mysqli 扩展库(面向对象/数据库操作封装/事务控制/预编译)

    1.和mysql扩展库的区别: (1   安全性.稳定性更高 (2  提供了面向对象和面向过程两种风格 2.php.ini  中的  extension=php_mysqli.dll 解除封印 3.面 ...

  10. css居中总结

    水平居中 1. inline和inline-*元素水平居中:text-align:center 2. block元素水平居中: block定宽:margin-left: auto; margin-ri ...