题意:有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. 食物链--poj1182(并查集含有关系)

    http://poj.org/problem?id=1182   题意应该就不用说了  再次回到食物链这道题,自己写了一遍,一直wa...原因竟然是不能用多实例,我也是醉了,但是我真的彻底的理解了,那 ...

  2. you

    抑制幽门螺旋杆菌: 1.西兰花 鲜嫩的西兰花蔬菜,含有一种物质叫异硫氰酸酯,这种物质就是幽门螺杆菌的 " 天敌 ",可达到百分百抑制的作用,甚至还有医生给了它一个最强天然抗生素的称 ...

  3. python2与python3之间的主要区别

    python2与python3之间的主要区别 print py2:print语句,语句就意味着可以直接跟要打印的东西,如果后面接的是一个元组对象,直接打印 py3:print函数,函数就以为这必须要加 ...

  4. CentOS忘记普通用户密码解决办法

    普通用户忘记密码 1.使用root用户登录系统,找到/etc/shadow文件. 2.找到用户名开头的那一行,例如我的用户名为pds,,以冒号为分割符,红色部分是密码加密部分 pds:$1$Civop ...

  5. [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈

    Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, ...

  6. X-Forwarded-For 负载均衡 7 层 HTTP 模式获取来访客户端真实 IP 的方法(IIS/Apache/Nginx/Tomcat)

    https://help.aliyun.com/knowledge_detail/13051859.html?pos=1 1.IIS 6 配置方案2.IIS 7 配置方案3.Apache 配置方案4. ...

  7. ssh 远程执行命令

    SSH 是 Linux 下进行远程连接的基本工具,但是如果仅仅用它来登录那可是太浪费啦!SSH 命令可是完成远程操作的神器啊,借助它我们可以把很多的远程操作自动化掉!下面就对 SSH 的远程操作功能进 ...

  8. 机器学习理论基础学习15---条件随机场(CRF)

    一.CRF的由来HMM->MEMM->CRF 二.HMM到MEMM MEMM打破了HMM的观测条件独立假设 三.MEMM到CRF CRF克服了MEMM的label bias problem ...

  9. Microsoft Interview第一轮

    上来随意交谈了一小会儿,开了点小玩笑,chat了一些关于他们recruter行程的话题,缓和了一下气氛. 进入正题,问了做的research的方向,我说是DLT,然后大概给他讲解了一下具体是什么, 跟 ...

  10. 55. Jump Game(贪心)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...