poj 3352(tarjan)
来源:互联网 发布:aspcms三合一网站源码 编辑:程序博客网 时间:2024/06/05 15:15
传送门
题意:加最少的边使原图(保证所有点连通)成为一个边-双连通图。
题解:用tarjan求双连通分量缩点(形成一棵无根树)后计算度数为1的节点(叶节点)的个数为cnt,那么答案为(cnt+1)/2。下面是证明:首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是cnt/2(向上取整)次,把所有点收缩到了一起。也就是(cnt+1)>>1。
本人觉得还是可以理解的,至少感性理解完全够了。。。
P.S.还有一个关键问题,本人基础不扎实,并不知道求边双连通分量的tarjan在更新时是否需要判”入栈”,反正判不判都能过(poj太水,有些错的代码都能过)。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>#include<stack>using namespace std;const int MAXN=1004;int n,m;int head[MAXN],etot;struct EDGE { int v,nxt;}e[MAXN<<1];int dfn[MAXN],low[MAXN],bel[MAXN],deg[MAXN],tim,bcc;stack<int > S;inline int read() { int x=0;char c=getchar(); while (c<'0'||c>'9') c=getchar(); while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); return x;}inline void adde(int u,int v) { e[etot].nxt=head[u],e[etot].v=v,head[u]=etot++; e[etot].nxt=head[v],e[etot].v=u,head[v]=etot++;}void dfs(int p,int fa) { low[p]=dfn[p]=++tim; S.push(p); for (int i=head[p];~i;i=e[i].nxt) { int v=e[i].v; if (!dfn[v]) { dfs(v,p); low[p]=min(low[p],low[v]); } else if (dfn[v]<dfn[p]&&v^fa) low[p]=min(low[p],dfn[v]); } if (dfn[p]==low[p]) { ++bcc; while (1) { int t=S.top(); S.pop(); bel[t]=bcc; if (t==p) break; } }}int main() {// freopen("poj 3352.in","r",stdin); while (~scanf("%d%d",&n,&m)) { etot=bcc=tim=0;// memset(instack,false,sizeof(instack)); memset(head,-1,sizeof(head)); memset(deg,0,sizeof(deg)); memset(dfn,0,sizeof(dfn)); while (!S.empty()) S.pop(); for (int i=0;i<m;++i) { int u=read(),v=read(); adde(u,v); } dfs(1,0);//under the premise that all nodes are connected for (int i=0;i<etot;i+=2) { int v=e[i].v,u=e[i^1].v; if (bel[u]^bel[v]) ++deg[bel[u]],++deg[bel[v]]; } int ans=0; for (int i=1;i<=bcc;++i) ans+=(deg[i]==1); printf("%d\n",ans+1>>1);//(int)ceil(1.0*ans/2) } return 0;}
阅读全文
0 0
- poj 3352(tarjan)
- POJ 3352 Road Construction(图论-tarjan)
- poj 3352 Road Construction (tarjan)
- poj 1236(tarjan算法)
- POJ 1144 Network(Tarjan)
- poj 1523 SPF (tarjan)
- POJ 1523 SPF(Tarjan)
- Tarjan算法解析(转)附POJ -- 3352
- POJ-3352 Road Construction (边双连通分量[Tarjan])
- poj 3352 Road Construction(Tarjan,边双连通分量)
- POJ 3352 Road Construction POJ 3177Redundant Paths(Tarjan缩点)
- poj 3177&&poj 3352加边构双联通(有重边)用tarjan 模板求的
- POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)
- poj 3117&&poj 3352 (边双连通分量+缩点 Tarjan算法 )
- poj 2553 (tarjan的应用)
- poj 2186(tarjan 算法实现)
- poj Nearest Common Ancestors(LCA+Tarjan)
- poj 1523 SPF(tarjan求割点)
- java内存模型
- 【SSH】Struts2学习(三)OGNL、OGNL与Struts2结合
- C程序优化
- 详解 CORS 跨域资源共享
- 初级 WINDOWS API C++语言版 编程(转)
- poj 3352(tarjan)
- Windows程序的基本结构(转)
- 杂项汇总-Maven篇(持续集成中...)
- 消息映射的实现(转)
- windows消息宏(转)
- 理解消息循环和窗口过程(转)
- VC基础学习:初学者指针指南(转)
- VC调试入门(转)
- Unity3D 物体移动的方法