2038. Minimum Vertex Cover

Time limit: 1.0 second
Memory limit: 64 MB
A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at least one vertex of the set. A minimum vertex cover is a vertex cover with minimal cardinality.
Consider a set of all minimum vertex covers of a given bipartite graph. Your task is to divide all vertices of the graph into three sets. A vertex is in setN (“Never”) if there is no minimum vertex cover containing this vertex. A vertex is in set A (“Always”) if it is a part of every minimum vertex cover of the given graph. If a vertex belongs neither to N nor to A, it goes to the set E (“Exists”).

Input

The first line of input contains three integers nmk: the size of the first vertex set of the bipartite graph, the size of the second vertex set and the number of edges (1 ≤ nm ≤ 1000; 0 ≤ k ≤ 106). Next k lines contain pairs of numbers of vertices, connected by an edge. First number denotes a vertex from the first set, second — from the second set. Vertices in each set are numbered starting from one. No pair of vertices is connected by more than one edge.

Output

On the first line, print a sequence of n letters ‘N’, ‘E’, ‘A’ without spaces. The letter on position i corresponds to the set containing i-th vertex of the first set. The second line must contain the answer for the second vertex set in the same format.

Sample

input output
11 9 22
1 1
1 2
1 3
1 8
1 9
2 1
2 3
3 2
3 4
4 3
4 5
5 2
5 4
5 6
6 6
6 7
7 5
7 7
8 7
9 7
10 7
11 7
AEEEEEENNNN
EEEEEEANN
 
题解:
看了网上 http://blog.csdn.net/u011699990/article/details/45257071 的题解才会的
关键就是确定这个点是否在最小点覆盖的条件是 : 如果这个点是一个已盖点,从他相连的所有点中只要存在一个是未盖点,那么这个点一定是在最小点覆盖上的(这个结论很神奇,也很容易想通)
那么一旦确定了某个点是最小点覆盖上的点之后,他所对应的另外一个匹配点就一定不会在最小点覆盖了,如此循环,我们就能最终确定哪些点是一定在最小点覆盖上了,可以看看代码理解,代码写了注释
 
#include <bits/stdc++.h>
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define two(x) (1<<(x))
#define clr(x,y) memset((x),(y),sizeof((x)))
#define dbg(x) cout << #x << "=" << x << endl;
#define lowbit(x) ((x)&(-x))
const int mod = 1e9 + 7;
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std;
const int maxn = 1e3 + 15;
vector < int > LE[maxn] , RE[maxn] ;
int linkL[maxn],linkR[maxn],N,M,K,ansL[maxn],ansR[maxn],vis[maxn]; int DFS( int x ){
for( auto v : LE[x] ){
if( vis[v] ) continue;
vis[v] = 1;
if( linkR[v] == -1 || DFS( linkR[v] ) ){
linkL[x] = v , linkR[v] = x;
return 1;
}
}
return 0;
} int main( int argc , char * argv[] ){
N=read(),M=read(),K=read();
rep(i,1,K){
int u = read() , v = read();
LE[u].pb( v ); RE[v].pb( u );
}
clr( linkL , -1 ) ; clr( linkR , -1 );
// 进行二分图最大匹配
rep(i,1,N){
rep(j,1,M) vis[j]=0;
DFS( i );
}
// 初始认为所有匹配的点都是 E , 对于未匹配点,加入队列中
queue < int > Q;
rep(i,1,N) if(~linkL[i]) ansL[i] = 1; else Q.push( i );
rep(i,1,M) if(~linkR[i]) ansR[i] = 1; else Q.push( i + N );
// 对于一定在最小点覆盖的点满足的条件即 : 这个匹配点连接的所有边中存在非匹配点 , 即,如果存在的话,就必须选这个点
// 开始跑
// 注意队列中的点都是未匹配点
while(!Q.empty()){
int x = Q.front() ; Q.pop();
// 是左边的点
if( x <= N ){
for( auto v : LE[x] ){
// 右边的点是一个匹配点同时没有check过
if( ~linkR[v] && ansR[v] != 2 ){
ansR[v] = 2; // 这个点必选
ansL[linkR[v]] = 0; // 那么他所对应的左边的匹配点一定不选
Q.push( linkR[v] ); // 左边那个点不选了,加入到队列中
}
}
}else{
for( auto v : RE[x - N] ){
// 左边的点是一个匹配点同时没有check过
if( ~linkL[v] && ansL[v] != 2 ){
ansL[v] = 2; // 这个点必选
ansR[linkL[v]] = 0; // 那么他所对应的右边的匹配点一定不选
Q.push( linkL[v] + N ); // 左边那个点不选了,加入到队列中
}
}
}
}
rep(i,1,N) if(ansL[i] == 0) putchar('N') ; else if( ansL[i] == 1 ) putchar('E') ; else putchar('A');
puts("");
rep(i,1,M) if(ansR[i] == 0) putchar('N') ; else if( ansR[i] == 1 ) putchar('E') ; else putchar('A');
puts("");
return 0;
}

  

URAL 2038 Minimum Vertex Cover的更多相关文章

  1. 最小顶点覆盖(Minimum Vertex Cover)与最大独立集(Maximum Independent Set)

    问题描述:就是在图中找最小的点集,使得覆盖所有边. 和独立集等价:独立集问题:在图中找最大的点集,使得点集内的所有点互不相连. 引理:顶点覆盖集和独立集互补. 上面这个引理使得这两个问题可以相互规约, ...

  2. SCU - 4439 Vertex Cover (图的最小点覆盖集)

    Vertex Cover frog has a graph with \(n\) vertices \(v(1), v(2), \dots, v(n)\) and \(m\) edges \((v(a ...

  3. 四川第七届 D Vertex Cover(二分图最小点覆盖,二分匹配模板)

    Vertex Cover frog has a graph with nn vertices v(1),v(2),…,v(n)v(1),v(2),…,v(n) and mm edges (v(a1), ...

  4. SCU 4439 Vertex Cover|最小点覆盖

    传送门 Vertex Cover frog has a graph with n vertices v(1),v(2),…,v(n)v(1),v(2),…,v(n) and m edges (v(a1 ...

  5. 集合覆盖 顶点覆盖: set cover和vertex cover

    这里将讲解一下npc问题中set cover和vertex cover分别是什么. set cover: 问题定义: 实例:现在有一个集合A,其中包含了m个元素(注意,集合是无序的,并且包含的元素也是 ...

  6. PAT1134:Vertex Cover

    1134. Vertex Cover (25) 时间限制 600 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A vertex ...

  7. A1134. Vertex Cover

    A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...

  8. PAT A1134 Vertex Cover (25 分)——图遍历

    A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...

  9. PAT 甲级 1134 Vertex Cover

    https://pintia.cn/problem-sets/994805342720868352/problems/994805346428633088 A vertex cover of a gr ...

随机推荐

  1. 【MySQL】关于MySQL错误日志信息的收集

    为方便维护MySQL,写了个脚本用以提供收集错误信息的接口.这些错误信息来自与MySQL错误日志,而 通过grep mysql可以获取error-log的路径. #!/usr/bin/env pyth ...

  2. JavaScript学习笔记(高级部分—02)

    47.switch语句的语法: switch (i) { case 20: alert("20"); break; case 30: alert("30"); ...

  3. [置顶] Android访问控制系统测试与评估

    5.1实验方案 通过以上章节,本文阐述了目前Android平台上的恶意软件以“隐私窃取”和“恶意扣费”类为主,本研究课题访问控制的目标也正是阻止恶意软件“隐私窃取”和“恶意扣费”的行为,因此,本实验方 ...

  4. Qt的Graphics-View框架和OpenGL结合详解

    Qt的Graphics-View框架和OpenGL结合详解 演示程序下载地址:这里 程序源代码下载地址:这里 这是一篇纯技术文,介绍了这一个月来我抽时间研究的成果. Qt中有一个非常炫的例子:Boxe ...

  5. SQL Profile 总结(一)

    一.前提概述 在介绍SQL Profile之前,不得不说的一个工具就是SQL Tuning Advisor:这个工具是从Oracle 10g開始引入,它的任务就是分析一个指定的SQL语句,并建议怎样使 ...

  6. C#。5 函数 类

    函数:能够独立完成某个功能的模块.          好处:1.结构更清析(编写.维护方便 ).2.代码重用.3.分工开发.          四要素:名称,输入(参数),输出(返回的类型),加工(函 ...

  7. XAML 概述

    我们将向 Windows 运行时应用开发人员介绍 XAML 语言和 XAML 概念,并介绍在使用 XAML 创建 Windows 运行时应用时,在 XAML 中声明对象和设置属性的不同方式. 什么是 ...

  8. [Android分享] 如何解决Android 5.0中出现的警告:Service Intent must be explicit

    Android 5.0程序运行报Service Intent must be explicit错误,原因是5.0的service必须显式调用 改成 Intent intent = new Intent ...

  9. centos 服务器装与python34源码安装

    http://www.111cn.net/sys/CentOS/63645.htm 1.CentOS安装Python的依赖包(不安装依赖包,会导致python安装不完整) yum groupinsta ...

  10. html Table实现表头固定

    最近一直在搞前台琐碎的东西,也学习了一下linux,没有时间对新的东西进行深入的研究和学习,没有写博客,不过归咎其原因还是在于自己的惰怠. 废话不多说,今天想将一个前台页面设计的一个小东西分享一下,那 ...