
题目描述
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.
输入
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.
输出
样例输入
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
模板: