poj 3177 Redundant Paths(tarjan边双连通)

题目链接:http://poj.org/problem?id=3177

题意:求最少加几条边使得没对点都有至少两条路互通。

题解:边双连通顾名思义,可以先求一下连通块显然连通块里的点都是双连通的,然后就是各个连通块之间的问题。

也就是说只要求一下桥,然后某个连通块桥的个数位1的总数,结果就是(ans+1)/2。为什么是这个结果自行画图

理解一下,挺好理解的。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1e5 + 10;
const int M = 2e5 + 10;
struct TnT {
    int v , next;
    bool cut;
}edge[M];
int head[N] , e;
int Low[N] , DFN[N] , Stack[N] , Belong[N];
bool Instack[N];
int Index , top , bridge , block;
void init() {
    memset(head , -1 , sizeof(head));
    e = 0;
}
void add(int u , int v) {
    edge[e].v = v , edge[e].next = head[u] , edge[e].cut = false , head[u] = e++;
}
void Tarjan(int u , int pre) {
    int v;
    Low[u] = DFN[u] = ++Index;
    Stack[top++] = u;
    Instack[u] = true;
    for(int i = head[u] ; i != -1 ; i = edge[i].next) {
        v = edge[i].v;
        if(v == pre) continue;
        if(!DFN[v]) {
            Tarjan(v , u);
            Low[u] = min(Low[u] , Low[v]);
            if(Low[v] > DFN[u]) {
                bridge++;
                edge[i].cut = true;
                edge[i^1].cut = true;
            }
        }
        else if(Instack[v]) Low[u] = min(Low[u] , DFN[v]);
    }
    if(Low[u] == DFN[u]) {
        block++;
        do {
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] =  block;
        } while(v != u);
    }
}
int du[N];
int main() {
    int f , r;
    while(~scanf("%d%d" , &f , &r)) {
        init();
        for(int i = 0 ; i < r ; i++) {
            int u , v;
            scanf("%d%d" , &u , &v);
            add(u , v);
            add(v , u);
        }
        memset(DFN , 0 , sizeof(DFN));
        memset(Instack , false , sizeof(Instack));
        memset(du , 0 , sizeof(du));
        Index = 0 , block = 0 , top = 0;
        for(int i = 1 ; i <= f ; i++)
            if(!DFN[i]) Tarjan(i , i);
        for(int i = 1 ; i <= f ; i++) {
            for(int j = head[i] ; j != -1 ; j = edge[j].next) {
                if(edge[j].cut) {
                    du[Belong[i]]++;
                }
            }
        }
        int ans = 0;
        for(int i = 1 ; i <= block ; i++) {
            if(du[i] == 1) {
                ans++;
            }
        }
        printf("%d\n" , (ans + 1) / 2);
    }
    return 0;
}
时间: 2024-09-29 05:08:37

poj 3177 Redundant Paths(tarjan边双连通)的相关文章

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点) ACM 题目地址: POJ 3352 Road Construction POJ 3177 Redundant Paths 题意: 问要添加几条边才能使所给无向图图变成边双连通图. 分析: 边连通度:使无向图G不连通的最少删边数量为其边连通度. 边双连通图:边连通度大于1的无向图. 首先缩点,让图变成一个DAG. 现在问题转化为:在树中至少添加多少条边能使图变

Poj 3352 Road Construction &amp; Poj 3177 Redundant Paths(边双连通分量+缩点)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9465   Accepted: 4699 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro

POJ 3177 Redundant Paths(边双连通分量)

[题目链接] http://poj.org/problem?id=3177 [题目大意] 给出一张图,问增加几条边,使得整张图构成双连通分量 [题解] 首先我们对图进行双连通分量缩点, 那么问题就转化为给出一棵树,加边使得其成为边双连通分量的最小边数, 只要从叶节点连一条边到任意节点,那么就可以使得这个叶节点加入到双连通分量中, 那么优先叶节点和叶节点连接,所以其答案为(叶节点+1)/2 [代码] #include <cstdio> #include <algorithm> #in

POJ 3177 Redundant Paths 无向图边双联通基础题

题意: 给一个无向图,保证任意两个点之间有两条完全不相同的路径 求至少加多少边才能实现 题解: 得先学会一波tarjan无向图 桥的定义是:删除这条边之后该图不联通 一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足 DFN(u)<Low(v).(因为 v 想要到达 u 的父亲必须经过(u,v)这条边,所以删去这条边,图不连通) 先用Tarjan无向图缩边双联通分量,这样原图就构成了一颗树, 对于树的叶子节点来说,显然他们需要连边,可以证明的是,我们连至多(叶子节点个数+1)/2的边就

POJ 3177 Redundant Paths (tarjan无向图求缩点)

#include <iostream> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<cstdio> #include<stack> #include <map> #define lson(p) (p<<1) #define rson(p) (p<<1|1) #define

POJ 3177 Redundant Paths POJ 3352 Road Construction(双连通)

POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的,一份代码能交,给定一个连通无向图,问加几条边能使得图变成一个双连通图 思路:先求双连通,缩点后,计算入度为1的个数,然后(个数 + 1) / 2 就是答案(这题由于是只有一个连通块所以可以这么搞,如果有多个,就不能这样搞了) 代码: #include <cstdio> #include <cstring> #include <algorithm&

tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accepted: 5330 Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the re

POJ 3177 Redundant Paths(Tarjan)

题目链接 题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 . 思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1.然后找到所有叶子节点,即度数为1的节点的个数leaf,最后要添加的边的条数就是(leaf+1)/2 : 1 // 3177 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using

poj 3177 Redundant Paths

Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forc