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. Java Web学习笔记-Servle生命周期

    Servlet会在服务器启动或第一次请求该Servlet的时候开始生命周期,在服务器停止的时候结束生命周期. 无论请求多少次Servlet,最多只有一个Servlet实例.多个客户端并发请求Servl ...

  2. 使用github和hexo搭建博客

    title: How to build your blog by Hexo in github Welcome to huihuang's blog,you will find what you ex ...

  3. Xcode文档安装

    找到所需文档的下载地址,搜索.dmg 安装位置

  4. django笔记-模型数据模板呈现过程记录(多对多关系)

    首先,推荐一个网址:http://www.tuicool.com/articles/BfqYz2F,因为这里的比我的要有条理,更有利于各位的理解. 以下仅为为个人一次不完整的笔记: 环境:ubuntu ...

  5. JS原型链理解

    1. 每个对象都有原型属性(__proto__)2. 对象的原型(__proto__)指向其构造函数(Class)的prototype属性3. 构造函数(Class)的prototype属性本身也是一 ...

  6. Jedis 例子(demo)大全

    第一步:到git下载jedis源码,如果你用maven或者gradle,那么直接下官方的即可,地址:https://github.com/xetorthio/jedis:如果你用ant,下载这个:ht ...

  7. hibernate4中使用Session doWork()方法进行jdbc操作(代码)

    Hibernate3.3.2版本中getSession().connection()已被弃用,hibernate4中官方推荐使用Session doWork()方法进行jdbc操作 首先看看Work接 ...

  8. iOS- Terminating app due to uncaught exception 'NSRangeException'

    错误描述: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM object ...

  9. Java 线程Thread.Sleep详解

    我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢? 思考下面这两个问题: 1.假设现在是 2008-4-7 12:00:00.000,如果 ...

  10. linux 生成KEY的方法与使用

    转自:http://blog.163.com/tqq_0716/blog/static/7690741220110611350344/ 服务器A: 192.168.1.1 服务器B: 192.168. ...