hdu 5556 Land of Farms 最大团+暴力

Land of Farms

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

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亚洲区合肥站-重现赛(感谢中科大)

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5921 5920 5919 5918 5917

题意:  有一个农场大小为n*m  里面有一些古老的农田,你现在需要新建一些农田,要求新建的农田之间不能相连,古老的农田不可拆分,如果你选择了一块土地(原为古老的农田)建立新农田则需要把该块古老的农田全部包含。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define CT continue
#define SC scanf

char f[15][15];
int anc[16],flag[14],mp[12][12],cnta,ans,res;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0},match[105],used[105];
vector<int> G[205];
int cas,r,l;
bitset<15> sta;

void add_edge(int u,int v)
{
    G[u].push_back(v);
    G[v].push_back(u);
    //if(sta==0) cout<<"u: "<<u<<" v:"<<v<<"\n";
}

bool dfs(int u)
{
     used[u]=1;
     for(int i=0;i<G[u].size();i++){
         int v=G[u][i],w=match[v];
         if(w<0||(!used[w]&&dfs(w))){
             match[u]=v;
             match[v]=u;
             return true;
         }
     }
     return false;
}

int bi_mactch()
{
    int res=0;
    MM(match,-1);
    for(int i=1;i<=r;i++)   for(int j=1;j<=l;j++){
      int k=(i-1)*l+j;
      if(match[k]<0){
         MM(used,0);
         if(dfs(k)) res++;
      }
    }
    return res;
}

int par[15];

int findr(int u)
{
    if(par[u]!=u)
       par[u]=findr(par[u]);
    return par[u];
}

void unite(int u,int v)
{
    int ru=findr(u),rv=findr(v);
    if(ru!=rv) par[ru]=rv;
}

int num=0;
void sear(int i,int j)
{
    if(f[i][j]>=‘0‘&&f[i][j]<=‘9‘){
       int k=f[i][j]-‘0‘;
       if(!flag[k]) return;
       mp[i][j]=-1;
       for(int d=0;d<4;d++){
              int tx=i+dx[d],ty=j+dy[d];
              if(f[tx][ty]>=‘0‘&&f[tx][ty]<=‘9‘){
                 int k2=f[tx][ty]-‘0‘;
                 if(flag[k2]) unite(k,k2);
              }
              else if(f[tx][ty]==‘.‘)  mp[tx][ty]=-1;
          }
    }
}

void bgraph()
{
    for(int i=1;i<=110;i++) G[i].clear();
    for(int i=1;i<=r;i++)
      for(int j=1;j<=l;j++)
        if(f[i][j]==‘.‘&&!mp[i][j]){
           num++;
           int k=(i-1)*l+j;
           //if(sta==0) cout<<"kk:"<<k<<"\n";
           if(f[i-1][j]==‘.‘&&!mp[i-1][j]) add_edge(k,k-l);
           if(f[i][j-1]==‘.‘&&!mp[i][j-1]) add_edge(k,k-1);
        }
}

void solve()
{
    ans=0;
    for(int k=0;k<=(1<<cnta)-1;k++){
        res=0;MM(mp,0);MM(flag,0);
        sta=k;
        for(int i=0;i<cnta;i++) if(sta[i]) {
            int w=anc[i+1];flag[w]=1;
            par[w]=w;
        }
        for(int i=1;i<=r;i++)   for(int j=1;j<=l;j++)  sear(i,j);
        for(int i=1;i<=cnta;i++)
            if(flag[anc[i]]&&par[anc[i]]==anc[i]) res++;
        num=0;bgraph();
        ans=max(ans,res+num-bi_mactch());
    }
}

int main()
{
    SC("%d",&cas);
    int kk=0;
    while(cas--){
        SC("%d%d",&r,&l);
        MM(flag,0);
        cnta=0;
        for(int i=1;i<=r;i++) {
            SC("%s",f[i]+1);
            for(int j=1;j<=l;j++)
              if(f[i][j]!=‘.‘){
                int k=f[i][j]-‘0‘;
                if(!flag[k]){
                    flag[k]=1;
                    anc[++cnta]=k;
                }
            }
        }
        solve();
        printf("Case #%d: %d\n",++kk,ans);
    }
    return 0;
}

  分析:

1.先2^10=10^3暴力枚举选择的古代的田,选择了后,在用并查集维护一下,能产生的牧田数。

2.删除选择的古代的田周围的普通田地,再用下网格的二分图,考虑不想邻的连接一条边,那么最后显然成成了,求这样一个最大团

3.二分图的最大团=补图的最大点独立集合=顶点数-最大匹配数

时间: 2024-10-29 00:58:12

hdu 5556 Land of Farms 最大团+暴力的相关文章

HDU 3699 A hard Aoshu Problem (暴力搜索)

题意:题意:给你3个字符串s1,s2,s3;要求对三个字符串中的字符赋值(相同的字符串进行相同的数字替换), 替换后的三个数进行四则运算要满足左边等于右边,求有几种解法. Sample Input 2 A A A BCD BCD B Sample Output 5 72 eg:ABBDE   ABCCC   BDBDE :令 A = 1, B = 2, C = 0, D = 4, E = 5 12245 + 12000 = 24245: 注意没有前导零!! #include<stdio.h>

hdu 2721(字符串处理,位运算 暴力)

Persistent Bits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 201    Accepted Submission(s): 116 Problem Description WhatNext Software creates sequence generators that they hope will produce

hdu 4585 Shaolin两种方法(暴力和STL)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The mast

hdu 4886 TIANKENG’s restaurant(2)(hash+暴力)

题目链接:hdu 4886 TIANKENG's restaurant(2) 题目大意:给定一个字符串S,要求在该字符串中找到一个最短并且字符串字典序最小. 解题思路:每次枚举字符串的长度,然后将S中所有该长度的子串映射成一个9进制数,最后再遍历一遍标记数组. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1000005; i

HDU 4930 Fighting the Landlords (超级暴力+读懂题意)

题目链接:HDU 4930 Fighting the Landlords 斗地主!!.不会玩这游戏,真是苦逼.题意其他都还好,就是要注意只要第一个回合1号玩家能压制2号玩家就算赢了(突破点). 其他就分类暴力了,思路还是比较清晰的. 注意点: 1.对方炸弹,必输 2.一回合就出完牌,必胜 AC代码: #include<stdio.h> #include<string.h> int vis1[30],vis2[30]; int find(char s) { if(s=='T') re

Land of Farms HDU - 5556 二分图匹配

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×MN×Mgrids. A farm consists of one or more connected grids. Two grids are adjacent if they sha

hdu 4585 Shaolin两种方法(暴力和STL map set)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The mast

hdu 3183 A Magic Lamp rmq或者暴力

A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki must answer a question

hdu 4932 Miaomiao&amp;#39;s Geometry(暴力枚举)

Miaomiao's Geometry                                                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description There are N point on X-axis . Miaomiao would like