HDU 3836 Equivalent Sets 强连通分量

Equivalent Sets

Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others)
Total Submission(s): 2587    Accepted Submission(s): 877


Problem 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
2011 Multi-University Training Contest 1 - Host by HNU

题目链接:HDU 3836 Equivalent Sets
题目大意:给你n个点,m条有向边,问至少还需添加多少条边边使得整幅图强连通。
题目分析:求一次强连通,对所有分量缩点,设入度等于0的点的个数为in,出度等于0的点的个数为ou,则结果即为max(in,ou)。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define min(A, B) ((A) < (B) ? (A) : (B))
#define max(A, B) ((A) > (B) ? (A) : (B))
#define clear(A, X) memset(A, X, sizeof A)
using namespace std;
const int maxN = 20005;
const int maxE = 1000000;
struct Edge{
    int u, v, n;
};
Edge edge[maxE];
int adj[maxN], cntE;
int belong[maxN], cntT;
int DFN[maxN], LOW[maxN];
int Stack[maxE], top, dfs_clock, INS[maxN];
int in[maxN], ou[maxN];
int n, m;
void addedge(int u, int v){
    edge[cntE].u = u; edge[cntE].v = v; edge[cntE].n = adj[u]; adj[u] = cntE++;
}
void Tarjan(int u){
    DFN[u] = LOW[u] = ++dfs_clock;
    Stack[top++] = u;
    INS[u] = 1;
    for(int i = adj[u]; ~i; i = edge[i].n){
        int v = edge[i].v;
        if(!DFN[v]){
            Tarjan(v);
            LOW[u] = min(LOW[u], LOW[v]);
        }
        else if(INS[v]){
            LOW[u] = min(LOW[u], DFN[v]);
        }
    }
    if(DFN[u] == LOW[u]){
        cntT++;
        while(1){
            int v = Stack[--top];
            belong[v] = cntT;
            INS[v] = 0;
            if(v == u) break;
        }
    }
}
void work(){
    int u, v;
    clear(DFN, 0);
    clear(adj, -1);
    clear(INS, 0);
    clear(in, 0);
    clear(ou, 0);
    cntT = cntE = top = dfs_clock = 0;
    for(int i = 0; i < m; ++i){
        scanf("%d%d", &u, &v);
        addedge(u, v);
    }
    for(int i = 1; i <= n; ++i) if(!DFN[i]) Tarjan(i);
    if(cntT <= 1) printf("0\n");
    else{
        for(int i = 0; i < cntE; ++i){
            int u = edge[i].u, v = edge[i].v;
            if(belong[u] != belong[v]){
                ou[belong[u]]++;
                in[belong[v]]++;
            }
        }
        int in_cnt = 0, ou_cnt = 0;
        for(int i = 1; i <= cntT; ++i){
            if(!in[i]) in_cnt++;
            if(!ou[i]) ou_cnt++;
        }
        printf("%d\n", max(in_cnt, ou_cnt));
    }
}
int main(){
    while(~scanf("%d%d", &n, &m)) work();
    return 0;
}


你可能感兴趣的:(HDU)