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

首先tarjan缩点

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

就等于N-SCCcnt-最大匹配

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

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

//by dezhonger
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <deque>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
//#include <stack>
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<<"="<<x<<endl;


const double euler_r = 0.57721566490153286060651209;
const double pi = 3.141592653589793238462643383279;
const double E = 2.7182818284590452353602874713526;
const int inf=~0U>>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<int> 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值