Codeforces Gym 100114 J. Computer Network
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的更多相关文章
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- 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 ...
- [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)
[Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- Codeforces Gym 100114 H. Milestones 离线树状数组
H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...
- Codeforces GYM 100114 B. Island 水题
B. Island Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description O ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- Codeforces GYM 100114 C. Sequence 打表
C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description ...
随机推荐
- telnet连接ip
win7 telnet命令无法开启的解决方案(不是内部命令或外部命令) 出处:西西整理 作者:西西 日期:2012-12-21 14:22:06 [大 中 小] 评论: 0 | 我要发表看法 如果你想 ...
- GLEW OpenGL Access violation when using glGenVertexArrays
http://stackoverflow.com/questions/20766864/glew-opengl-access-violation-when-using-glgenvertexarray ...
- Flash性能优化
Adobe Scout All the internal operations of Flash Player. Flash Builder Actionscript 3 code level pro ...
- Django笔记-helloworld
网上的Django资料太乱了,我想写一下自己的学习过程(只记大体过程,有时间就完善).(用eclipse+PyDev工具开发的) 1.项目结构 2.关键代码:(注意缩进,可能贴上来缩进格式等有变化,我 ...
- apache struts 2 任意代码执行漏洞
漏洞检测地址:http://0day.websaas.cn 漏洞利用工具,如下: 漏洞利用,如下: step1 step2 step3 提权思路,如下: 1.开启虚拟终端,执行命令,但是,提示“连接被 ...
- 查找“CDN、负载均衡、反向代理”等大型网络真实IP地址的方法
首先,CDN.负载均衡.反向代理还分为很多层,有时查出来的是最外层的 CDN 服务器群,真实的机器是不对外开放的,类似这样的: 用户 → CDN 网络 → 一台或多台真实机器 ↗ CDN Server ...
- jQuery监听键盘事件及相关操作使用教程
一.首先需要知道的是: 1.keydown() keydown事件会在键盘按下时触发. 2.keyup() keyup事件会在按键释放时触发,也就是你按下键盘起来后的事件 3.keypress() k ...
- OC-常见错误 方法与函数的区别
对象方法: 1,减号 - 2,声明必须写在@interface和@end之间 实现必须写在@implement 和@end之间 3,对象方法只能由对象来调用 4,对象方法归类.对象所有 函数: 函 ...
- java批量生成excel代码分享
package com.test.util; /** * @author ocq * */ import java.io.FileOutputStream; import java.io.IOExce ...
- UCMA设置lync在线状态
摘要 UCMA全称Microsoft Unified Communications Managed API,主要用来构建工作在Microsoft Lync Server上的中间层应用程序.开发人员可以 ...