HDU 3861--The King’s Problem【scc縮点構図&&二分整合最小経路カバーを求める】


The King’s Problem
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2137    Accepted Submission(s): 763
Problem Description
In the Kingdom of Silence, the king has a new problem. There are N cities in the kingdom and there are M directional roads between the cities. That means that if there is a road from u to v, you can only go from city u to city v, but can’t go from city v to city u. In order to rule his kingdom more effectively, the king want to divide his kingdom into several states, and each city must belong to exactly one state.
What’s more, for each pair of city (u, v), if there is one way to go from u to v and go from v to u, (u, v) have to belong to a same state.And the king must insure that in each state we can ether go from u to v or go from v to u between every pair of cities (u, v) without passing any city which belongs to other state.
  Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom into.
 
Input
The first line contains a single integer T, the number of test cases. And then followed T cases.
The first line for each case contains two integers n, m(0 < n <= 5000,0 <= m <= 100000), the number of cities and roads in the kingdom. The next m lines each contains two integers u and v (1 <= u, v <= n), indicating that there is a road going from city u to city v.
 
Output
The output should contain T lines. For each test case you should just output an integer which is the least number of states the king have to divide into.
 
Sample Input

   
   
   
   
1 3 2 1 2 1 3

 
Sample Output

   
   
   
   
2

 
王はnの都市を計画し、いくつかの州に分けなければならない.3つの要求があります:1、辺uからv、および辺vからuがあれば、u、vは同じ州内に区分されなければなりません.2、一つの州内の2時に少なくとも一方が他方に着くことができる.3、一つの点は一つの州に分けられ、少なくともどれだけの州を建てるかを聞く.
構想:まず互いに2つの到達可能な点を強連通で1つの州に帰し、それから縮点を行い、新しい図を確立し、それからハンガリーアルゴリズムで最大マッチングを求め、答え=強連通で求めた連通ブロック-最大マッチング(最小経路カバー=結点数-最大マッチング)を求める.
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#define maxn 50000+100
#define maxm 200000+100
using namespace std;
int n, m;

struct node {
    int u, v, next;
};

node edge[maxm];
int head[maxn], cnt;
int low[maxn], dfn[maxn];
int dfs_clock;
int Stack[maxn], top;
bool Instack[maxn];
int Belong[maxn];
int scc_clock;
vector<int>Map[maxn];

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void addedge(int u, int v){
    edge[cnt] = {u, v, head[u]};
    head[u] = cnt++;
}

void getmap(){
    scanf("%d%d", &n, &m);
    while(m--){
        int a, b;
        scanf("%d%d", &a, &b);
        addedge(a, b);
    }
}

void Tarjan(int u, int per){
    int v;
    low[u] = dfn[u] = ++dfs_clock;
    Stack[top++] = u;
    Instack[u] = true;
    for(int i = head[u]; i != -1; i = edge[i].next){
        int v = edge[i].v;
        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(dfn[u] == low[u]){
        scc_clock++;
        do{
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = scc_clock;
        }
        while( v != u);
    }
}

void find(){
    memset(low, 0, sizeof(low));
    memset(dfn, 0, sizeof(dfn));
    memset(Belong, 0, sizeof(Belong));
    memset(Stack, 0, sizeof(Stack));
    memset(Instack, false, sizeof(false));
    dfs_clock = scc_clock = top = 0;
    for(int i = 1; i <= n ; ++i){
        if(!dfn[i])
            Tarjan(i, i);
    }
}

void suodian(){
    for(int i = 1; i <= scc_clock; ++i)
        Map[i].clear();
    for(int i = 0; i < cnt; ++i){
        int u = Belong[edge[i].u];
        int v = Belong[edge[i].v];
        if(u != v){
            Map[u].push_back(v);
        }
    }
}

int used[maxn], link[maxn];

bool dfs(int x){
    for(int i = 0; i < Map[x].size(); ++i){
        int y = Map[x][i];
        if(!used[y]){
            used[y] = 1;
            if(link[y] == -1 || dfs(link[y])){
                link[y] = x;
                return true;
            }
        }
    }
    return false;
}

void hungary(){
    int ans = 0;
    memset(link, -1, sizeof(link));
    for(int j = 1; j <= scc_clock; ++j){
        memset(used, 0, sizeof(used));
        if(dfs(j))
            ans++;
    }
    printf("%d
", scc_clock - ans); } int main (){ int T; scanf("%d", &T); while(T--){ init(); getmap(); find(); suodian(); hungary(); } return 0; }