Road Construction

本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路。

题意:给你一副图,求最少需要加几条边使其变为边双连通图。

思路:kuangbin模板上有介绍,这里就不详细说明了。具体做法是tarjan缩点后求度为1(2)的数量ans,答案就是(ans+1)/2。

const int N=1e5+5;
struct edge
{
int to,next,f;
} e[N*2];
int ti,c,n,m,top,tot,bridge;
int head[N],low[N],dfn[N],belong[N],Stack[N],vis[N],du[N];
void add(int u,int v)
{
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
void init()
{
bridge=ti=c=top=tot=0;
memset(head,-1,sizeof(head));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(vis,0,sizeof(vis));
memset(du,0,sizeof(du));
memset(e,0,sizeof(e));
}
void tarjan(int u)
{
int v;
low[u]=dfn[u]=++ti;
vis[u]=1;
Stack[top++]=u;
for(int i=head[u]; i+1; i=e[i].next)
{
int v=e[i].to;
if(e[i].f) continue;
e[i].f=e[i^1].f=1;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]&&dfn[v]<low[u]) low[u]=dfn[v];
}
if(dfn[u]==low[u])
{
c++;
do
{
v=Stack[--top];
vis[v]=0;
belong[v]=c;
}
while(v!=u);
}
}
void solve()
{
for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
int ans=0;
for(int i=1; i<=n; i++)
for(int j=head[i]; j+1; j=e[j].next)
{
int v=e[j].to;
if(belong[i]!=belong[v])
{
du[belong[i]]++;
du[belong[v]]++;
}
}
for(int i=1; i<=c; i++)
{
// printf("%d %d\n",i,du[i]);
if(du[i]==2) ans++;
}
printf("%d\n",(ans+1)/2);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
int u,v;
while(m--)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
solve();
}
return 0;
}

POJ-3352 Road Construction,tarjan缩点求边双连通!的更多相关文章

  1. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  2. poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 503 ...

  3. Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)

     http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...

  4. POJ 3352 Road Construction(边双连通分量,桥,tarjan)

    题解转自http://blog.csdn.net/lyy289065406/article/details/6762370   文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...

  5. POJ 3352 Road Construction(边—双连通分量)

    http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...

  6. POJ 3177 Redundant Paths POJ 3352 Road Construction

    这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...

  7. POJ - 3352 Road Construction(边双连通分支)

    1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2.POJ - 3177 Redundant Paths(边双连通分支)(模板)  与这道题一模一样.代码就改了下范围,其他都没动 ...

  8. POJ 3352 Road Construction 双联通分量 难度:1

    http://poj.org/problem?id=3352 有重边的话重边就不被包含在双连通里了 割点不一定连着割边,因为这个图不一定是点连通,所以可能出现反而多增加了双连通分量数的可能 必须要用割 ...

  9. poj 3353 Road Construction tarjan 边双联通分支 缩点+结论

    题意:一个连通的无向图,求至少需要添加几条边,救能保证删除任意一条边,图仍然是连通的. 链接:http://poj.org/problem?id=3352 思路:边的双连通图.其实就是要求至少添加几条 ...

随机推荐

  1. SourceInsight主题设置

    自己经常忘记怎样设置SourceInsight主题,这次一定要记住! 0. 退出SourceInsight软件1. 替换配置文件操作:拷贝Global.CF3到“我的文档\Source Insight ...

  2. Objective-C Fast Enumeration

    Fast enumeration is an Objective-C's feature that helps in enumerating through a collection. So in o ...

  3. uvm_reg_defines——寄存器模型(四)

    文件: src/marcos/uvm_reg_defines 类: 无 该文件是寄存器模型src/reg/* 文件对于的宏文件,主要定义了寄存器地址位宽,寄存器数据位宽,字节的大小.计算机从最初的8, ...

  4. /etc/default/useradd

    系统默认的shell在,/etc/default/useradd 中,添加用户的时候如果不指定shell,默认的shell就是该文件下制定的文件

  5. siege4安装和使用介绍

    使用文档参考地址:https://www.joedog.org/siege-manual/ siege4地址:http://download.joedog.org/siege/ cd /usr/loc ...

  6. mongodb-3.2.8 单机复制集安装

    规划: replSet 复制集名称: rs1 MongoDB数据库安装安装路径为:/usr/local/mongodb/ 复制集成员IP与端口: 节点1: localhost:28010   (默认的 ...

  7. 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows

    题目描述 Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths tha ...

  8. fluent_python1

    Magic Method python中有些跟对象本身有关的方法, 以两个下划线开始,两个下划线结束, 一般称为魔法方法(magic method). 比如 obj[key] 的背后就是 __geti ...

  9. 从汇编看c++中的多态

    http://www.cnblogs.com/chaoguo1234/archive/2013/05/19/3079078.html 在c++中,当一个类含有虚函数的时候,类就具有了多态性.构造函数的 ...

  10. [已解决]gitee初次使用git clone报错

    本文描述的错误按实际出现先后顺序排列,并且附上一些其他可能会出现的问题 错误1: JZKJ@DESKTOP-I7Q9QJ4 MINGW64 ~ $ git clone https://gitee.co ...