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

1429 - Assassin`s Creed (II)
PDF (English) Statistics Forum
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;
}





你可能感兴趣的:(lightoj 1429 - Assassin`s Creed (II) 【BFS预处理传递闭包 + SCC缩点 + DAG最小路径覆盖】)