POJ 3041 Asteroids (maximum bipartite graph matching)

### topic Link ###

Subject to the effect:

And give you N K, there are K asteroid on a N * N dpi. There is a cut can be cut sideways or vertically weapons, and asked how many times the minimal cut, all the planets will be destroyed.

 

analysis:

1 ~ n the number of rows added left set, the number n 1 to join the right set of columns. All points are then given as undirected edges, is connected to the bipartite graph.

1, for each side, as long as one of the endpoints is selected, the article side planetary represented may be destroyed. Similarly, if you select this endpoint, all connections to this endpoint once all the planets will be destroyed.

2, for the samples, assuming a selected I (set left) --- 1 (right set) this edge, then I have selected the first column or the first row of vertically cut sideways cut. So is eliminated and 1 (left set of) all connected edges represent the asteroid will, for the 1 (the right set of) empathy.

It is found that: the two end points A and B corresponding to the selected edge, then A need not be related to the other side about other selected sides B, and A and B do not need to be selected.

So the essence of this problem is that a number of issues covered by the minimum point.

Minimum coverage problem: find the minimum number of points, so the figure can have all sides has been selected at least one endpoint. It means that a selected point, this point as the end edges will be overwritten.

Then the bipartite graph is equivalent to the smallest coverage bipartite graph maximum matching. 

#include<iostream>
#include<algorithm>
#include<string.h>
#define maxn 1008
using namespace std;
int n,m,e,cnt;
int head[maxn];
int cx[maxn],cy[maxn];
bool vis[maxn];
struct Edge
{
    int to;
    int next;
}edge[maxn*maxn];
inline void add(int u,int v)
{
    edge[++cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt;
    return;
}
inline int dfs(int u)
{
    for(int i=head[u];i;i=edge[i].next){
        int v=edge[i].to;
        if(!vis[v]){
            vis[v]=true;
            if(cy[v]==0||dfs(cy[v])){
                cx[u]=v;
                cy[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    // =freopen("testdata (7).in","r",stdin);
    scanf("%d%d",&n,&m);
    int A,B;
    for(int i=1;i<=m;i++){scanf("%d%d",&A,&B);add(A,B);}
    int ans = 0;
    for(int i=1;i<=n;i++){
        if(!cx[i]) {memset(vis,0,sizeof(vis));years + = dfs (i);} 
    } 
    printf ( " % d \ n " , year); 
}

 

Guess you like

Origin www.cnblogs.com/Absofuckinglutely/p/11365265.html