HDU5556Land of Farms 【二分匹配+最大独立集+奇偶分类】

Land of Farms

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 622    Accepted Submission(s): 201


Problem Description
Farmer John and his brothers have found a new land. They are so excited and decide to build new farms on the land. The land is a rectangle and consists of N×M grids. A farm consists of one or more connected grids. Two grids are adjacent if they share a common border, i.e. their Manhattan distance is exactly 1. In a farm, two grids are considered connected if there exist a series of adjacent grids, which also belong to that farm, between them.

Farmer John wants to build as many farms as possible on the new land. It is required that any two farms should not be adjacent. Otherwise, sheep from different farms would fight on the border. This should be an easy task until several ancient farms are discovered.

Each of the ancient farms also consists of one or more connected grids. Due to the respect to the ancient farmers, Farmer John do not want to divide any ancient farm. If a grid from an ancient farm is selected in a new farm, other grids from the ancient farm should also be selected in the new farm. Note that the ancient farms may be adjacent, because ancient sheep do not fight each other.

The problem is a little complicated now. Can you help Farmer John to find a plan with the maximum number of farms?


Input
The first line of input contains a number T indicating the number of test cases (T≤200).

Each test case starts with a line containing two integers N and M, indicating the size of the land. Each of the following N lines contains M characters, describing the map of the land (1≤N,M≤10). A grid of an ancient farm is indicated by a single digit (0-9). Grids with the same digit belong to the same ancient farm. Other grids are denoted with a single character “.”. It is guaranteed that all test cases are valid.


Output
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the maximum number of new farms.


Sample Input
3
3 4
..3.
023.
.211
2 3
...
...
4 4
1111
1..1
1991
1111


Sample Output
Case #1: 4
Case #2: 3
Case #3: 1


Source
2015ACM/ICPC亚洲区合肥站-重现赛(感谢中科大)

模拟赛时做的 刚接触到这题 一面懵逼
猜到是最大独立集,但是不会构图
目测是最大团,但是没最大团板子
果然是菜….只能靠题解了


首先,不考虑ancient farms
对点(i,j),其上下左右的点(ni,nj),有(i+j)%2!=(ni+nj)%2
所以 可以将点按(i+j)的奇偶性分为2个集合
(i,j)对与其相邻的点建边 跑一遍二分匹配
图的最大独立集(二分图X,Y集合的点数之和-最大匹配)就是答案ans=nx+ny-maxMatch
HK算法可以在 O(V1/2E) 内求出最大匹配


考虑上ancient farms
发现ancient farms最多只有10个
如果枚举任意一个ancient farms是否建为新农场
对建成新农场的ancient farms的点以及其附近的点全部删掉
对没建成新农场的ancient farms的点也删掉
于是问题转化为上面的最大独立集问题
需要 O(210V1/2E)


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include <string.h>
#include<math.h>
#include<unordered_set>

using namespace std;
#define ll long long
#define pii pair<int,int>

const int inf=1e9+7;
const int N = 250;
const int M = N*N*5;
const int INF=inf;

const int NX=130;
const int NY=130;

struct Edge{
    int to,next;
}edge[NX*NY];
int head[NX];

inline void addEdge(int k,int u,int v){
    edge[k].to=v;
    edge[k].next=head[u];
    head[u]=k;
}

int nx,ny;//左右集合顶点数
int cx[NX],cy[NY];//x/y配对到的点的标号 标号:x:1-nx y:1-ny
int disX[NX],disY[NY];//顶点距离标号
int dis;//可匹配点距离
bool bmask[NY];//寻找增广路时的标志数组

bool searchPath(){//是否存在增广路
    queue<int>que;
    dis=INF;
    fill(disX,disX+nx+1,-1);
    fill(disY,disY+ny+1,-1);
    for(int i=1;i<=nx;++i){
        if(cx[i]==-1){
            que.push(i);
            disX[i]=0;
        }
    }
    while(!que.empty()){
        int u=que.front();
        que.pop();
        if(disX[u]>dis){
            break;
        }
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(disY[v]==-1){
                disY[v]=disX[u]+1;
                if(cy[v]==-1){
                    dis=disY[v];
                }
                else{
                    disX[cy[v]]=disY[v]+1;
                    que.push(cy[v]);
                }
            }
        }
    }
    return INF!=dis;
}

int findPath(int u){
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(!bmask[v]&&disY[v]==disX[u]+1){
            bmask[v]=1;
            if(cy[v]!=-1&&disY[v]==dis){//disY[v]==dis 但是v已经被匹配了
                continue;
            }
            if(cy[v]==-1||findPath(cy[v])){//v未被匹配 或者 v被匹配 但是匹配v的点 又找到新的匹配点
                cy[v]=u;
                cx[u]=v;
                return 1;
            }
        }
    }
    return 0;
}

int maxMatch(){
    int res=0;
    fill(cx,cx+nx+1,-1);
    fill(cy,cy+ny+1,-1);
    while(searchPath()){
        fill(bmask,bmask+ny+1,false);
        for(int i=1;i<=nx;++i){
            if(cx[i]==-1){
                res+=findPath(i);
            }
        }
    }
    return res;
}

int state[12];
int sIdx[12];
char mp[12][12];
bool use[12][12];
int dirX[4]={0,0,-1,1};
int dirY[4]={-1,1,0,0};

int initState(int n,int m){
    state[0]=1;
    fill(sIdx,sIdx+12,-1);
    for(int i=1;i<=9;++i){
        state[i]=state[i-1]<<1;
    }
    int stateNum=0;
    for(int i=0;i<n;++i){
        for(int j=0;j<m;++j){
            if(mp[i][j]!='.'){
                int num=mp[i][j]-'0';
                if(sIdx[num]==-1){
                    sIdx[num]=stateNum++;
                }
                mp[i][j]='0'+sIdx[num];
            }
        }
    }
    return stateNum;
}

bool initUse(int s,int n,int m){//初始化(i,j)是否被删掉,并返回ancient farms是否相邻
    for(int i=0;i<n;++i){
        fill(use[i],use[i]+m,true);
    }
    for(int i=0;i<n;++i){
        for(int j=0;j<m;++j){
            if(mp[i][j]!='.'){
                use[i][j]=false;
                int num=mp[i][j]-'0';
                if(state[num]&s){
                    for(int k=0;k<4;++k){
                        int ni=i+dirX[k];
                        int nj=j+dirY[k];
                        if(ni>=0&&ni<n&&nj>=0&&nj<m){
                            use[ni][nj]=false;
                            if(mp[ni][nj]!=mp[i][j]){
                                int num1=mp[i][j]-'0';
                                int num2=mp[ni][nj]-'0';
                                if((num1!=num2)&&(state[num2]&s)){
                                    return false;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return true;
}

int idx[12][12];//idx[i][j]=(i,j)在二分图中的标号

void getIdx(int i,int j,int&x,int&y){
    if(idx[i][j]==-1){
        if((i+j)&1){
            nx++;
            idx[i][j]=nx;
        }
        else{
            ny++;
            idx[i][j]=ny;
        }
    }
    if((i+j)&1){
        x=idx[i][j];
    }
    else{
        y=idx[i][j];
    }
}

void buildG(int n,int m){
    for(int i=0;i<n;++i){
        fill(idx[i],idx[i]+m,-1);
    }
    nx=ny=0;
    int edgeK=0;
    fill(head,head+n*m+1,-1);
    for(int i=0;i<n;++i){
        for(int j=0;j<m;++j){
            if(!use[i][j]){
                continue;
            }
            int x,y;
            getIdx(i,j,x,y);
            for(int k=0;k<4;++k){
                int ni=i+dirX[k];
                int nj=j+dirY[k];
                if(ni>=0&&ni<n&&nj>=0&&nj<m&&use[ni][nj]){
                    getIdx(ni,nj,x,y);
                    addEdge(edgeK++,x,y);
                }
            }
        }
    }
}

int divi(int s){//s状态下 多少个ancient farms被建成新农村
    int ans=0;
    while(s){
        ans+=(s&1);
        s>>=1;
    }
    return ans;
}

int slove(int n,int m){
    int stateNum=initState(n,m);//初始化ancient farms状态,返回有多少个ancient farms 
    int ans=0;
    for(int s=0,end=1<<stateNum;s<end;++s){//枚举每种古代建筑建or不建
        if(initUse(s,n,m)){
            buildG(n,m);
            int match=maxMatch();
            ans=max(ans,nx+ny-match+divi(s));
        }
    }
    return ans;
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;++t){
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;++i){
            scanf("%s",mp[i]);
        }
        printf("Case #%d: %d\n",t,slove(n,m));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值