码迷,mamicode.com
首页 > 其他好文 > 详细

最少加多少边成为强连通图

时间:2017-09-09 13:50:24      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:bool   sizeof   col   math   turn   print   span   lin   gcd   

#include <iostream>  
#include <cstdlib>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include <cmath>  
#include <map>  
#include <stack>  
  
using namespace std;  
  
typedef long long LL;  
typedef unsigned long long ULL;  
  
const int MAXN(100010);  
const int MAXE(100010);  
template<typename T>  
bool checkmin(T &a, const T &b){  
    return b < a? (a = b, true): false;  
}  
template<typename T>  
bool checkmax(T &a, const T &b){  
    return b > a? (a = b, true): false;  
}  
  
inline int lowb(int i){return i&-i;}  
int gcd(int a, int b){  
    while(b){  
        int t = a%b;  
        a = b;  
        b = t;  
    }  
    return a;  
}  
  
struct E{  
    int u, v;  
    E *next;  
};  
  
struct G{  
    E *h[MAXN];  
    E e[MAXE], *r;  
    void init(int n){  
        memset(h, 0, sizeof(h[0])*(n+1));  
        r = e;  
    }  
    void add(int u, int v){  
        r->u = u;  
        r->v = v;  
        r->next = h[u];  
        h[u] = r++;  
    }  
} g, g1, g2, g3;  
stack<int> st;  
int co[MAXN], rep[MAXN], to[MAXN], ind[MAXN], out[MAXN], cn;  
bool vis[MAXN];  
  
void dfs(int u){  
    vis[u] = true;  
    for(E *i = g.h[u]; i; i = i->next)  
        if(!vis[i->v])  
            dfs(i->v);  
    st.push(u);  
}  
  
void dfs1(int u){  
    co[u] = cn;  
    for(E *i = g1.h[u]; i; i = i->next)  
        if(!co[i->v])  
            dfs1(i->v);  
}  
  
void dfs2(int u, int rt){  
    vis[u] = true;  
    bool leaf(true);  
    for(E *i = g2.h[u]; i; i = i->next){  
        leaf = false;  
        if(!vis[i->v]) dfs2(i->v, rt);  
    }  
    if(leaf) to[u] = rt;  
}  
  
int main(){  
    int n, u;  
    scanf("%d", &n);  
    g.init(n);  
    g1.init(n);  
    for(int i = 1; i <= n; ++i){  
        scanf("%d", &u);  
        g.add(i, u);  
        g1.add(u, i);  
    }  
    for(int i = 1; i <= n; ++i)  
        if(!vis[i])  
            dfs(i);  
    while(!st.empty()){  
        u = st.top();  
        st.pop();  
        if(co[u]) continue;  
        co[u] = ++cn;  
        rep[cn] = u;  
        dfs1(u);  
    }  
    if(cn == 1){  
        printf("0\n");  
        return 0;  
    }  
    g2.init(cn);  
    memset(ind, 0, sizeof(ind[0])*(cn+1));  
    for(E *i = g.e; i < g.r; ++i)  
        if(co[i->u] != co[i->v]){  
            g2.add(co[i->v], co[i->u]);  
            ++out[co[i->u]];  
            ++ind[co[i->v]];  
        }  
    memset(vis, 0, sizeof(vis[0])*(cn+1));  
    for(int i = 1; i <= cn; ++i)  
        if(out[i] == 0)  
            dfs2(i, i);  
    int ans = 0, f = 0;  
    for(int i = 1; i <= cn; ++i)  
        if(ind[i] == 0){  
            if(!f) f = i;  
            ++ans;  
        }  
    printf("%d\n", ans);  
    int l = f;  
    for(int i = f+1; i <= cn; ++i)  
        if(ind[i] == 0){  
            printf("%d %d\n", rep[to[l]], rep[i]);  
            l = i;  
        }  
    printf("%d %d\n", rep[to[l]], rep[f]);  
    return 0;  
}  

 

最少加多少边成为强连通图

标签:bool   sizeof   col   math   turn   print   span   lin   gcd   

原文地址:http://www.cnblogs.com/jianrenfang/p/7497921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!