1. 程式人生 > >2017 icpc南寧 The Maximum Unreachable Node Set(有向圖最長反鏈)

2017 icpc南寧 The Maximum Unreachable Node Set(有向圖最長反鏈)

In this problem, we would like to talk about unreachable sets of a directed acyclic graph G=(V,E)G = (V,E)G=(V,E). In mathematics a directed acyclic graph (DAG)(DAG)(DAG) is a directed graph with no directed cycles. That is a graph such that there is no way to start at any node and follow a consistently-directed sequence of edges in E that eventually loops back to the beginning again.

A node set denoted by VUR⊂VV_UR \subset VVU​R⊂V containing several nodes is known as an unreachable node set of GGG if, for each two different nodes uuu and vvv in VURV_{UR}VUR​, there is no way to start at uuu and follow a consistently-directed sequence of edges in EEE that finally archives the node vvv. You are asked in this problem to calculate the size of the maximum unreachable node set of a given graph GGG.

Input

The input contains several test cases and the first line contains an integer T(1≤T≤500)T (1 \le T \le 500)T(1≤T≤500) which is the number of test cases.

For each case, the first line contains two integers n(1≤n≤100)n (1 \le n \le 100)n(1≤n≤100) and m(0≤m≤n(n−1)/2)m (0 \le m \le n(n - 1)/2)m(0≤m≤n(n−1)/2) indicating the number of nodes and the number of edges in the graph GGG. Each of the following mmm lines describes a directed edge with two integers uuu and v(1≤u,v≤nv (1 \le u, v \le nv(1≤u,v≤n and u≠v)u \neq v)u≠v) indicating an edge from the uuu-th node to the vvv-th node. All edges provided in this case are distinct.

We guarantee that all directed graphs given in input are DAGs and the sum of m in input is smaller than 500000500000500000.

Output

For each test case, output an integer in a line which is the size of the maximum unreachable node set of GGG.

樣例輸入

3
4 4
1 2
1 3  
2 4
3 4
4 3
1 2
2 3
3 4
6 5
1 2
4 2
6 2
2 3
2 5

樣例輸出

2
1
3

就是找出有向無環圖的最長反鏈

在有向無環圖中:

鏈是一個點的集合,這個集合中任意兩個元素v、u,要麼v能走到u,要麼u能走到v。

反鏈是一個點的集合,這個集合中任意兩點誰也不能走到誰。

Dilworth定理:最小鏈覆蓋數 = 最長反鏈長度。

最小鏈覆蓋。就是用最少的鏈,經過所有的點至少一次。先求一下傳遞閉包,然後就變成了求有向圖的最小路徑覆蓋(因為最小鏈覆蓋允許不同的鏈相交,而最小路徑覆蓋不允許相交),最後跑二分圖就行了

#include <bits/stdc++.h>
using namespace std;
const int MAXN=110;
int G[MAXN][MAXN],vis[MAXN],match[MAXN],n;
bool Find(int u)
{
    for(int i = 1; i <= n; i++){
        if(G[u][i] && !vis[i]){
            vis[i] = 1;
            if(match[i] == -1 || Find(match[i])){
                match[i] = u;
                return true;
            }
        }
    }
    return false;
}
int max_match()
{
    int ans = 0;
    memset(match,-1,sizeof(match));
    for(int i = 1; i <= n; i++){
        memset(vis,0,sizeof(vis));
        if(Find(i)){
            ans++;
        }
    }
    return ans;
}
void folyd()
{
    for(int k = 1;k <= n;k++)
        for(int i = 1;i <= n;i++)
            if(G[i][k])
               for(int j = 1;j <= n;j++)
                   if(G[k][j]) G[i][j]=1;
}
int main()
{
    int T;
    int m,u,v,ans;
    scanf("%d",&T);
    while(T--) {
       ans = 0;
       scanf("%d%d",&n,&m);
       memset(G,0,sizeof(G));
       for(int i = 1; i <= m;i++){
            scanf("%d%d",&u,&v);
            G[u][v] = 1;
       }
       folyd();
       printf("%d\n",n - max_match());
    }
    return 0;
}
/*
3
4 4
1 2
1 3
2 4
3 4
4 3
1 2
2 3
3 4
6 5
1 2
4 2
6 2
2 3
2 5
*/