题目描述

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.

输入

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 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 题解:先求出来传递闭包,并拆点,用二分图求原图的最小路径覆盖。
#include <bits/stdc++.h>
using namespace std;
const int N=;
bool graph[N][N],visited[N];
int n,match[N];
bool djk(int u)
{
if(visited[u]) return false;
visited[u]=true;
for(int v=;v<n;v++){
if(graph[u][v]&&(match[v]==-||djk(match[v]))){
match[v]=u;
return true;
}
}
return false;
}
int main(){
ios::sync_with_stdio(false);
int T;
cin>>T;
while(T--){
int m;
cin>>n>>m;
memset(graph,,sizeof(graph));
for(int i=;i<m;i++){
int a,b;
cin>>a>>b;
graph[a-][b-]=true;
}
for(int k=;k<n;k++){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
graph[i][j] |=graph[i][k]&&graph[k][j];
}
}
}
int result=n;
memset(match,-,sizeof(match));
memset(visited,,sizeof(visited));
for(int i=;i<n;i++){
if(djk(i)){
result--;
memset(visited,,sizeof(visited));
}
}
cout<<result<<endl;
}
}

二分图:

二分图的性质

二分图中,点覆盖数是匹配数。
    (1) 二分图的最大匹配数等于最小覆盖数,即求最少的点使得每条边都至少和其中的一个点相关联,很显然直接取最大匹配的一段节点即可。
    (2) 二分图的独立数等于顶点数减去最大匹配数,很显然的把最大匹配两端的点都从顶点集中去掉这个时候剩余的点是独立集,这是|V|-2*|M|,同时必然可以从每条匹配边的两端取一个点加入独立集并且保持其独立集性质。
    (3) DAG的最小路径覆盖,将每个点拆点后作最大匹配,结果为n-m,求具体路径的时候顺着匹配边走就可以,匹配边i→j',j→k',k→l'....构成一条有向路径。

(4)最大匹配数=左边匹配点+右边未匹配点。因为在最大匹配集中的任意一条边,如果他的左边没标记,右边被标记了,那么我们就可找到一条新的增广路,所以每一条边都至少被一个点覆盖。

(5)最小边覆盖=图中点的个数-最大匹配数=最大独立集。

二分图的判定

二分图是这样一个图: 有两顶点集且图中每条边的的两个顶点分别位于两个顶点集中,每个顶点集中没有边直接相连接!

 无向图G为二分图的充分必要条件是,G至少有两个顶点,且其所有回路的长度均为偶数。

 判断二分图的常见方法是染色法: 开始对任意一未染色的顶点染色,之后判断其相邻的顶点中,若未染色则将其染上和相邻顶点不同的颜色, 若已经染色且颜色和相邻顶点的颜色相同则说明不是二分图,若颜色不同则继续判断,bfs和dfs可以搞定!

易知:任何无回路的的图均是二分图。

参考:http://dsqiu.iteye.com/blog/1689505

模板:

有向无环图(DAG)的最小路径覆盖


The Maximum Unreachable Node Set的更多相关文章

  1. 2017ICPC南宁 M题 The Maximum Unreachable Node Set【二分图】

    题意: 找出不能相互访问的点集的集合的元素数量. 思路: 偏序集最长反链裸题. 代码: #include<iostream> #include<cstring> using n ...

  2. The Maximum Unreachable Node Set 【17南宁区域赛】 【二分匹配】

    题目链接 https://nanti.jisuanke.com/t/19979 题意 给出n个点 m 条边 求选出最大的点数使得这个点集之间 任意两点不可达 题目中给的边是有向边 思路 这道题 实际上 ...

  3. ACM-ICPC 2017 南宁赛区现场赛 M. The Maximum Unreachable Node Set(二分图)

    题目链接:https://nanti.jisuanke.com/t/19979 题意:给出一个 n 个点,m 条边的 DAG,选出最大的子集使得其中结点两两不能到达. 题解:参考自:https://b ...

  4. 2017ICPC南宁M The Maximum Unreachable Node Set (偏序集最长反链)

    题意:给你一张DAG,让你选取最多的点,使得这些点之间互相不可达. 思路:此问题和最小路径可重复点覆盖等价,先在原图上跑一边传递闭包,然后把每个点拆成两个点i, i + n, 原图中的边(a, b)变 ...

  5. f2fs源码解析(五) node管理结构梳理

    node是f2fs重要的管理结构, 它非常重要! 系统挂载完毕后, 会有一个f2fs_nm_info结构的node管理器来管理node的分配. f2fs_nm_info中最让人疑惑的是几颗基数树: s ...

  6. oracle 11g crs检测结果

    +ASM1@testdb11a /oracle/media/grid$ ./runcluvfy.sh stage -pre crsinst -n testdb11a,testdb11b -verbos ...

  7. runcluvfy.sh运行结果

    $ ./runcluvfy.sh stage -pre crsinst -n rac11g1,rac11g2 -verbose Performing pre-checks for cluster se ...

  8. oracle11g RAC添加节点

    OS: [root@rac ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 DB: SQL> SELECT * FROM ...

  9. Oracle11.2.0.4 RAC安装文档

    1 环境配置 参考官方文档<Grid Infrastructure Installation Guide for Linux> 1.1 软件环境 操作系统: [root@howe1 ~]# ...

随机推荐

  1. 实战Arch Unit

    在以前的文章中介绍了通过 [<实战PMD>](https://zhuanlan.zhihu.com/p/105585075).[<实战Checkstyle>](https:// ...

  2. Idea Spring工程不识别注解

    如图所示 Idea工具报出很多注解不识别,开始怀疑是 工具问题,重装Idea.配置lombak都不行,切换分支发现正常,一定是合入代码修改啥了,一行行比对,果然是这行 import org.sprin ...

  3. JDBC(一)

    1.   JDBC介绍 JDBC(Java DataBase Connectivity),即Java数据库的连接.JDBC是一种用于执行SQL语句(DML,DDL,DQL)的Java API,可以为多 ...

  4. JAVA内存分配-通俗讲解

    Java的内存分配上,主要分4个块: 一块是用来装代码的,就是编译的东西. 一块是用来装静态变量的,例如用static关键字的变量,例如字符串常量. 一块是stack,也就是栈,是用来装变量和引用类型 ...

  5. 吴裕雄--天生自然 JAVA开发学习:String 类

    public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'r', 'u', 'n' ...

  6. Deep learning with Python

    一.导论 1.1 人工智能.机器学习.深度学习 人工智能.机器学习 人工智能:1980年代达到高峰的是专家系统,符号AI是之前的,但不能解决模糊.复杂的问题. 机器学习是把数据.答案做输入,规则作输出 ...

  7. [mark]C# 异常处理

    https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/programming-guide/exceptions/index

  8. oracle学习笔记(六)——函数&存储过程的异同

    我看的书上除了能看出来函数有返回值,存储过程没有,其他啥也看不出来... 网上大大的总结

  9. js变量的相关要点

    如果变量在函数内没有声明(没有使用 var 关键字),该变量为全局变量. JavaScript 变量生命周期在它声明时初始化. 局部变量在函数执行完毕后销毁. 全局变量在页面关闭后销毁.

  10. ! [remote rejected] master -> master (pre-receive hook declined)

    前天准备上传一个project到GitLab上,但是试了很多次都上传不上去,报错如下: ! [remote rejected] master -> master (pre-receive hoo ...