我是靠谱客的博主 搞怪冬天,这篇文章主要介绍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.

样例输入

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

样例输出

复制代码
1
2
3
2 1 3

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

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


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#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("%dn", 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; }

最后

以上就是搞怪冬天最近收集整理的关于The Maximum Unreachable Node Set的全部内容,更多相关The内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(126)

评论列表共有 0 条评论

立即
投稿
返回
顶部