HDU5556:Land of Farms(图的最大独立集 & 最大团)

来源:互联网 发布:java计算时间差 毫秒 编辑:程序博客网 时间:2024/06/04 00:46

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 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 TT indicating the number of test cases (T200T≤200). 

Each test case starts with a line containing two integers NN and MM, indicating the size of the land. Each of the following NN lines contains MM characters, describing the map of the land (1N,M101≤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”. XX is the test case number starting from 1. YY is the maximum number of new farms.
Sample Input
33 4..3.023..2112 3......4 411111..119911111
Sample Output
Case #1: 4Case #2: 3Case #3: 1
题意:有N*M的地图,要求建尽量多的不相邻的农场,若建在有数字的地方,拥有同样数字的格子都要建农场(若这些特殊农场本来就相邻也没关系),问最多建几个。

思路:将每个'.'和每类数字编号,相邻的元素建无向图,相当于求图的最大独立集,相当于求补图的最大团。

因为HDU挂了,程序待提交测试。

# include <iostream># include <cstdio># include <vector># include <cstring># include <algorithm>using namespace std;const int maxn = 1e5+8;char s[13][13];int n, m, g[300][300];int id[13][13], vis[103], tmp[103], tot=0;int dp[103], a[103][103], imax, _;void init(){    for(int i=1; i<=n; ++i)    {        for(int j=1; j<=m; ++j)        {            if(s[i][j] == '.') id[i][j] = ++tot;            else if(!vis[s[i][j]])            {                vis[s[i][j]] = ++tot;                id[i][j] = tot;            }            else id[i][j] = vis[s[i][j]];        }    }    for(int i=1; i<=n; ++i)    {        for(int j=1; j<=m; ++j)        {            if(i>1 && (s[i-1][j] == '.'||s[i-1][j] != s[i][j])) g[id[i][j]][id[i-1][j]] = 1;            if(i<n && (s[i+1][j] == '.'||s[i+1][j] != s[i][j])) g[id[i][j]][id[i+1][j]] = 1;            if(j>1 && (s[i][j-1] == '.'||s[i][j-1] != s[i][j])) g[id[i][j]][id[i][j-1]] = 1;            if(j<m && (s[i][j+1] == '.'||s[i][j+1] != s[i][j])) g[id[i][j]][id[i][j+1]] = 1;        }    }}void dfs(int up, int dep){    if(up == 0)    {        if(dep > imax)            imax = dep;        return;    }    for(int i=0; i<up; ++i)    {        int u = a[dep][i];        if(dep + tot - u + 1 <= imax) return;        if(dep + dp[u] <= imax) return;        _++;        int cnt = 0;        for(int j=i+1; j<up; ++j)        {            int v = a[dep][j];            if(!g[u][v]) a[dep+1][cnt++] = v;        }        dfs(cnt, dep+1);        --_;    }}int solve(){    for(int i=tot; i>=1; --i)    {        int cnt = 0;        _ = 0;        tmp[_++] = i;        for(int j=i+1; j<=tot; ++j)            if(!g[i][j]) a[1][cnt++] = j;        dfs(cnt, 1);        dp[i] = imax;    }    return imax;}int main(){    int t, cas=1;    scanf("%d",&t);    while(t--)    {        imax = tot = 0;        memset(g, 0, sizeof(g));        memset(vis, 0, sizeof(vis));        memset(dp, 0, sizeof(dp));        scanf("%d%d",&n,&m);        for(int i=1; i<=n; ++i) scanf("%s",s[i]+1);        init();        printf("Case #%d: %d\n",cas++,solve());    }    return 0;}



原创粉丝点击