hdu 4005 边连通度与缩点
思路:先将图进行缩点,建成一颗树,那么如果这是一条单路径树(即最大点度不超过2),就不在能删的一条边,使得不连通。因为将其头尾相连,形成一个圈,那么删任意一条边,图都是连通的。
上面的是无解的情况,如果有解,那么这个解一定是树中不全在一条路径上的三条边中的一条,使得这三条边中的最大边最小,即得解。同样,对任意一个节点,其三个子树上的边一定是三条不全在一条路径上的边。问题就转化为求一个节点的第三小边。
但直接求第三小边容易出错,并且不易求得。我们可以先选一条树中的最小边,这条边一定是三条边中的一条,我们就沿着这条边的两个端点找。那么问题就又变成了求一个节点的次小边了。这个很容易求得。
感谢http://www.cnblogs.com/wuyiqi/archive/2011/11/04/2235671.html提供的测试数据。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define Maxn 10010
#define Maxm 200010
#define inf 0x7fffffff
using namespace std;
int dfn[Maxn],low[Maxn],vi[Maxn],head[Maxn],Stack[Maxn],id[Maxn],degree[Maxn],lab,e,n,top,num,m,ans,wer[Maxn][];
struct Edge{
int u,v,next,f,val;
}edge[Maxm];
void init()
{
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(vi,,sizeof(vi));
memset(id,,sizeof(id));
memset(degree,,sizeof(degree));
memset(head,-,sizeof(head));
lab=top=e=num=;
ans=inf;
}
inline int Max(int a,int b,int c)
{
int temp=a>b?a:b;
return temp>c?temp:c;
}
void add(int u,int v,int val)
{
edge[e].u=u,edge[e].v=v,edge[e].f=,edge[e].val=val,edge[e].next=head[u],head[u]=e++;
edge[e].u=v,edge[e].v=u,edge[e].f=,edge[e].val=val,edge[e].next=head[v],head[v]=e++;
}
void Tarjan(int u)
{
int i,v;
dfn[u]=low[u]=++lab;
Stack[top++]=u;
vi[u]=;
for(i=head[u];i!=-;i=edge[i].next)
{
if(edge[i].f) continue;
edge[i].f=edge[i^].f=;
v=edge[i].v;
if(!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
if(vi[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
++num;
do{
i=Stack[--top];
id[i]=num;
vi[i]=;
}while(i!=u);
}
}
int dfs(int u,int f)
{
int i,v;
int temp;
wer[u][]=wer[u][]=inf;
for(i=head[u];i!=-;i=edge[i].next)
{
v=edge[i].v;
if(v==f) continue;
temp=edge[i].val;
temp=min(temp,dfs(v,u));
if(temp<wer[u][])
{
wer[u][]=wer[u][];
wer[u][]=temp;
}
else
if(temp<wer[u][])
wer[u][]=temp;
ans=min(ans,wer[u][]);
}
return wer[u][];
}
int solve()
{
int i,j,u,v;
Tarjan();
int en=e;
int Maxdegree=;
memset(head,-,sizeof(head));
e=;
int Minedge=inf,choice;
for(i=;i<en-;i+=)
{
u=edge[i].u,v=edge[i].v;
if(id[u]!=id[v])
{
add(id[u],id[v],edge[i].val);
if(edge[i].val<Minedge)
{
Minedge=edge[i].val,choice=e-;
}
degree[id[u]]++,degree[id[v]]++;
Maxdegree=Max(Maxdegree,degree[id[u]],degree[id[v]]);
}
}
if(Maxdegree<=)
return ;
u=edge[choice].u,v=edge[choice].v;
dfs(u,v);
dfs(v,u);
return ;
}
int main()
{
int i,j,a,b,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
if(!solve())
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}
/*
6
2 2
3 6
4 3
5 4
6 5
7 7
15 14
2 7
4 5
5 6
8 1
9 2
10 3
11 4
3 8
6 9
7 1
12 11
13 12
14 13
15 14
16 15
2 7
4 5
5 6
8 1
9 2
10 3
11 4
16 1
3 6
6 9
7 1
12 11
13 12
14 13
15 14
6 5
2 1
3 2
4 3
5 4
6 5
9 8
2 1
6 5
7 7
8 4
9 6
3 2
4 5
5 6
9 8
2 1
6 5
7 7
8 4
9 4
3 2
4 5
5 6
4*/
hdu 4005 边连通度与缩点的更多相关文章
- HDU 4005 The war(双连通好题)
HDU 4005 The war pid=4005" target="_blank" style="">题目链接 题意:给一个连通的无向图.每条 ...
- hdu 4612 边连通度缩点+树的最长路径
思路:将以桥为分界的所有连通分支进行缩点,得到一颗树,求出树的直径.再用树上的点减去直径,再减一 #pragma comment(linker, "/STACK:1024000000,102 ...
- HDU 4005 The war 双连通分量 缩点
题意: 有一个边带权的无向图,敌人可以任意在图中加一条边,然后你可以选择删除任意一条边使得图不连通,费用为被删除的边的权值. 求敌人在最优的情况下,使图不连通的最小费用. 分析: 首先求出边双连通分量 ...
- hdu 4005 The war
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 In the war, the intelligence about the enemy is ...
- hdu 4005(边双连通)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 思路:首先考虑边双连通分量,如果我们将双连通分量中的边删除,显然我们无法得到非连通图,因此要缩点 ...
- HDU 4005 The war Tarjan+dp
The war Problem Description In the war, the intelligence about the enemy is very important. Now, o ...
- hdu 4005 双联通 2011大连赛区网络赛E *****
题意: 有一幅图,现在要加一条边,加边之后要你删除一条边,使图不连通,费用为边的费用,要你求的是删除的边的最小值的最大值(每次都可以删除一条边,选最小的删除,这些最小中的最大就为答案) 首先要进行缩点 ...
- hdu 4635 强连通度缩点
思路:想用Tarjan算法进行缩点,并记录每个连通分支的点数.缩点完毕过后,找出所有出度或入度为0的连通分量,假设该连通分量的点数为num[i],那么 ans=Max(ans,(n-num-1)*(n ...
- HDU 3836 Equivalent SetsTarjan+缩点)
Problem Description To prove two sets A and B are equivalent, we can first prove A is a subset of B, ...
随机推荐
- HDU 5680 zxa and set (数学 推导结论)
zxa and set 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/G Description zxa has a set , ...
- WinAPI: FindWindow、FindWindowEx - 查找窗口
FindWindow( lpClassName, {窗口的类名} lpWindowName: PChar {窗口的标题} ): HWND; {返回窗口的 ...
- 关于TCP主动关闭连接中的wait_timeout
首先我们先来回顾一下tcp关闭连接的过程: 假设A和B连接状态为EST,A需要主动关闭: A发送FIN给B,并将状态更改为FIN_WAIT1, B接收到FIN将状态更改为CLOSE_WAIT,并回复A ...
- 在WinForm中使用Web Service来实现软件自动升级
来源:互联网 winform程序相对web程序而言,功能更强大编程更方便,但软件更新却相当麻烦,要到客户端一台一台地升级,面对这个实际问题,在最近的一个小项目中,本人设计了一个通过软件实现自动升级技术 ...
- wiki1169-传纸条(dp)
http://wikioi.com/problem/1169/ 四维数组和三维数组: #include<iostream> #include<cstdio> #include& ...
- LCD_ILI9320横竖屏方向的问题。
发现仅仅设置R03H是不能设置方向的,还需要设置R32H,R33H的坐标位置. 比如我现在是 R03H=0x1000H,R20H=239-x,R21H=319-y:竖直正向 R03H=0x1030H, ...
- C#小常识集锦(一)--《更锋利的CSharp代码》读书笔记
❀.采用类似匈牙利命名法为控件命令,对于其他变量申明则不需要了 e.g. Button btnAdd; e.g. TextBox txtUsername; e.g. ListBox lstFileNa ...
- [Redux + Webpack] Hot reloading Redux Reducers with Webpack
Webpack will hot reload the component, but the reducer we need hard refresh. To sovle the problem, g ...
- NSRange,判断字符串的各种操作~
今天写的都比较简单,偶尔偷一下懒,猪真的很懒啊~ - (void)viewDidLoad { [super viewDidLoad]; //抽取指定范围的字符串 NSString *string1 ...
- CISCO 双线接入MAP配置详解
随着我国宽带技术的普及,各个公司都会有一至二条Internet接入线路,这些线路可能由电信.网通.长宽.联通等不同的IS提供,尽管他们在局端采用的技术可能有不同,但对客户而言都是同样接入方式,以太 ...