HDU 3836 Equivalent Sets(tarjan + 压缩强连通)

本文介绍了一种算法,用于计算证明多个集合等价所需的最小步骤数。通过构建有向图并运用Tarjan算法找到强连通分量,进而确定使所有集合等价所需的最少额外关系数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大体题意:

告诉你如果证明A等于B,要证明A是B的子集,并且B是A的子集。

现在给你n 个集合,告诉m 个集合的关系,求在加多少个关系使得这N个集合相等?

思路:

A是B的子集, 用 A到B有一条有向边来代替,那么这个问题就转换为了  在加多少边使得这个图强连通!

可以用tarjan算法求出所有的强连通分量,把每一个强连通分量压缩成一个点,这个点内部肯定是强连通,那么就是加边使得这些点强连通!

这样 每个点必须至少有一个入度 和至少有一个出度,我们可以统计出有s1个 没有入度的点,有s2个没有出度的点,假设s2>s1吧,那么我们必须加够s2个边,对于s1来说,多出来的边随便连好了!

详细见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 20000 + 10;
struct Node{
    int v,next;
}p[50007];
int cnt,head[maxn];
void addedge(int u,int v){
    p[cnt].v = v;
    p[cnt].next = head[u];
    head[u] = cnt++;
}
bool inStack[maxn];
int Stack[maxn],top,Time;
int dfn[maxn],low[maxn],Tar,botin[maxn],belong[maxn],botout[maxn];
void init(){
    top = Time = cnt = Tar = 0;
    memset(head,-1,sizeof head);
    memset(inStack,0,sizeof inStack);
    memset(dfn,-1,sizeof dfn);
    memset(botin,0,sizeof botin);
    memset(botout,0,sizeof botout);
}
void dfs(int k){
    Stack[++top] = k;
    dfn[k] = low[k] = ++Time;
    inStack[k] = 1;
    for (int i = head[k]; i != -1; i = p[i].next){
        int v = p[i].v;
        if (dfn[v] == -1){
            dfs(v);
            low[k] = min(low[k],low[v]);
        }
        else if (inStack[v]){
            low[k] = min(low[k],dfn[v]);
        }
    }
    if (dfn[k] == low[k]){
        ++Tar;
        int cur = Stack[top];
        while(cur != k){
            belong[cur] = Tar;
            inStack[cur] = 0;
            cur = Stack[--top];
        }
        belong[k] = Tar;
        inStack[k] = 0;
        --top;
    }

}
int main(){
    int T,n,m;
    while(scanf("%d %d",&n, &m) == 2){
        init();
        for (int i = 0; i < m; ++i){
            int u,v;
            scanf("%d %d",&u, &v);
            addedge(u,v);
        }
        for (int i = 1; i <= n; ++i){
            if (dfn[i] == -1)dfs(i);
        }
        for (int i = 1; i <= n; ++i){
            for (int j = head[i]; j != -1; j = p[j].next){
                int v=p[j].v;
                if (belong[v] != belong[i]){
                    botout[belong[i]]++;
                    botin[belong[v]]++;
                }
            }
        }
        if (Tar == 1){
            puts("0");
            continue;
        }
        int noout=0,noin = 0;
        for (int i = 1; i <= Tar; ++i){
            if (botin[i] == 0)++noin;
            if (botout[i] == 0)++noout;
        }
        printf("%d\n",max(noin,noout));
    }

    return 0;
}


HDU - 3836
Time Limit: 4000MS Memory Limit: 104857KB 64bit IO Format: %I64d & %I64u

 Status

Description

To prove two sets A and B are equivalent, we can first prove A is a subset of B, and then prove B is a subset of A, so finally we got that these two sets are equivalent. 
You are to prove N sets are equivalent, using the method above: in each step you can prove a set X is a subset of another set Y, and there are also some sets that are already proven to be subsets of some other sets. 
Now you want to know the minimum steps needed to get the problem proved.

Input

The input file contains multiple test cases, in each case, the first line contains two integers N <= 20000 and M <= 50000. 
Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.

Output

For each case, output a single integer: the minimum steps needed.

Sample Input

4 0
3 2
1 2
1 3

Sample Output

4
2

        
  

Hint

       
Case 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.

Source

Problem descriptions:
System Crawler 2016-10-13
Initialization.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值