题目链接

Problem Statement

We have a grid with N horizontal rows and N vertical columns.
Let (i,j) denote the square at the i-th row from the top and j-th column from the left. A character ci,jc_{i,j}ci,j​ describes the color of (i,j).
B means the square is painted black; W means the square is painted white; ? means the square is not yet painted.

Takahashi will complete the black-and-white grid by painting each unpainted square black or white.
Let the zebraness of the grid be the number of pairs of a black square and a white square sharing a side.
Find the maximum possible zebraness of the grid that Takahashi can achieve.

Sample Input 1

2
BB
BW

Sample Output 1

2

Sample Input 2

3
BBB
BBB
W?W

Sample Output 2

4

Sample Input 3

5
?????
?????
?????
?????
?????

Sample Output 3

40

建图+网络流~
对一个大小为 n∗nn*nn∗n 的矩阵,显然最大值就是 2∗n∗(n−1)2*n*(n-1)2∗n∗(n−1),那么这题就可以转化成一个最小割(或者最大流),答案就是最大值减去最小割,建图如下:

  • 每一个点和四周的点连边,流量为 111
  • 对所有非 ? 的点,判断奇偶性连一条 inf 边到源点或者汇点

注意算法复杂度,EK 算法可能会超时,要用 Dicnic 算法,AC代码如下:

#include <bits/stdc++.h>using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 5;
#define inf 0x3f3f3f3fstruct edge {int from, to;int cap;int flow;edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) {}
};vector<edge> edges;
vector<int> G[MAXN];
int vis[MAXN];
int d[MAXN];
int cur[MAXN];
int m;
int s, t;void add(int from, int to, int cap) {edges.push_back(edge(from, to, cap, 0));edges.push_back(edge(to, from, 0, 0));int m = edges.size();G[from].push_back(m - 2);G[to].push_back(m - 1);
}bool bfs() {memset(vis, 0, sizeof(vis));queue<int> q;q.push(s);d[s] = 1;vis[s] = 1;while (!q.empty()) {int x = q.front();q.pop();for (int i = 0; i < G[x].size(); i++) {edge &e = edges[G[x][i]];if (!vis[e.to] && e.cap > e.flow) {vis[e.to] = 1;d[e.to] = d[x] + 1;q.push(e.to);}}}return vis[t];
}int dfs(int x, int a) {if (x == t || a == 0)return a;int flow = 0, f;for (int &i = cur[x]; i < G[x].size(); i++) {edge &e = edges[G[x][i]];if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) {e.flow += f;edges[G[x][i] ^ 1].flow -= f;flow += f;a -= f;if (a == 0)break;}}return flow;
}int dicnic() {int ans = 0;while (bfs()) {memset(cur, 0, sizeof(cur));ans += dfs(s, inf);}return ans;
}int main() {int n;cin >> n;vector<string> c(n);for (int i = 0; i < n; i++) cin >> c[i];s = n * n + 1, t = n * n + 2;for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {int x = i * n + j;if (c[i][j] == 'B') {if ((i + j) % 2) add(s, x, inf);else add(x, t, inf);} else if (c[i][j] == 'W') {if ((i + j) % 2) add(x, t, inf);else add(s, x, 10);}if (i < n - 1) {int y = (i + 1) * n + j;add(x, y, 1);add(y, x, 1);}if (j < n - 1) {int y = i * n + j + 1;add(x, y, 1);add(y, x, 1);}}}cout << 2 * n * (n - 1) - dicnic();return 0;
}

Caddi Programming Contest 2021(AtCoder Beginner Contest 193) F.Zebraness相关推荐

  1. Caddi Programming Contest 2021(AtCoder Beginner Contest 193) 题解

    Caddi Programming Contest 2021(AtCoder Beginner Contest 193) A - Discount 打折浮点数除即可 B - Play Snuke 枚举 ...

  2. NEC Programming Contest 2021(AtCoder Beginner Contest 229) B - Hard Calculation

    题目链接:B - Hard Calculation (atcoder.jp) Problem Statement You are given positive integers A and B. Le ...

  3. Atcoder TOYOTA SYSTEMS Programming Contest 2021(AtCoder Beginner Contest 228) B - Takahashi‘s Secret

    题目链接:B - Takahashi's Secret (atcoder.jp) Problem Statement Takahashi has N friends. They have nickna ...

  4. Atcoder TOYOTA SYSTEMS Programming Contest 2021(AtCoder Beginner Contest 228) C - Final Day

    题目链接:C - Final Day (atcoder.jp) Problem Statement N students are taking a 4-day exam. There is a 300 ...

  5. KYOCERA Programming Contest 2021 (AtCoder Beginner Contest 200) A~E 题解

    ABC200/KYOCERA2021 A~E [A - Century](https://atcoder.jp/contests/abc200/tasks/abc200_a) 题目大意 输入格式 输出 ...

  6. Mynavi Programming Contest 2021 (AtCoder Beginner Contest 201) A~E 题解

    ABC201/Mynavi2021 A~E [A - Tiny Arithmetic Sequence](https://atcoder.jp/contests/abc201/tasks/abc201 ...

  7. NEC Programming Contest 2021 (AtCoder Beginner Contest 229)

    终于开始补提了 重点 : C, E的倒着算, F的染色,G的相邻的转换: B - Hard Calculation #include <iostream> #include <alg ...

  8. TOYOTA SYSTEMS Programming Contest 2021(AtCoder Beginner Contest 228) ABCD

    A 题意: 有一个开关,每天s点开,t点关(可能在第2天或第n天),判断x点时开着还是关着. 思路: 按照是否需要隔夜分个类. #include<bits/stdc++.h> using ...

  9. NOMURA Programming Contest 2021(AtCoder Regular Contest 121)

    文章目录 A - 2nd Greatest Distance B - RGB Matching C - Odd Even Sort D - 1 or 2 E - Directed Tree F - L ...

最新文章

  1. 纯数学教程 Page 203 例XLI (6)
  2. VC模仿超炫QQ界面的实现
  3. 石头剪刀布php源码,php实现的网页版剪刀石头布游戏示例
  4. 三线调速风扇原理_学修电风扇~风机转速慢、调速失灵故障维修。
  5. The temporary upload location [/tmp/tomcat.xxx/work/Tomcat/localhost/etc] is not valid
  6. 零基础学习什么编程语言比较合适?别的不说,听说大佬都学了这个!
  7. LeetCode:面试题40. 最小的k个数
  8. mini 打开窗口提交表单,按钮在页脚
  9. 首个月球旅客!SpaceX将送普通人上太空,马斯克暗示首单来自日本
  10. Unity3D基础29:消息发送
  11. 孙鑫 VC++深入详解——学习笔记
  12. 【Life】 Never Too Late, Just Do it Better!
  13. 【数值预报】按时间维度合并/重新生成nc、grib网格数据(按天、小时组织的文件合并成按月组织文件)
  14. 现在各种云建站,挑两个给大家分析一下。
  15. 何为非侵入式负荷识别-事件检测
  16. linux备份mysql文件并恢复的脚本,以及其中出现的错误:ERROR: ASCII '\0' appeared in the statement...
  17. IDEA文件UTF-8格式控制台输出中文乱码
  18. 【PMP备考经验分享】从选择培训机构到5A通关,你需要做什么?
  19. 基于MATLAB的神经网络(ANN)回归
  20. CAN总线和485总线的区别

热门文章

  1. 【D1N910】正则表达式30分钟入门教程 (二)-学习笔记 实践
  2. Microsoft Word Mac版 v2021 16.45 文档处理工具
  3. 斐讯N1 安装mysql
  4. springboot+mysql社区疫情管控系统-计算机毕业设计源码19081
  5. PyQt5 Qt 窗体去边框,去边框后可移动
  6. 【数学建模】1层次分析法模型部分
  7. Office2010最佳体验之 同工作齐分享
  8. 自行车存放管理系统c语言课程设计,基于51单片机的自行车智能辅助系统设计-(Proteus仿真图+源程序+论文)...
  9. 服务器小文件,小型文件服务器
  10. 微信小程序-点赞业务实现