lightoj 1429 - Assassin`s Creed (II) 【BFS预处理传递闭包 + SCC缩点 + DAG最小路径覆盖】

Ezio需要在有限时间内杀死位于不同城市的N个目标,城市间由单向道路连接。他请求刺客公会帮助,派遣刺客从任意城市出发,可能多次访问已由其他刺客去过的城市。问题是要找出完成所有杀戮所需的最少刺客数量。输入包含T(≤ 70)个测试案例,每个案例有N(1 ≤ N ≤ 1000)个城市和M(0 ≤ M ≤ 10000)条单向道路。输出为每个案例的编号和所需最小刺客数。注意数据集较大,需使用快速I/O方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1429 - Assassin`s Creed (II)
Time Limit: 4 second(s)Memory Limit: 32 MB

Ezio needs to kill N targets located in N different cities. The cities are connected by some one way roads. As time is short, Ezio can send a massage along with the map to the assassin's bureau to send some assassins who will start visiting cities and killing the targets. An assassin can start from any city, he may visit any city multiple times even the cities that are already visited by other assassins. Now Ezio wants to find the minimum number of assassins needed to kill all the targets.

Input

Input starts with an integer T (≤ 70), denoting the number of test cases.

Each case starts with a blank line. Next line contains two integers N (1 ≤ N ≤ 1000) and M (0 ≤ M ≤ 10000), where N denotes the number of cities and M denotes the number of one way roads. Each of the next M lines contains two integers u v (1 ≤ u, v ≤ N, u ≠ v) meaning that there is a road from u to v. Assume that there can be at most one road from a city u to v.

Output

For each case, print the case number and the minimum number of assassins needed to kill all targets.

Sample Input

Output for Sample Input

3

 

5 4

1 2

1 3

4 1

5 1

 

7 0

 

8 8

1 2

2 3

3 4

4 1

1 6

6 7

7 8

8 6

Case 1: 2

Case 2: 7

Case 3: 2

Note

Dataset is huge, use faster I/O methods.



题意:给你一个n个点m条有向边的图。要求选择最少的点作为起点,沿单向路径走完所有的点,每个点可以重复走多次。


思路:预处理传递闭包,然后去掉图中可能存在的环——SCC缩点,我们可以得到一个DAG图。下面就是求解DAG图上的最小路径覆盖 = 点数(缩点后的图) - 最大匹配数。

注意:不能用Floyd预处理传递闭包,会超时,可以选择用BFS查找。

跑了3288ms,太渣了。
用了两个vector存储和一个链式前向星存储图,如果全用链式前向星存储的话估计会快点。

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <algorithm>
#define MAXN 1000+10
#define MAXM 1000000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{//存储的是原图的传递闭包
    int from, to, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int sccno[MAXN], scc_cnt;
int low[MAXN], dfn[MAXN];
int dfs_clock;
int n, m;
void init(){
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v)
{
    Edge E = {u, v, head[u]};
    edge[edgenum] = E;
    head[u] = edgenum++;
}
stack<int> S;
bool Instack[MAXN];
void tarjan(int u, int fa)
{
    int v;
    low[u] = dfn[u] = ++dfs_clock;
    S.push(u); Instack[u] = true;
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].to;
        if(!dfn[v])
        {
            tarjan(v, u);
            low[u] = min(low[u], low[v]);
        }
        else if(Instack[v])
            low[u] = min(low[u], dfn[v]);
    }
    if(low[u] == dfn[u])
    {
        scc_cnt++;
        for(;;)
        {
            v = S.top(); S.pop();
            Instack[v] = false;
            sccno[v] = scc_cnt;
            if(u == v) break;
        }
    }
}
void find_cut(int l, int r)
{
    memset(low, 0, sizeof(low));
    memset(dfn, 0, sizeof(dfn));
    memset(sccno, 0, sizeof(sccno));
    memset(Instack, false, sizeof(Instack));
    dfs_clock = scc_cnt = 0;
    for(int i = l; i <= r; i++)
        if(!dfn[i]) tarjan(i, -1);
}
bool vis[MAXN];
vector<int> T[MAXN];//原图
void BFS(int u)
{
    queue<int> Q;
    memset(vis, false, sizeof(vis));
    vis[u] = true;
    for(int i = 0; i < T[u].size(); i++)
    {
        int v = T[u][i];
        if(vis[v]) continue;
        Q.push(v);
        vis[v] = true;
        addEdge(u, v);
    }
    while(!Q.empty())
    {
        int now = Q.front();
        Q.pop();
        for(int i = 0; i < T[now].size(); i++)
        {
            int v = T[now][i];
            if(vis[v]) continue;
            Q.push(v);
            addEdge(u, v);
            vis[v] = true;
        }
    }
}
void getMap()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        T[i].clear();
    for(int i = 0; i < m; i++)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        T[a].push_back(b);
    }
    init();
    for(int i = 1; i <= n; i++)
        BFS(i);
}

vector<int> G[MAXN];//缩点后的图
void suodian()
{
    for(int i = 1; i <= scc_cnt; i++) G[i].clear();
    for(int i = 0; i < edgenum; i++)
    {
        int u = sccno[edge[i].from];
        int v = sccno[edge[i].to];
        if(u != v)
            G[u].push_back(v);
    }
}
bool used[MAXN];
int match[MAXN];
int DFS(int u)
{
    for(int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        if(!used[v])
        {
            used[v] = true;
            if(match[v] == -1 || DFS(match[v]))
            {
                match[v] = u;
                return 1;
            }
        }
    }
    return 0;
}
int k = 1;
void solve()
{
    find_cut(1, n);
    suodian();
    int ans = 0;
    memset(match, -1, sizeof(match));
    for(int i = 1; i <= scc_cnt; i++)
    {
        memset(used, false, sizeof(used));
        ans += DFS(i);
    }
    printf("Case %d: %d\n", k++, scc_cnt - ans);
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        getMap();
        solve();
    }
    return 0;
}





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值