HDU - 3836 Equivalent Sets ( 加边使其为强连通图

题目描述

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.

输入

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.

输出

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.

题意

一个图 加最少的边怎么成为一个强连通图
求出各个连通分量 加边即可

AC代码

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;

#define ls              st<<1
#define rs              st<<1|1
#define fst             first
#define snd             second
#define MP              make_pair
#define PB              push_back
#define LL              long long
#define PII             pair<int,int>
#define VI              vector<int>
#define CLR(a,b)        memset(a, (b), sizeof(a))
#define ALL(x)          x.begin(),x.end()
#define rep(i,s,e) for(int i=(s); i<=(e); i++)
#define tep(i,s,e) for(int i=(s); i>=(e); i--)

const int INF = 0x3f3f3f3f;
const int MAXN = 2e5+10;
const int mod = 1e9+7;
const double eps = 1e-8;

void fe() {
  #ifndef ONLINE_JUDGE
      freopen("in.txt", "r", stdin);
      freopen("out.txt","w",stdout);
  #endif
}
LL read()
{
   LL 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<<1)+(x<<3)+ch-'0',ch=getchar();
   return x*f;
}

stack<int> S;
vector<int> E[MAXN];
int n, m;
int dfn[MAXN], low[MAXN], bl[MAXN];
int cnt, tot;
bool vis[MAXN];
int in[MAXN], out[MAXN];

void tarjan(int u) {
    low[u] = dfn[u] = ++tot;
    S.push(u);
    vis[u] = true;
    for(int i = 0; i < E[u].size(); i++) {
        int v = E[u][i];
        if(!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        } else if(vis[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        cnt++;
        while(true) {
            int v = S.top();
            S.pop();
            vis[v] = false;
            bl[v] = cnt;
            if(v == u) 
                break;
        }
    }
}
void get_dag() {
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < E[i].size(); j++) {
            int u = bl[i];
            int v = bl[E[i][j]];
            if(u != v) {
                out[u]++;  
                in[v]++;
            }
        }
    }
}
int main(int argc, char const *argv[])
{
    while(cin >> n >> m) { 
        CLR(vis, false); CLR(low, 0); CLR(dfn, 0);
        CLR(bl, 0); CLR(in, 0); CLR(out, 0);
        for(int i = 1; i <= n; i++) E[i].clear(); 
        while(!S.empty()) S.pop();
        cnt = 0, tot = 0;
        for(int i = 0; i < m; i++) {
            int a, b;
            cin >> a >> b;
            E[a].push_back(b);
        }
        for(int i = 1; i <= n; i++) {
            if(!dfn[i]) 
                tarjan(i);
        }
        if(cnt <= 1) {
            cout << "0\n";
            continue;
        }
        get_dag();
        int x = 0, y = 0;
        for(int i = 1; i <= cnt; i++) {
            if(!in[i]) x++;
            if(!out[i]) y++;
        }
        cout << max(x, y) << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wang2332/article/details/80614892