题目描述

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. jenkins+saltstack+pipeline 部署springcloud 多模块jar包

    在jenkins上安装salt-master, pipeline{ agent{       node{               label 'master'               cust ...

  2. 浅入深出Java输入输出流主线知识梳理

      Java把不同类型的输入.输出,这些输入输出有些是在屏幕上.有些是在电脑文件上, 都抽象为流(Stream) 按流的方向,分为输入流与输出流,注意这里的输出输出是相对于程序而言的,如:如对于一个J ...

  3. arp攻击 (可查看同一局域网他人手机照片)

    国家法律一定要遵守,知识要用在对的地方. 本贴只为了和大家交流学习,请勿用在其他地方,损害任何人的利益. 今天我,来说一下arp攻击的原理和教程 原理什么的还是自行百度好,因为专业的说明是严谨而又经得 ...

  4. 4. git目录探秘

    HEAD当前指向的分支信息.cconfig,当前仓库的配置信息,core,用户,远程,分支等信息.(命令操作其实就是修改当前config文件)refs---heads,其实就是分支,里面包含所有的分支 ...

  5. 雅可比行列式【2】Jacobian行列式的意义

    2.1 线性变换将面积伸缩 对于一个\(\R^2\to\R^2\)的线性变换: \[ T(x,y)= \left[ \begin{array}{c} 4x-2y\\ 2x+3y \end{array} ...

  6. c语言中%s和%d的区别

    /************************************************************************* > File Name: ptr_both. ...

  7. UML-设计模式-本地服务容错-适配器+工厂模式

    问题1:我们的ProductCatalog存储在了数据库里了,但是数据库瘫掉了,怎么办? 解决:本地(Map)---->Local(文件)---->DB 问题2:如果新加了存储Produc ...

  8. UML-重构

    1.重构是什么? 重构是重写或重新构建已有代码的结构化和规律性方法,但不会改变已有代码的外在行为,而是采用一系列少量转换的步骤,并且每一步都结合了重新执行的测试.重构并不是全部推翻原有代码结构. 2. ...

  9. ZJNU 2349 - 抽抽抽

    为4的倍数,列出所有可能情况再判断即可 处理输入的数据对4取模 可得 4444 2244 2222 1111 3333 1133 1223 1344 1124 3324 共十种情况 所以得出答案 #i ...

  10. php的date()函数判断今天是星期几

    d  月份中的第几天,有前导零的 2 位数字 01 到 31 D  星期中的第几天,文本表示,3 个字母 Mon 到 Sun j  月份中的第几天,没有前导零 1 到 31 l  ("L&q ...