POJ 3177 Redundant Paths 边双连通分支

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

题目大意是 给一个无向图,求至少要添加多少条边才能使其变为边双连通图

边双连通图简单来说就是联通且没有割边(桥)

图是连通的,并且没有给重边(所以程序中没有处理重边)

思想是缩环,然后统计有多少个叶子节点,答案为(叶子节点数目+1)/ 2

统计叶子的过程是先找出所有的割边

然后逐个遍历点,遍历边,若边是割边则去点所在的scc的度数加一

这样到最后度数为1的scc就是叶子

完全参考自kuangbin模板

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <ctime>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1;
    char c = getchar();
    while((c < ‘0‘ || c > ‘9‘) && c != ‘-‘ && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == ‘-‘) c = getchar(), tmp = -1;
    while(c >= ‘0‘ && c <= ‘9‘) x *= 10, x += (c - ‘0‘),c = getchar();
    n = x*tmp;
    return true;
}

template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar(‘-‘);
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------

const int maxn = 5010;
const int maxm = 20010;
struct Edge
{
    int to, next;
    bool cut;
}edge[maxm];

int head[maxn], tot;
int Low[maxn], DFN[maxn], Stack[maxn], Belong[maxn];
int Index, top;
int scc;
int bridge;
bool Instack[maxn];
int num[maxn];

void addedge(int u, int v)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    edge[tot].cut = false;
    head[u] = tot++;
    edge[tot].to = u;
    edge[tot].next = head[v];
    edge[tot].cut = false;
    head[v] = tot++;
}

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].to;
        if(v == pre)
            continue;
        if(!DFN[v])
        {
            Tarjan(v, u);
            if(Low[u] > Low[v])
                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] > Low[v])
            Low[u] = Low[v];
    }
    if(Low[u] == DFN[u])
    {
        scc++;
        do
        {
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = scc;
            num[scc]++;
        }
        while(v != u);
    }
}

int du[maxn];

void solve(int N)
{
    memset(DFN, 0, sizeof(DFN));
    memset(Instack, 0, sizeof(Instack));
    memset(num, 0, sizeof(num));
    Index = scc = top = 0;
    for(int i = 1; i <= N; i++)
    {
        if(!DFN[i])
            Tarjan(i, 0);
    }

    for(int i = 1; i <= N; i++)
    {
        for(int j = head[i]; j != -1; j = edge[j].next)
        {
            if(edge[j].cut) //若为桥,则去点所在的scc的度+1
                du[Belong[edge[j].to]]++;
        }
    }

    int leaf = 0;
    for(int i = 1; i <= scc; i++)   //找“叶子” 即度为1的节点
    {
        if(du[i] == 1)
            leaf++;
    }

    printf("%d\n", (leaf + 1)/ 2);
}

void init()
{
    tot = 0;
    bridge = 0;
    memset(head, -1, sizeof(head));
}

int main()
{
    //freopen("in.txt", "r", stdin);

    int F, R;
    scanf("%d%d", &F, &R);

    init();
    for(int i = 0; i < R; i++)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        addedge(u, v);
    }

    solve(F);

    return 0;
}
时间: 2024-10-11 11:47:44

POJ 3177 Redundant Paths 边双连通分支的相关文章

Poj 3177 Redundant Paths (双连通分支+节点统计)

题目描述: 给出一个无向的连通图,问最少加入几条边,才能使所给的图变为无桥的双连通图? 解题思路: 可以求出原图中所有的不包含桥的所有最大连通子图,然后对连通子图进行标记缩点,统计度为1的叶子节点leaf有多少个,答案就是(leaf+1)/2: 这个题目有重边,在处理的时候要注意下. 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5

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

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

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 POJ 3352 Road Construction(双连通)

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

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 3177 Redundant Paths (双联通)

/******************************************************* 题目:Redundant Paths (poj 2177) 链接:http://poj.org/problem?id=3177 算法:双联通+缩点 思路:先找出所有双联通分量,把这些分量缩成一个点 再找出所有度为一的点,用这些点数加一除2就可以了 ********************************************************/ #include<cs

[双连通分量] POJ 3177 Redundant Paths

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 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

POJ 3177 Redundant Paths (双连通)

题目地址:POJ 3177 找出各个双连通分量度数为1的点,然后作为叶子节点,那么ans=(叶子结点数+1)/2.需要注意的是有重边. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include