题目大意:一个图,要求你加入最少的边,使得最后得到的图为一个边双连通分支。所谓的边双连通分支,即不存在桥的连通分支(题目保证数据中任意两点都联通)。

解题思路:先用tarjan算法进行缩点建立DAG图, 然后再进行寻找度为1的点有个数x, 那么需要添加的边即为(x+1)/ 2;

起初这样写, 一直WA,然后发现下面两个数据,发现并不能过。

#include <stdio.h>
#include <set>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std; const int N = ;
vector<int>G[N];
vector<pair<int, int> >DAG;
int dfn[N], low[N], mk[N];
int tot;
int n, m; void init()
{
tot = ;
DAG.clear();
for(int i=; i<=n; ++ i)
{
mk[i] = ;
G[i].clear();
dfn[i] = low[i] = -;
}
} void tarjan(int u, int f)
{
dfn[u] = low[u] = ++ tot;
for(int i = ; i < G[u].size(); ++ i)
{
int v = G[u][i];
if(dfn[v] == -)
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
if(dfn[u] < low[v])
DAG.push_back(make_pair(low[u], low[v]));
}
else if(v != f)
low[u] = min(low[u], dfn[v]);
}
} void solve()
{
init();
for(int i=; i<=m; ++ i)
{
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
tarjan(, -);
for(int i=; i<DAG.size(); ++ i)
{
pair<int, int> S = DAG[i];
mk[S.first] ++, mk[S.second] ++;
}
int ans = ;
for(int i=; i<=n; ++ i)
{
if(mk[i] == )
ans ++;
}
printf("%d\n", (ans + ) / );
} int main()
{
while(scanf("%d%d", &n, &m) != EOF)
solve();
return ;
}

需要特别注意两组数据:

2 2

1 2

1 2

2 1

1 2

答案分别是:

0

1

代码如下:

#include <stdio.h>
#include <set>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std; const int N = ;
vector<int>G[N];
int dfn[N], low[N], mk[N];
int tot;
int n, m; void init()
{
tot = ;
for(int i=; i<=n; ++ i)
{
mk[i] = ;
G[i].clear();
dfn[i] = low[i] = -;
}
} void tarjan(int u, int f)
{
dfn[u] = low[u] = ++ tot;
for(int i = ; i < G[u].size(); ++ i)
{
int v = G[u][i];
if(dfn[v] == -)
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(v != f)
low[u] = min(low[u], dfn[v]);
}
} void solve()
{
init();
for(int i=; i<=m; ++ i)
{
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
tarjan(, -);
for(int i = ; i <= n; ++ i)
{
for(int j = ; j < G[i].size(); ++ j)
{
if(low[i] != low[G[i][j]])
mk[low[i]] ++;
}
}
int ans = ;
for(int i = ; i <= n; ++ i)
if(mk[i] == )
ans ++;
printf("%d\n", (ans + ) / );
} int main()
{
while(scanf("%d%d", &n, &m) != EOF)
solve();
return ;
}

POJ 3352-Road Construction (图论-双边联通分支算法)的更多相关文章

  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 双联通分量 难度:1

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

  5. POJ 3352 Road Construction ; POJ 3177 Redundant Paths (双联通)

    这两题好像是一样的,就是3177要去掉重边. 但是为什么要去重边呢??????我认为如果有重边的话,应该也要考虑在内才是. 这两题我用了求割边,在去掉割边,用DFS缩点. 有大神说用Tarjan,不过 ...

  6. POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)

    Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...

  7. poj 3352 Road Construction(边双连通分量+缩点)

    题目链接:http://poj.org/problem?id=3352 这题和poj 3177 一样,参考http://www.cnblogs.com/frog112111/p/3367039.htm ...

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

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

  9. 【边双连通】poj 3352 Road Construction

    http://poj.org/problem?id=3352 [题意] 给定一个连通的无向图,求最少加多少条边使得这个图变成边双连通图 [AC] //#include<bits/stdc++.h ...

随机推荐

  1. 自动选中div中的文字

    <html> <head> <title></title> <script type="text/javascript" de ...

  2. Docker 安装及命令

    CentOS 安装 通过脚本安装:curl -sSL https://get.docker.com/ | sh通过yum安装:yum install docker-engine============ ...

  3. dbms_stats包更新、导出、导入、锁定统计信息

    dbms_stats包问世以后,我们可通过一种新的方式来为CBO收集统计数据.目前,已经不再推荐使用老式的Analyze分析表和dbms_utility方法来生成CBO统计数据.dbms_stats能 ...

  4. pt-online-schema-change 实例

    pt-pmp (http://www.cnblogs.com/ivictor/p/6012183.html) pt-online-schema-change (http://blog.csdn.net ...

  5. Spring mvc创建的web项目,如何获知其web的项目名称,访问具体的链接地址?

    Spring mvc创建的web项目,如何获知其web的项目名称,访问具体的链接地址? 访问URL:  http://localhost:8090/firstapp/login 在eclipse集成的 ...

  6. 获取本机MAC地址

    <%@ page contentType="text/html; charset=GBK" %><html><head><meta htt ...

  7. Datagridview 添加checkbox列,并判断Datagridview 中的checkbox列是否被选中

    Solution1://In Fill DataGridViewEvent : DataGridViewCheckBoxColumn ChCol = new DataGridViewCheckBoxC ...

  8. codeforces 659 G. Fence Divercity 组合数学 dp

    http://codeforces.com/problemset/problem/659/G 思路: f(i,0/1,0/1) 表示到了第i个,要被切的块开始了没有,结束了没有的状态的方案数 递推看代 ...

  9. FileUploadInterceptor拦截器的笔记

    当请求表单中包含一个文件file,FileUploadInterception拦截器会自动应用于这个文件. 表单: <s:form namespace="/xxx" acti ...

  10. python学习笔记(一)

    1. BeautifulSoup是一个很好用的Python写的一个HTML/XML的解析器,它可以处理不规范标记并生成剖析树(parse tree).Beautifulsoup可以对便签Object进 ...