HDU2767 强连通 Tarjan缩点

hdu2767

题 意 : 求 最 少 要 添 加 几 条 边 使 得 已 知 的 图 变 成 强 连 通 图 题意:求最少要添加几条边使得已知的图变成强连通图 使

题 解 : T a r j a n , 缩 点 , 那 么 只 需 要 在 缩 成 的 点 之 间 建 边 就 行 了 。 题解:Tarjan,缩点,那么只需要在缩成的点之间建边就行了。 Tarjan,
缩 完 点 后 若 某 个 强 连 通 分 量 的 出 度 或 者 入 度 为 0 , 那 么 就 必 须 要 加 边 , 最 后 的 答 案 就 是 m a x ( 出 度 为 0 的 个 数 , 入 度 为 0 的 个 数 ) 缩完点后若某个强连通分量的出度或者入度为0,那么就必须要加边,最后的答案就是max(出度为0的个数,入度为0的个数) 0max(00)
注意特判原图就是强连通图的情况


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector> 
#include<stack> 
using namespace std;
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x)  x&-x;  
#define debugint(name,x) printf("%s: %d\n",name,x);
#define debugstring(name,x) printf("%s: %s\n",name,x);
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 2e4+5;
const int mod = 1e9+7;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
vector<int>v[maxn];
stack<int>st;
int n,m,low[maxn],dfn[maxn],vis[maxn],flag,tot,cnt,id[maxn];
int in[maxn],out[maxn];
void Tarjan(int x){
    low[x] = dfn[x] = ++tot;
    st.push(x);
    vis[x] = 1;
    for(int i = 0; i < v[x].size(); i++){
        int u = v[x][i];
        if(!dfn[u]){
            Tarjan(u);
            low[x] = min(low[x],low[u]);
        }
        else if(vis[u]){
            low[x] = min(low[x],dfn[u]);
        }
    }

    if(low[x] == dfn[x]){
        cnt++;
        while(1){
            int now = st.top();
            st.pop();
            id[now] = cnt;
            vis[now] = 0;
            if(now == x) break;
        }
    }
}
void init(){
    cnt = tot = 0;
    mem(vis,0);
    mem(low,0);
    mem(dfn,0);
    mem(in,0);
    mem(out,0);
    mem(id,0);
    while(!st.empty()) st.pop();
    for(int i = 1; i <= n; i++)
        v[i].clear();
}
int main() {
    int t;
    cin>>t;
    while(t--){
        scanf("%d%d",&n,&m);
        init();
        for(int i = 1; i <= m; i++){
            int a,b;
            v[a=read()].push_back(b=read());
        }
        for(int i = 1; i <= n;i++)
            if(!dfn[i])
                Tarjan(i);
        if(cnt == 1){
            puts("0");
            continue;
        }
        int ans = 1e8;
        for(int i = 1; i <= n; i++){
            for(int j = 0; j < v[i].size(); j++){
                int u = v[i][j];
                if(id[i] != id[u])
                    in[id[i]] = out[id[u]] = 1;
            }
        }
        int x1 = 0,x2 = 0;
        for(int i = 1; i <= cnt; i++){
            if(!in[i]) x1++;
            if(!out[i]) x2++;
        }
        cout<<max(x1,x2)<<endl;
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值