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
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值