hdu 3864 强连通缩点+最小路径覆盖

首先tarjan缩点

一个SCC在一个中,之后求最小路径覆盖

就等于N-SCCcnt-最大匹配

不是很明白为什么。。。缩点后一个DAG。。为什么代码没有转二分图直接求也AC?

代码是照着http://www.cnblogs.com/kane0526/archive/2013/07/21/3203992.html写的。。主要是不会写taijan

//by dezhonger
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
using namespace std;

#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;
const int mod = int(1e9) + 7;
typedef long long ll;
typedef long long LL;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

const int N = 5000;
const int maxn = 5005;
const int M = 100000;
vector g[N+5];//图
struct E
{
    int u, v;
}e[M+5];//边

int  dfn[maxn], low[maxn], stack[maxn], belong[maxn], visit[maxn], match[maxn];
bool instack[maxn];
int top, scnt, Index, n, m;

void Init_tarjan()
{
    top = scnt = Index = 0;
    for(int i = 1; i <= n; i++) dfn[i] = low[i] = instack[i] = 0;
}

void tarjan(int u)
{
    stack[++top] = u;
    dfn[u] = low[u] = ++Index;
    instack[u] = 1;
    for(int i = 0; i < g[u].size(); i++)
    {
        int v = g[u][i];
        if(!dfn[v])
        {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if(instack[v])
        {
            low[u] = min(low[u], dfn[v]);
        }

    }
    if(low[u] == dfn[u])
    {
        int vv;
        scnt++;
        do
        {
            vv = stack[top--];
            instack[vv] = 0;
            belong[vv] = scnt;
        }while(u != vv);
    }
}



bool find(int x)
{
    for(int i = 0; i < g[x].size(); i++)
    {
        int k = g[x][i];
        if(!visit[k])
        {
            visit[k] = true;
            if(match[k] == -1 || find(match[k]))
            {
                match[k] = x;
                return true;
            }
        }
    }
    return false;
}
int erfen()
{
    int cnt = 0;
    CLR(match, -1);
    for(int i = 1; i <= scnt; i++)
    {
        for(int j = 1; j <= scnt; j++) visit[j] = 0;
        cnt += find(i);
    }
    return cnt;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(0);
    int T, u, v, ans;
    cin >> T;
    while(T--)
    {
        cin >> n >> m;
        for(int i = 1; i <= n; i++) g[i].clear();
        for(int i = 1; i <= m; i++)
        {
            cin >> u >> v;
            //cin >> e[i].u >> e[i].v;
            e[i].u = u; e[i].v = v;
            g[u].pb(v);
        }

        Init_tarjan();
        for(int i = 1; i <= n; i++)
        {
            if(!dfn[i]) tarjan(i);
        }
        ans = 0;

        for(int i = 1; i <= n; i++) g[i].clear();
       // for(int i = 1; i <= n; i++) cout << belong[i] << " ";cout << endl;

        for(int i = 1; i <= m; i++)
        {
            u = belong[e[i].u]; v = belong[e[i].v];
            if(u!=v) g[u].pb(v);
        }
        ans = erfen();
        cout << scnt - ans << endl;

    }


    return 0;
}


你可能感兴趣的:(hdu 3864 强连通缩点+最小路径覆盖)