URAL 2038 Minimum Vertex Cover(二分匹配)

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 set N (“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

给出一个二部图和一些连接的边  问有那些点是二部图的关键点


问题就是在于如何判断点是否一直在二部图的最小点覆盖之中 

一开始的思路是 跑一遍匈牙利 然后枚举最小覆盖中的点 把这个点删除 看二分匹配的值有没有变化。。

这种方法 一个是复杂度可能有问题 还有就是处理过程没那么简单。。。

后来问了金猛。。他画了图 发现 

先做一次最大匹配 然后对于已经匹配的点 如果他存在与之相连 但是不是被匹配的点 那么他总是在最小覆盖集里面

详细的看代码。。。感谢金猛

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' | ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}
int n,m,k;
vector<int> vec[MAXN];
int mp[MAXN][MAXN];
char ch[]="NEA";
int linkx[MAXN],linky[MAXN],vis[MAXN],ansx[MAXN],ansy[MAXN];
int dfs(int u)
{
    int sz=vec[u].size();
    for(int i=0;i<sz;i++)
    {
        int v=vec[u][i];
        if(!vis[v])
        {
            vis[v]=1;
            if(linky[v]==-1||dfs(linky[v]))
            {
                linkx[u]=v;
                linky[v]=u;
                return 1;
            }
        }
    }
    return 0;
}

void hungary()
{
    MEM(linkx,-1); MEM(linky,-1);
    for(int i=1;i<=n;i++)
    {
        MEM(vis,0);
        dfs(i);
    }
}

int main()
{
//    fread;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        for(int i=1;i<=n;i++) vec[i].clear();
        MEM(mp,0);
        while(k--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            mp[u][v]=1;
            vec[u].push_back(v);
        }
        hungary();
        queue<int> que;
        for(int i=1;i<=n;i++)
        {
            if(linkx[i]==-1)
                ansx[i]=0,que.push(i);
            else ansx[i]=1;
        }
        for(int i=1;i<=m;i++)
        {
            if(linky[i]==-1)
                ansy[i]=0,que.push(i+n);
            else ansy[i]=1;
        }
        while(!que.empty())
        {
            int i=que.front(); que.pop();
            if(i<=n)
            {
                int u=i;
                for(int v=1;v<=m;v++)
                {
                    if(mp[u][v])
                    {
                        ansy[v]=2;
                        int tmp=linky[v];
                        if(ansx[tmp]) que.push(tmp);
                        ansx[tmp]=0;
                    }
                }
            }
            else
            {
                int v=i-n;
                for(int u=1;u<=n;u++)
                {
                    if(mp[u][v])
                    {
                        ansx[u]=2;
                        int tmp=linkx[u];
                        if(ansy[tmp]!=0) que.push(tmp+n);
                        ansy[tmp]=0;
                    }
                }
            }
        }
        for(int i=1;i<=n;++i)
            putchar(ch[ansx[i]]);
        puts("");
        for(int i=1;i<=m;++i)
            putchar(ch[ansy[i]]);
        puts("");
    }
    return 0;
}




没有更多推荐了,返回首页