题意:

有一个边带权的无向图,敌人可以任意在图中加一条边,然后你可以选择删除任意一条边使得图不连通,费用为被删除的边的权值。

求敌人在最优的情况下,使图不连通的最小费用。

分析:

首先求出边双连通分量,缩点成树。

敌人如果选则树中\(u,v\)节点之间加一条边,则路径\(u \to v\)中所有的边都会变成环。

我们只能考虑删除其他的权值最小的边使图不连通。

从敌人的角度考虑:如果使树中权值最小的边成环,那么我们的费用会增加。在最小边成环的前提下,如果还能使次小边也成环,我们的费用又会增加,以此类推。

因此先找到最小边,然后从这条边两头DFS,肯定选择最小值所在的子树延长路径,然后用其他子树中的最小边更新答案。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std; const int INF = 0x3f3f3f3f;
const int maxn = 10000 + 10;
const int maxm = 200000 + 10; struct Edge
{
int v, w, nxt;
Edge() {}
Edge(int v, int w, int nxt): v(v), w(w), nxt(nxt) {}
}; struct Graph
{
int ecnt, head[maxn];
Edge edges[maxm]; void init() { ecnt = 0; memset(head, -1, sizeof(head)); } void AddEdge(int u, int v, int w) {
edges[ecnt] = Edge(v, w, head[u]);
head[u] = ecnt++;
}
}; Graph g, t; int n, m; stack<int> S;
int dfs_clock, pre[maxn], low[maxn];
int scc_cnt, sccno[maxn]; void dfs(int u, int fa) {
pre[u] = low[u] = ++dfs_clock;
S.push(u);
bool flag = false;
for(int i = g.head[u]; ~i; i = g.edges[i].nxt) {
int v = g.edges[i].v;
if(v == fa && !flag) { flag = true; continue; }
if(!pre[v]) {
dfs(v, u);
low[u] = min(low[u], low[v]);
} else if(!sccno[v]) low[u] = min(low[u], pre[v]);
} if(low[u] == pre[u]) {
scc_cnt++;
for(;;) {
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} void find_scc() {
memset(pre, 0, sizeof(pre));
memset(sccno, 0, sizeof(sccno));
dfs_clock = scc_cnt = 0;
dfs(1, 0);
} int ans; int dfs2(int u, int fa) {
int min1 = INF, min2 = INF;
for(int i = t.head[u]; ~i; i = t.edges[i].nxt) {
int v = t.edges[i].v;
if(v == fa) continue;
int tmp = t.edges[i].w;
tmp = min(tmp, dfs2(v, u));
if(tmp < min2) min2 = tmp;
if(min2 < min1) swap(min1, min2);
}
ans = min(ans, min2);
return min1;
} int main()
{
while(scanf("%d%d", &n, &m) == 2) {
g.init();
while(m--) {
int u, v, w; scanf("%d%d%d", &u, &v, &w);
g.AddEdge(u, v, w);
g.AddEdge(v, u, w);
} find_scc(); t.init();
int a, b, minEdge = INF;
for(int u = 1; u <= n; u++) {
for(int i = g.head[u]; ~i; i = g.edges[i].nxt) {
int v = g.edges[i].v;
if(sccno[u] == sccno[v]) continue;
int w = g.edges[i].w;
t.AddEdge(sccno[u], sccno[v], w);
if(w < minEdge) { minEdge = w; a = sccno[u]; b = sccno[v]; }
}
} ans = INF;
dfs2(a, b);
dfs2(b, a);
if(ans == INF) ans = -1;
printf("%d\n", ans);
} return 0;
}

HDU 4005 The war 双连通分量 缩点的更多相关文章

  1. HDU 4005 The war(双连通好题)

    HDU 4005 The war pid=4005" target="_blank" style="">题目链接 题意:给一个连通的无向图.每条 ...

  2. HDU 2460 Network 边双连通分量 缩点

    题意: 给出一个无向连通图,有\(m\)次操作,每次在\(u, v\)之间加一条边,并输出此时图中桥的个数. 分析: 先找出边双连通分量然后缩点得到一棵树,树上的每条边都输原图中的桥,因此此时桥的个数 ...

  3. HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)

    Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...

  4. POJ3177 Redundant Paths(边双连通分量+缩点)

    题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...

  5. 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)

    layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...

  6. HDU 4612 Warm up (边双连通分量+缩点+树的直径)

    <题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...

  7. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  8. Hdu4005-The war(双连通缩点)

    In the war, the intelligence about the enemy is very important. Now, our troop has mastered the situ ...

  9. POJ3694 Network(边双连通分量+缩点+LCA)

    题目大概是给一张图,动态加边动态求割边数. 本想着求出边双连通分量后缩点,然后构成的树用树链剖分+线段树去维护路径上的边数和..好像好难写.. 看了别人的解法,这题有更简单的算法: 在任意两点添边,那 ...

随机推荐

  1. 用gethub下载ardupilot的最新源码

    1进入gethub的官方网站https://github.com/作者:恒久力行 QQ:624668529    在搜索框内输入ardupilot并点击搜索点回车       2会看到很多工程,选择那 ...

  2. pandas:数据分析

    一.介绍 pandas是一个强大的Python数据分析的工具包,是基于NumPy构建的. 1.主要功能 具备对其功能的数据结构DataFrame.Series 集成时间序列功能 提供丰富的数学运算和操 ...

  3. mathtype 章节号 Equation Chapter 1 Section 1 的去除

    mathtype 章节号 Equation Chapter 1 Section 1 的去除 转:http://hi.baidu.com/17ximm/blog/item/2882413e92fc96c ...

  4. swagger + springboot

    参考文章:  https://blog.csdn.net/xupeng874395012/article/details/68946676/ https://blog.csdn.net/hry2015 ...

  5. mybatis-mybatis-config.xml详细介绍

    1.mybatis-config.xml 1.1:配置,配置可以是引入外部文件,也可以是在本文件内写配置 <!-- <properties resource="jdbc.prop ...

  6. 9 Palindrome_Number

    Determine whether an integer is a palindrome. Do this without extra space. 判断一个数是否是回文数. public class ...

  7. TFS看板晨会

    迭代任务看板 打开任务看板 打开燃尽图查看剩余工作情况,如果离发布较近,但是还有很多剩余工作,可能需要提前准备移除一部分优先级低的需求,如果剩余工作较少,适当安排一些需求 任务板按照人员分组,查看每个 ...

  8. 《Ruby on Rails教程》学习笔记

    本文是我在阅读 Ruby on Rails 教程的简体中文版时所做的摘录,以及学习时寻找的补充知识.补充知识主要来自于 Ruby on Rails 實戰聖經. Asset Pipeline 在最新版 ...

  9. 【CF1000C】Covered Points Count(离散化+差分)

    点此看题面 大致题意: 给出\(n\)条线段,分别求有多少点被覆盖\(1\)次.\(2\)次...\(n\)次. 正常的算法 好吧,这道题目确实有个很简单的贪心做法(只可惜我做的时候没有想到,结果想了 ...

  10. Hive之数据模型

    (本文是基于多篇文章根据个人理解进行的整合,参考的文章见末尾的整理) 数据模型 hive的数据模型包括:database.table.partition和bucket. 1.Database:相当于关 ...