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). In mathematics a directed acyclic graph (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 V_UR \subset VVURV containing several nodes is known as an unreachable node set of GG if, for each two different nodes uu and vv in V_{UR}VUR, there is no way to start at uu and follow a consistently-directed sequence of edges in EE that finally archives the node vv. You are asked in this problem to calculate the size of the maximum unreachable node set of a given graph GG.

Input

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

For each case, the first line contains two integers n (1 \le n \le 100)n(1n100) and m (0 \le m \le n(n - 1)/2)m(0mn(n1)/2) indicating the number of nodes and the number of edges in the graph GG. Each of the following mm lines describes a directed edge with two integers uu and v (1 \le u, v \le nv(1u,vn and u \neq v)uv) indicating an edge from the uu-th node to the vv-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 500000500000.

Output

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

样例输入
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

题意:给你一张有向无环图,问最多可以取出多少个点,使得它们两两之间不能到达

解题思路:先floyd处理出能两两之间到达的点,然后二分图跑最大匹配,最后n-ans就是答案


#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <map>  
#include <set>
#include <stack>
#include <queue>  
#include <vector>  
#include <bitset>  
#include <functional>  

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n, m;
int x[109], y[109], mp[109][109];
int s[109], nt[10050], e[10050];
int visit[125];

bool path(int k)
{
	for (int i = s[k]; ~i; i = nt[i])
	{
		int ee = e[i];
		if (!visit[ee])
		{
			visit[ee] = 1;
			if (y[ee] == -1 || path(y[ee]))
			{
				y[ee] = k;
				x[k] = ee;
				return 1;
			}
		}
	}
	return 0;
}

void MaxMatch()
{
	int ans = 0;
	memset(x, -1, sizeof x);
	memset(y, -1, sizeof y);
	for (int i = 1; i <= n; i++)
	{
		if (x[i] == -1)
		{
			memset(visit, 0, sizeof visit);
			if (path(i)) ans++;
		}
	}
	printf("%d\n", n - ans);
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d %d", &n, &m);
		int cnt = 1, u, v;
		memset(s, -1, sizeof s);
		memset(mp, 0, sizeof mp);
		for (int i = 1; i <= m; i++)
		{
			scanf("%d %d", &u, &v);
			mp[u][v] = 1;
		}
		for (int k = 1; k <= n; k++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					if (mp[i][k] && mp[k][j]) mp[i][j] = 1;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				if (mp[i][j])
				{
					nt[cnt] = s[i], s[i] = cnt, e[cnt++] = j;
				}
		MaxMatch();
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值