HDU 4612 Warm up (边双连通分量+DP最长链)
【题意】给定一个无向图,问在允许加一条边的情况下,最少的桥的个数
【思路】对图做一遍Tarjan找出桥,把双连通分量缩成一个点,这样原图就成了一棵树,树的每条边都是桥。然后在树中求最长链,这样在两端点间连一条边就能形成环从而减少桥数。
不能更逗比。。多校第一场刚做出来的找最长链第二场就做错了= =,还一直以为是模板的问题。。。。。。
#include
#include
#include
#include
#include
#include
#define MID(x,y) ((x+y)/2)
#define mem(a,b) memset(a,b,sizeof(a))
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
const int MAXV = 200005;
const int MAXE = 2000005;
struct node{
int u, v;
int next;
bool bridge;
}arc[MAXE];
int cnt, head[MAXV];
void init(){
cnt = 0;
mem(head, -1);
return ;
}
void add(int u, int v){
arc[cnt].u = u;
arc[cnt].v = v;
arc[cnt].next = head[u];
arc[cnt].bridge = false;
head[u] = cnt ++;
arc[cnt].u = v;
arc[cnt].v = u;
arc[cnt].next = head[v];
arc[cnt].bridge = false;
head[v] = cnt ++;
return ;
}
int id, dfn[MAXV], low[MAXV];
int bridge_num;
bool vis_arc[MAXE]; //一条边无向边(两个有向边)只访问一次,
void tarjan(int u){
dfn[u] = low[u] = ++ id;
for (int i = head[u]; i != -1; i = arc[i].next){
if (vis_arc[i]) continue;
int v = arc[i].v;
vis_arc[i] = vis_arc[i^1] = 1;
if (!dfn[v]){
tarjan(v);
low[u] = min(low[u], low[v]);
if (dfn[u] bcc[MAXV];void fill(int u){
bcc[bcc_num].push_back(u);
mark[u] = bcc_num;
for (int i = head[u]; i != -1; i = arc[i].next){
if (arc[i].bridge) continue;
int v = arc[i].v;
if (mark[v] == 0)
fill(v);
}
}
void find_bcc(int n){
mem(vis, 0);
mem(mark, 0);
//确定每个点所属边双联通分量
for (int i = 1; i max1){
max2 = max1;
max1 = dp[bccv];
}
else{
if (dp[bccv] > max2){
max2 = dp[bccv];
}
}
}
}
}
}
if (0 == num){
dp[bccu] = 0;
}
else{
if (num == 1)
maxlong = max(maxlong, max1+1);
else
maxlong = max(maxlong, max1+max2+2);
dp[bccu] = max1 + 1;
}
return ;
}
int n, m;
int main(){
while(scanf("%d %d", &n, &m) != EOF){
if (n + m == 0)
break;
init();
for (int i = 0; iHDU 4612 Warm up (边双连通分量+DP最长链)的更多相关文章
- HDU 4612 Warm up (边双连通分量+缩点+树的直径)
<题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...
- HDU 4612 Warm up tarjan缩环+求最长链
Warm up Problem Description N planets are connected by M bidirectional channels that allow instant ...
- hdu 4612 Warm up(缩点+树上最长链)
本来就是自己负责图论,结果水了= = 题目其实很裸,就是求桥的数量,只是要新加上一条边罢了.做法:先缩点.再在树上搜最长链(第一场多校的hdu 4607Park Visit就考了最长链,小样,套个马甲 ...
- HDU 4612 Warm up(双连通分量缩点+求树的直径)
思路:强连通分量缩点,建立一颗新的树,然后求树的最长直径,然后加上一条边能够去掉的桥数,就是直径的长度. 树的直径长度的求法:两次bfs可以求,第一次随便找一个点u,然后进行bfs搜到的最后一个点v, ...
- hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- Hdu 4612 Warm up (双连通分支+树的直径)
题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然 ...
- HDU 4612——Warm up——————【边双连通分量、树的直径】
Warm up Time Limit:5000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- hdu 4612 Warm up 双连通+树形dp思想
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total S ...
- HDU 4612 Warm up(2013多校2 1002 双连通分量)
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
随机推荐
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) D. Little Artem and Dance
题目链接: http://codeforces.com/contest/669/problem/D 题意: 给你一个初始序列:1,2,3,...,n. 现在有两种操作: 1.循环左移,循环右移. 2. ...
- div+css的前端工程师的价值体现在哪些方面?
个人认为前端工程师正慢慢演变为产品工程师.wap app, 响应性UI等以html5技术为基础的开发将成为前端工程师的主要工作内容,解决产品跨平台跨设备的实现问题.Javascript, HTML, ...
- 【Unity--Apwork框架】AOP编程--拦截,用于缓存和异常处理(Unity框架的拦截注入-Interception)
第一步:定义拦截行为:CachingBehavior 和 ExceptionLoggingBehavior 他们都继承接口:IInterceptionBehavior (程序集 Microsoft.P ...
- python的dict()函数
dict(one=1,two=2) dict({'one':1,'two':2}) dict((('one',1),('two',2))) dict((['one',1],['two',2])) di ...
- POJ 3258
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5961 Accepted: 2579 D ...
- Namespace, string, vector and array
1. Headers should not include using declaration Code inside headers ordinarily should not include us ...
- GOOGLE搜索秘籍完全公开
一,GOOGLE简介 Google(www.google.com)是一个搜索引擎,由两个斯坦福大学博士生Larry Page与Sergey Brin于1998年9月发明,Google Inc. 于19 ...
- 关于DotNetBar中DataGridViewX 自动全屏 Anchor属性无效问题
由于在DataGridViewX 中使用了控件DataGridViewCheckBoxXColumn会导致 Anchor属性无效问题化,具体原因未知,建议改换为系统自带的DataGridViewChe ...
- 李洪强iOS开发之OC[017]函数和方法的区别
// // main.m // 15 - 函数和对象的方法的区别 // // Created by vic fan on 16/7/12. // Copyright © 2016年 李洪强. ...
- lintcode:Compare Strings 比较字符串
题目: 比较字符串 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 样例 给出 A = "ABCD" B = "ACD" ...