题解:AT_soundhound2018_c 広告

· · 题解

题目大意

给定一个 01 矩阵,要求在其中选出最多的 1 使得它们之间没有相邻的边。

Solution

我们可以考虑建立一个二分图,将相邻的两块分到两个不同的子集中连边,再求出最大独立集即可。

因为在最小点覆盖中,任意一条边都被至少选了一个顶点,所以对于其点集的补集,任意一条边都被至多选了一个顶点,所以不存在边连接两个点集中的点,且该点集最大。

因此二分图中,最大独立集 =n \,- 最小点覆盖。而最小点覆盖就是二分图的最大匹配,因此用网络流求出二分图的最大匹配即可。

具体地,按 x+y 的奇偶将每个点分为黑点和白点,并向周围的四个点连边,将所有的黑点连到源点,所有的白点连到汇点,跑 dinic 求最大匹配即可。

加强版:

P2774 方格取数问题

P4474 王者之剑

P3365 骑士共存问题

P4304 [TJOI2013] 攻击装置

P5030 长脖子鹿放置

Code

AC 记录

#include<bits/stdc++.h>
using namespace std;

int read()
{
    int s = 0,w = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') w = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        s = s * 10 + ch - '0';
        ch = getchar();
    }
    return s * w;
}

const int N = 1e6 + 10,inf = 0x3f3f3f3f;
const int dx[5] = {0,0,0,1,-1};
const int dy[5] = {0,1,-1,0,0};

long long n,m,S,T,u,flow,w,op,v,maxflow,a[110][110],num[110][110],color[110][110];
long long sum,cnt = 1,head[N],dep[N],x[N];
struct EDGE
{
    int to,nxt,flow;
}e[N];

void add(int u,int flow,int w)
{
    e[++cnt].to = flow,e[cnt].flow = w,e[cnt].nxt = head[u],head[u] = cnt;
    e[++cnt].to = u,e[cnt].flow = 0,e[cnt].nxt = head[flow],head[flow] = cnt;
}

bool bfs()
{
    int step = 0,t = 0;
    memset(dep,-1,sizeof(dep));
    x[t++] = S,dep[S] = 0;
    while(step < t)
    {
        int now = x[step++];
        for(int i = head[now];i;i = e[i].nxt)
        {
            int v = e[i].to;
            if(dep[v] == -1 && e[i].flow)
            {
                dep[v] = dep[now] + 1;
                x[t++] = v;
            }
        }
    }
    if(dep[T] != -1) return 1;
    return 0;
}

int dfs(int x,int step)
{
    if(x == T) return step;
    int w,used = 0;
    for(int i = head[x];i;i = e[i].nxt)
    {
        int v = e[i].to;
        if(dep[v] == dep[x] + 1 && e[i].flow)
        {
            w = dfs(v,min(step - used,e[i].flow));
            e[i].flow -= w;
            e[i ^ 1].flow += w;
            used += w;
            if(used == step) return step;
        }
    }
    if(!used) dep[x] = -1;
    return used;
}

void dinic(){while(bfs()) maxflow += dfs(S,inf);}

bool check(int x,int y){return (x >= 1 && x <= m && y >= 1 && y <= n);}

void init(){memset(head,0,sizeof(head)),cnt = 1;}

void build_edge()
{
    for(int i = 1;i <= m;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            if(color[i][j] == 0)
            {
                add(S,num[i][j],a[i][j]);
                for(int k = 1;k <= 4;k++)
                {
                    int xx = i + dx[k],yy = j + dy[k];
                    if(check(xx,yy)) add(num[i][j],num[xx][yy],inf);
                }
            }
            else add(num[i][j],T,a[i][j]);
        }
    }
}

int main()
{
    // freopen("input.in","r",stdin);
    init();m = read(),n = read();S = 0,T = 1e5;
    for(int i = 1;i <= m;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            char ch = getchar();
            if(ch == '\n') ch = getchar();
            if(ch == '.') a[i][j] = 1;else a[i][j] = 0;
            sum += a[i][j],op++,num[i][j] = op;
            color[i][j] = (i + j) % 2;
        }
    }
    build_edge(),dinic();
    cout << sum - maxflow << endl;
    return 0;
}