题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量。

思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2。3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后的点。

代码:

/**/
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
struct Edge{
int u, v, next;
}edge[maxn << ];
int index, scc_cnt, tot; //scc_cnt记录SCC
int dfn[maxn], low[maxn], sccno[maxn], in[maxn], head[maxn];
stack<int> s;
void addEdge(int u, int v){
edge[tot].v = v;
edge[tot].u = u;
edge[tot].next = head[u];
head[u] = tot++;
}
void tarjan(int u, int pre){
dfn[u] = low[u] = ++index;
s.push(u);
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].v;
if(!dfn[v]){
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(v != pre){
low[u] = min(low[u], dfn[v]);
}
}
if(dfn[u] == low[u]){
scc_cnt++;
int a;
while(){
a=s.top();
s.pop();
sccno[a] = scc_cnt;
if(a == u) break;
}
}
} int main(){
int n, m;
scanf("%d%d", &n, &m);
index = scc_cnt = tot = ;
while(!s.empty()) s.pop();
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn));
memset(sccno, , sizeof(sccno));
for(int i = ; i < m; i++){
int u, v;
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
for(int i = ; i <= n; i++){
if(!dfn[i])
tarjan(i, );
}
memset(in, , sizeof(in));
for(int i = ; i < tot; i += ){
int u = edge[i].u, v = edge[i].v;
if(sccno[u] != sccno[v]){
in[sccno[u]]++;
in[sccno[v]]++;
}
}
int cnt = ;
for(int i = ; i <= scc_cnt; i++){
if(in[i] == ) cnt++;
}
printf("%d\n", (cnt + ) / );
return ;
}
/**/
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
struct Edge{
int u, v, next;
}edge[maxn << ];
int index, scc_cnt, tot; //scc_cnt记录SCC
int dfn[maxn], low[maxn], sccno[maxn], in[maxn], head[maxn];
stack<int> s;
map<int, int> mp[maxn];
int Find(int x){
return sccno[x] == x? x : sccno[x] = Find(sccno[x]);
}
void addEdge(int u, int v){
edge[tot].v = v;
edge[tot].u = u;
edge[tot].next = head[u];
head[u] = tot++;
}
void tarjan(int u, int pre){
dfn[u] = low[u] = ++index;
s.push(u);
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].v;
if(!dfn[v]){
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(v != pre){
low[u] = min(low[u], dfn[v]);
}
}
if(dfn[u] == low[u]){
scc_cnt++;
int a;
while(){
a=s.top();
s.pop();
sccno[a] = u;
if(a == u) break;
}
}
} int main(){
int n, m;
scanf("%d%d", &n, &m);
index = scc_cnt = tot = ;
while(!s.empty()) s.pop();
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn));
memset(sccno, , sizeof(sccno));
for(int i = ; i < m; i++){
int u, v;
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
for(int i = ; i <= n; i++){
if(!dfn[i])
tarjan(i, );
}
for(int i = ; i <= n; i++) mp[i].clear();
memset(in, , sizeof(in));
for(int i = ; i < tot; i += ){
int u = edge[i].u, v = edge[i].v;
if(u > v) swap(u, v);
mp[u][v]++;
if(mp[u][v] == ){
int fx = Find(u), fy = Find(v);
if(fx != fy){
sccno[fx] = fy;
}
}
}
for(int i = ; i < tot; i += ){
int u = edge[i].u, v = edge[i].v;
int fx = Find(u), fy = Find(v);
if(fx != fy){
in[fx]++;
in[fy]++;
}
}
int cnt = ;
for(int i = ; i <= n; i++){
if(sccno[i] == i && in[i] == ) cnt++;
}
printf("%d\n", (cnt + ) / );
return ;
}

poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解的更多相关文章

  1. poj3177 Redundant Paths 边双连通分量

    给一个无向图,问至少加入多少条边能够使图变成双连通图(随意两点之间至少有两条不同的路(边不同)). 图中的双连通分量不用管,所以缩点之后建新的无向无环图. 这样,题目问题等效于,把新图中度数为1的点相 ...

  2. POJ3177 Redundant Paths【双连通分量】

    题意: 有F个牧场,1<=F<=5000,现在一个牧群经常需要从一个牧场迁移到另一个牧场.奶牛们已经厌烦老是走同一条路,所以有必要再新修几条路,这样它们从一个牧场迁移到另一个牧场时总是可以 ...

  3. [POJ3177]Redundant Paths(双联通)

    在看了春晚小彩旗的E技能(旋转)后就一直在lol……额抽点时间撸一题吧…… Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Tota ...

  4. POJ3177 Redundant Paths —— 边双联通分量 + 缩点

    题目链接:http://poj.org/problem?id=3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total ...

  5. [POJ3177]Redundant Paths(双连通图,割边,桥,重边)

    题目链接:http://poj.org/problem?id=3177 和上一题一样,只是有重边. 如何解决重边的问题? 1.  构造图G时把重边也考虑进来,然后在划分边双连通分量时先把桥删去,再划分 ...

  6. poj 3177 Redundant Paths(边双连通分量+缩点)

    链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任 ...

  7. [POJ3352]Road Construction

    [POJ3352]Road Construction 试题描述 It's almost summer time, and that means that it's almost summer cons ...

  8. POJ3352 Road Construction(边双连通分量)

                                                                                                         ...

  9. POJ3352 Road Construction (双连通分量)

    Road Construction Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Sub ...

随机推荐

  1. maven国内稳定的阿里源

    <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexu ...

  2. TortoiseGit的使用

    TortoiseGit只是一个外壳而已,它调用的是msysgit,相当于msysgit的windows gui而已,如果喜欢用git命令行,那就不需要安装它. 所以要先安装msysgit(window ...

  3. CentOS配置JDK环境

    1 .下载JDK,jdk1.7.0_80.gz 2. mkdir -p /usr/lib/java 3.sudo chmod -R 777 /usr/lib/java 4.tar -zxvf jdk1 ...

  4. django的contenttype表

    https://blog.csdn.net/aaronthon/article/details/81714496 这篇文章已经非常详细了,供自己以后忘了...回看...... 总结: 当一张表和多个表 ...

  5. java.io.File实战

    There are many things that can go wrong: A class works in Unix but doesn't on Windows (or vice versa ...

  6. Android(三) 匹配屏幕密度

    过去,程序员通常以像素为单位设计计算机用户界面.例如:图片大小为80×32像素.这样处理的问题在于,如果在一个dpi(每英寸点数) 高的显示器上运行该程序,则用户界面会显得很小.在有些情况下,用户界面 ...

  7. fiddler抓包工具使用

    此工具用于抓取302等看不到的包. 设置: 步骤一 步骤二 重启fiddler软件,设置才有效. 设置谷歌浏览器,使浏览器的访问都经过fiddler.(fiddler就成了代理了)设置如下 隐藏图片的 ...

  8. 003-spring cloud gateway-概述、Route模型、网关初始化配置过程、基本原理

    一.概述 网关服务核心是将进入的请求正确合理的路由到下层具体的服务进行业务处理,由此可见网关服务的核心就是路由信息的构建. Spring Cloud Gateway旨在提供一种简单而有效的方式来路由到 ...

  9. Andrew Ng-ML-第十九章-应用举例:照片OCR(光学字符识别)

    1.问题描述与 OCR pipeline 图1.图像文字识别流水线 首先是输入图片->进行文字检测->字符分割->字符识别. 这些阶段分别需要1-5人这样子. 2.滑动窗口 主要讲滑 ...

  10. 因子分析(Factor Analysis)

    原文地址:http://www.cnblogs.com/jerrylead/archive/2011/05/11/2043317.html 1 问题 之前我们考虑的训练数据中样例的个数m都远远大于其特 ...