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 ...
随机推荐
- 抽象工厂模式(python版)
http://blog.csdn.net/ponder008/article/details/6886039 抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类.优点:易 ...
- Nginx 实现MySQL的负载均衡
Nginx属于七层架构,支持的是http协议,本身对tcp协议没有支持.所以不能代理mysql等实现负载均衡.但是lvs这个东西不熟悉,主要是公司的的负载均衡都是nginx所以决定研究一下nginx的 ...
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-005-Applying LDAP-backed authentication
一. 1.This method is the LDAP analog to jdbcAuthentication() @Override protected void configure(Aut ...
- CentOS7.1 安装关键步骤 记录下来
SecureCRT下载地址 https://yunpan.cn/cS9W94kuvhXPb 访问密码 08cd[这里GNOME桌面 下的 要全选,截屏有误]
- 【POJ】3415 Common Substrings
后缀数组可解.使用单调栈优化. /* 3415 */ #include <iostream> #include <sstream> #include <string> ...
- Descending Order
Descending Order Description: Your task is to make a function that can take any non-negative integer ...
- poj 1260 Pearls(dp)
题目:http://poj.org/problem?id=1260 题意:给出几类珍珠,以及它们的单价,要求用最少的钱就可以买到相同数量的,相同(或更高)质量的珍珠. 珍珠的替代必须是连续的,不能跳跃 ...
- Maven相关内容学习笔记一:基本配置和使用
首先必须推荐的这本书<Maven实战> 许晓斌,机械工业出版社 Maven简介 其实使用Maven也有很久时间了,大部分都是别人建好了工程我使用一下,实际上并没有非常详细的使用经验,这次到 ...
- CodeForces Good Bye 2014 B. New Year Permutation
可能是因为这次没有分Div.1和Div.2,所以感觉题的难度比较大. 题意: 给出一个1~n的排列和一个邻接矩阵A,Aij = 1表示可以交换排列的第i项和第j项,问经过若干次交换后,求能够得到最小字 ...
- 各种好用的工具之一 ---- PNGGauntlet
1.PNGGauntlet实际上是一个图形前端,压缩图像的过程中使用的是PNGOUT, OptiPNG, 和DeflOpt这三款软件. 软件官网:http://pnggauntlet.com/ 可自行 ...