我是靠谱客的博主 搞怪冬天,最近开发中收集的这篇文章主要介绍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("%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 Maximum Unreachable Node Set所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部