题意:有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. 【PyQt5-Qt Designer】QComboBox(下拉列表框) 使用模板

    import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * ###### ...

  2. QEMU IO事件处理框架

    Qemu IO事件处理框架 qemu是基于事件驱动的,在基于KVM的qemu模型中,每一个VCPU对应一个qemu线程,且qemu主线程负责各种事件的监听,这里有一个小的IO监听框架,本节对此进行介绍 ...

  3. jquery两稳定版本比较~~

    jquery历经了多个版本的更新,版本上的比较貌似没什么必要性,一般来说新的版本会比旧的版本各方面都略有提升,但由于新版中增加了各种新的功能,难免会引起bug的发生.评估一个版本是否适合当前开发场景使 ...

  4. gradle多工程依赖

    多工程依赖:http://blog.csdn.net/w8452960/article/details/53415415 https://www.cnblogs.com/softidea/p/4525 ...

  5. [LeetCode] 345. Reverse Vowels of a String_Easy tag:Two Pointers

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1: In ...

  6. pod 的warning一定要注意消除,要不然你的pod配置有问题无法导入

    and the CHANGELOG for this version http://git.io/BaH8pQ. Downloading dependencies Using BlocksKit (2 ...

  7. 混淆和加密.NET开发工具

    .NET开发的工具,可以用ILSpy等很轻松的反编译查看源码,为了保护自己写的软件,一般会对软件进行加密,不仅内部关键数据通过加密,软件开发完毕后,对软件也进行加密,防止别人很轻松的反编译和查看到比较 ...

  8. AnsiString和各种数据类型间相互转换 [数据转换]

    //Ansistring 转 char void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = " ...

  9. Codeforces Round #246 (Div. 2) D E

    这题说的是给了一个字符串当前缀和后缀相同的时候就计算此时的 整个串种拥有这样的子串友多少个,没想到用KMP解 用0开头的那种类型的 KMP 今天刚好也学了一下,因为KMP的作用是找出最长前缀 KMP ...

  10. Spring 自动扫描 不支持jar包 <component-scan>

    问题描述: 本机windows myeclipse10.7 运行非maven java项目,其中部分类使用spring3 注解进行注入,使用配置文件component-scan进行扫描,在本机debu ...