hdu4612-Warm up(边的双连通分量)
题意:有n个点,m条边,有重边。现在可以任意在图上添加一条边,求桥的最少数目。
题解:思路就是求出双连通分量之后缩点成为一棵树,然后求出树的直径,连接树的直径就能减少最多的桥。
难点在于:有!重!边!
像我这样习惯于无脑用模板的人来说。。。。头疼死了。。。。。。
既然有重边,dfs的时候就不要标记点,以边为标记,然后照常求分量就好了。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <bitset>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define pk(x) printf("x=%d\n", x)
using namespace std;
#define PI acos(-1.0)
#define EPS 1E-6
#define clr(x,c) memset(x,c,sizeof(x))
typedef long long ll;
typedef pair<int, int> PII; const int N = ;
const int M = ; struct Edge {
int from, to, next;
int cut;
} edge[M];
int cnt_edge;
int head[N];
void add_edge(int u, int v)
{
edge[cnt_edge].from = u;
edge[cnt_edge].to = v;
edge[cnt_edge].next = head[u];
edge[cnt_edge].cut = ;
head[u] = cnt_edge++;
} int dfn[N]; int idx;
int low[N];
int stk[N]; int top;
int kind[N]; int cnt;
bool in[N]; void tarjan(int u, int pre)
{
low[u] = dfn[u] = ++idx;
in[u] = true;
stk[++top] = u;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (i == pre) continue;
if (!dfn[v])
{
tarjan(v, i^);
low[u] = min(low[u], low[v]);
if (low[v] > dfn[u])
{
edge[i].cut = true;
edge[i ^ ].cut = true;
}
}
else low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
cnt++;
int v;
do {
v = stk[top--];
in[v] = false;
kind[v] = cnt;
} while (u != v);
}
} vector<int> G[N];
int ans, ansv; void dfs(int u, int fa, int d) {
if (ans < d) {
ans = d;
ansv = u;
}
for (unsigned i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if (v == fa) continue;
dfs(v, u, d+);
}
} void init()
{
for (int i = ; i < N; ++i) G[i].clear();
memset(dfn, , sizeof dfn);
memset(head, -, sizeof head);
top = idx = cnt = cnt_edge = ;
}
int main()
{
int n, m;
while (~scanf("%d%d", &n, &m)) {
if (n + m == ) break;
init();
int u, v;
for (int i = ; i < m; ++i) {
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
tarjan(, -); int ret = ;
for (int i = ; i < cnt_edge; ++i) {
if (edge[i].cut) {
int u = edge[i].from;
int v = edge[i].to; if (u < v)
ret++;
G[ kind[u] ].push_back( kind[v] );
}
}
ans = ; ansv = ; dfs(, -, );
ans = ; dfs(ansv, -, );
printf("%d\n", ret-ans); }
return ;
}
/**
6 6
1 2 2 3 3 4 4 5 2 6 6 2 6 8
1 2 2 3 3 4 4 5 2 6 6 2 1 3 4 7 5 4
2 1 2 3 2 4 2 5 5 5
2 1 2 3 2 4 2 5 3 4 5 5
1 2 2 3 3 4 3 4 4 5 0
1
2
0
0
*/
======
wa:12次,tle:1次,mle:3次,ce:2次
一颗赛艇。。。
hdu4612-Warm up(边的双连通分量)的更多相关文章
- HDU 4612——Warm up——————【边双连通分量、树的直径】
Warm up Time Limit:5000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- HDU4612 Warm up 边双连通分量&&桥&&树直径
题目的意思很简单,给你一个已经连通的无向图,我们知道,图上不同的边连通分量之间有一定数量的桥,题目要求的就是要你再在这个图上加一条边,使得图的桥数目减到最少. 首先要做的就是找出桥,以及每个点所各自代 ...
- HDU-4612 Warm up 边双连通分量+缩点+最长链
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 简单图论题,先求图的边双连通分量,注意,此题有重边(admin还逗比的说没有重边),在用targ ...
- 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 ...
- hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- 【HDU4612】 双连通分量求桥
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 题目大意:给你一个无向图,问你加一条边后最少还剩下多少多少割边. 解题思路:好水的一道模板题.先 ...
- [HDOJ4612]Warm up(双连通分量,缩点,树直径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 所有图论题都要往树上考虑 题意:给一张图,仅允许添加一条边,问能干掉的最多条桥有多少. 必须解决 ...
- HDU 4612 Warm up (边双连通分量+DP最长链)
[题意]给定一个无向图,问在允许加一条边的情况下,最少的桥的个数 [思路]对图做一遍Tarjan找出桥,把双连通分量缩成一个点,这样原图就成了一棵树,树的每条边都是桥.然后在树中求最长链,这样在两端点 ...
- HDU 4612 Warm up (边双连通分量+缩点+树的直径)
<题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
随机推荐
- 关于Java中try-catch-finally-return语句的思考
我们知道return语句用在某一个方法中,一是用于返回函数的执行结果,二是用于返回值为void类型的函数中,仅仅是一个return语句(return ;),此时用于结束方法的执行,也即此return后 ...
- 20130729--Samba的学习
(一).基本概念 samba是一个能让你的Unix计算机和其它MS Windows计算机相互共享资源的软件. samba提供有关资源共享的三个功能,包括:smbd,执行它可以使Unix能够共享资源给其 ...
- Head First 设计模式笔记:单例模式
单例模式 确保一个类只有一个实例,并提供一个全局访问点. 类图: Singleton static uniqueInstance //其他属性... static getInstance() //其他 ...
- Hardwood Species
http://poj.org/problem?id=2418 #include<cstdio> #include<cstring> #include<string> ...
- HDU4628+状态压缩DP
/* 状态压缩DP dp[ i ]:达到i状态的最小step. 题意:每次可以去掉一个回文串,求最少几步能取完. */ #include<stdio.h> #include<stri ...
- Google Play市场考察报告
考察了Google Play日本市场的10款应用,考察的重点在于每个App有什么亮点,盈利模式在哪里.本文并不是App的功能介绍. (1)恋爱文集[文库类应用] 该应用收录了一些恋爱文章,其主要受众是 ...
- 最新版FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用简介
http://www.cnblogs.com/kflwz/articles/1337310.html 1.下载最新版FreeTextBox(版本3.1.6),解压 FreeTextBox 3. ...
- Android:控件布局(绝对布局)AbsoluteLayout
绝对布局也叫坐标布局,指定元素的绝对位置,因为适应性很差,一般很少用到.可以使用RelativeLayout替代. 常用属性: android:layout_x --------组件x坐标 andr ...
- POJ1035——Spell checker(字符串处理)
Spell checker DescriptionYou, as a member of a development team for a new spell checking program, ar ...
- TOP命令详解
TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示系统中C ...