The Maximum Unreachable Node Set
时间限制: 20 Sec 内存限制: 128 MB提交: 51 解决: 8
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
In this problem, we would like to talk about unreachable sets of a directed acyclic graph G = (V, E). In
mathematics a directed acyclic graph (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 ⊂ V containing several nodes is known as an unreachable node set of G if, for each two different nodes u and v in V UR , there is no way to start at u and follow a consistently-directed sequence of edges in E that finally archives the node v. You are asked in this problem to calculate the size of the maximum unreachable node set of a given graph G.
A node set denoted by V UR ⊂ V containing several nodes is known as an unreachable node set of G if, for each two different nodes u and v in V UR , there is no way to start at u and follow a consistently-directed sequence of edges in E that finally archives the node v. You are asked in this problem to calculate the size of the maximum unreachable node set of a given graph G.
输入
The input contains several test cases and the first line contains an integer T (1 ≤ T ≤ 500) which is the number of
test cases.
For each case, the first line contains two integers n (1 ≤ n ≤ 100) and m (0 ≤ m ≤ n(n − 1)/2) indicating the number of nodes and the number of edges in the graph G. Each of the following m lines describes a directed edge with two integers u and v (1 ≤ u, v ≤ n and u 6= v) indicating an edge from the u-th node to the v-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 500000.
For each case, the first line contains two integers n (1 ≤ n ≤ 100) and m (0 ≤ m ≤ n(n − 1)/2) indicating the number of nodes and the number of edges in the graph G. Each of the following m lines describes a directed edge with two integers u and v (1 ≤ u, v ≤ n and u 6= v) indicating an edge from the u-th node to the v-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 500000.
输出
For each test case, output an integer in a line which is the size of the maximum unreachable node set of G.
样例输入
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
给你n个点m条边的DAG,问最多选多少边,使得集合内地点都不可达。
可以用Floyd得到可达矩阵,然后二分匹配跑一下最大匹配,不就是求出了两两相连的最多的点。然后n减去这些点,就是结果。水的要命。
代码实现:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))
using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}
ll upd(ll x,ll v){x=x+v>=mod?x+v-mod:x+v;return x;}
int s[105][105],n,m,vis[105],link[105];
int dfs(int num)
{
for(int i=1;i<=n;i++)
{
if(s[num][i]&&!vis[i])
{
vis[i]=1;
if(link[i]==-1||dfs(link[i]))
{
link[i]=num;
return 1;
}
}
}
return 0;
}
int main()
{
int i,j,k,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
mset(s,0);
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
s[x][y]=1;
}
//求可达矩阵
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(s[i][k]&&s[k][j])
s[i][j]=1;
}
}
}
mset(link,-1);
int ans=0;
for(i=1;i<=n;i++)
{
mset(vis,0);
if(dfs(i))
ans++;
}
printf("%d\n",n-ans);
}
}