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 ll long long
using namespace std;
const int N = 1e5+10;
int n,m;
struct Edge{
    int to,nxt;
}edge[N<<2];
int tot,head[N];
int dfn[N],low[N],fa[N],d[N];
stack<int> st;
int ind,ans;
void init(){
    tot = 0;
    memset(head,-1,sizeof head);
    memset(dfn,0,sizeof dfn);
    memset(low,0,sizeof low);
}
void add(int u,int v){
    edge[tot].to = v;
    edge[tot].nxt = head[u];
    head[u] = tot++;
}
void tarjan(int u,int root){
    int v,sg=0;
    dfn[u] = low[u] = ++ind;
    st.push(u);
    for(int i =head[u];i!=-1;i=edge[i].nxt){
        v = edge[i].to;
        if(v==root && !sg) {
            sg = 1;
            continue;
        }
        if(!dfn[v]){
            tarjan(v,u);
            low[u] = min(low[u],low[v]);
        }
        else if(dfn[v] < dfn[u]){
            low[u] = min(low[u],dfn[v]);
        }
    }
    if(low[u]== dfn[u]){
        int v = st.top();
        while(v !=u){
            fa[v] = u;
            st.pop();
            v = st.top();
        }
        fa[u] = u;
        st.pop();
    }
}
void solve(){
    int fr,to;
    init();
    for(int i=1;i<=m;++i){
        scanf("%d%d",&fr,&to);
        add(fr,to);
        add(to,fr);
    }
    tarjan(1,0);
    for(int i=1;i<=n;++i){
        for(int j=head[i];j!=-1;j=edge[j].nxt){
            if(fa[i]!=fa[edge[j].to]){
                d[fa[i]]++;
                d[fa[edge[j].to]]++;
            }
        }
    }
    int cnt = 1;
    for(int i=1;i<=n;++i){
        if(d[i]==2){
            cnt++;
        }
    }
    printf("%d\n",(cnt)/2);
}
int main()
{
    while(scanf("%d%d",&n,&m)){
        solve();
        break;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/xxrlz/p/11368106.html

时间: 2024-11-07 17:00:01

POJ 3177 Redundant Paths (tarjan无向图求缩点)的相关文章

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 for

POJ 3177 Redundant Paths 边双连通分量+缩点

题目链接: poj3177 题意: 给出一张连通图,为了让任意两点都有两条通路(不能重边,可以重点),至少需要加多少条边 题解思路: 分析:在同一个边双连通分量中,任意两点都有至少两条独立路可达,所以同一个边双连通分量里的所有点可以看做同一个点. 缩点后,新图是一棵树,树的边就是原无向图桥. 现在问题转化为:在树中至少添加多少条边能使图变为双连通图. 结论:添加边数=(树中度为1的节点数+1)/2 具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上

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 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(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 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 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 无向图边双联通基础题

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