Light OJ 1429 - Assassin`s Creed (II)

POJ 2594 类似,只不过这里给的是一个有环的图!还有每个人可以走重复的点,任意访问多次都行,只要可达即可。

解法也类似,只不过这里先要缩点,因为同一个强连通分量内的点都相互可达嘛,所以直接缩点就好了!

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 1010;


int vis[maxn];
int y[maxn];
vector <int> G[maxn], G2[maxn], G3[maxn];
int n, m;
int a[maxn][maxn];

int pre[maxn];
int low[maxn];
int sccno[maxn];
int dfs_clock;
int scc_cnt;
stack <int> S;

void dfs(int u)
{
    pre[u] = low[u] = ++dfs_clock;
    S.push(u);
    for(int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        if(!pre[v])
        {
            dfs(v);
            low[u] = min(low[u], low[v]);
        }
        else if(!sccno[v])
            low[u] = min(low[u], pre[v]);
    }
    if(pre[u] == low[u])
    {
        scc_cnt++;
        while(1)
        {
            int x = S.top();
            S.pop();
            sccno[x] = scc_cnt;
            if(x == u)
                break;
        }
    }
}
void find_scc()
{
    dfs_clock = scc_cnt = 0;
    memset(sccno, 0, sizeof(sccno));
    memset(pre, 0, sizeof(pre));
    for(int i = 1; i <= n; i++)
        if(!pre[i])
            dfs(i);
}
void DFS(int now,int u)
{
    vis[u] = 1;
    for(int i = 0;i < G2[u].size();i ++){
        int v = G2[u][i];
        if(!vis[v]) {
            G[now].push_back(v);
            DFS(now, v);
        }
    }
}
bool dfs2(int u)
{
    for(int i = 0; i < G3[u].size(); i++)
    {
        int v = G3[u][i];
        if(vis[v])
            continue;
        vis[v] = true;
        if(y[v] == -1 || dfs2(y[v]))
        {
            y[v] = u;
            return true;
        }
    }
    return false;
}
int hungary()
{
    int ans = 0;
    memset(y, -1, sizeof(y));
    for(int i = 1; i <= scc_cnt; i++)
    {
        memset(vis, 0, sizeof(vis));
        if(dfs2(i)) ans++;
    }
    return ans;
}

int main()
{
    int T, ca = 0;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &n, &m);
        for(int i = 0; i <= n; i++)
            G[i].clear(), G2[i].clear(), G3[i].clear();
        while(m--)
        {
            int u, v;
            scanf("%d %d", &u, &v);
            G2[u].push_back(v);
        }
        for(int i = 1; i <= n; i++){
            memset(vis, 0, sizeof(vis));
            DFS(i,i);
        }

        find_scc();
        for(int u = 1; u <= n; u++)
        {
            for(int i = 0; i < G[u].size(); i++)
            {
                int v = G[u][i];
                if(sccno[u] != sccno[v])
                    G3[sccno[u]].push_back(sccno[v]);
            }
        }
        printf("Case %d: %d\n", ++ca, scc_cnt - hungary());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值