首页 > 其他分享 >AtCoder Beginner Contest 193 F Zebraness

AtCoder Beginner Contest 193 F Zebraness

时间:2023-05-25 13:47:16浏览次数:66  
标签:AtCoder solver int flow 193 Zebraness add edges len

洛谷传送门

AtCoder 传送门

复习一下最小割。

考虑翻转横纵坐标和为奇数的颜色,那么就变成了,相邻两个格子,相同颜色产生 \(1\) 的贡献。

一眼 P4313 文理分科。每个格子被分到 \(S\) 表示染黑,分到 \(T\) 表示染白。对于每两个相邻格子,建两个虚点,分别表示它们都染黑或者都染白的情况。

最后跑一下最小割。

code
// Problem: F - Zebraness
// Contest: AtCoder - Caddi Programming Contest 2021(AtCoder Beginner Contest 193)
// URL: https://atcoder.jp/contests/abc193/tasks/abc193_f
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 110;
const int maxm = 1000100;
const int inf = 0x3f3f3f3f;
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};

int n, ntot, head[maxm], len = 1, S, T, id[maxn][maxn];
char s[maxn][maxn];

struct edge {
	int to, next, cap, flow;
} edges[maxm];

inline void add_edge(int u, int v, int c, int f) {
	edges[++len].to = v;
	edges[len].next = head[u];
	edges[len].cap = c;
	edges[len].flow = f;
	head[u] = len;
}

struct Dinic {
	int d[maxm], cur[maxm];
	bool vis[maxm];
	
	inline void add(int u, int v, int c) {
		add_edge(u, v, c, 0);
		add_edge(v, u, 0, 0);
	}
	
	inline bool bfs() {
		for (int i = 1; i <= ntot; ++i) {
			vis[i] = 0;
			d[i] = -1;
		}
		queue<int> q;
		q.push(S);
		vis[S] = 1;
		d[S] = 0;
		while (q.size()) {
			int u = q.front();
			q.pop();
			for (int i = head[u]; i; i = edges[i].next) {
				edge &e = edges[i];
				if (!vis[e.to] && e.cap > e.flow) {
					vis[e.to] = 1;
					d[e.to] = d[u] + 1;
					q.push(e.to);
				}
			}
		}
		return vis[T];
	}
	
	int dfs(int u, int a) {
		if (u == T || !a) {
			return a;
		}
		int flow = 0, f;
		for (int &i = cur[u]; i; i = edges[i].next) {
			edge &e = edges[i];
			if (d[e.to] == d[u] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) {
				e.flow += f;
				edges[i ^ 1].flow -= f;
				flow += f;
				a -= f;
				if (!a) {
					break;
				}
			}
		}
		return flow;
	}
	
	inline int solve() {
		int flow = 0;
		while (bfs()) {
			for (int i = 1; i <= ntot; ++i) {
				cur[i] = head[i];
			}
			flow += dfs(S, inf);
		}
		return flow;
	}
} solver;

void solve() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%s", s[i] + 1);
		for (int j = 1; j <= n; ++j) {
			id[i][j] = ++ntot;
			if (s[i][j] != '?' && ((i + j) & 1)) {
				s[i][j] = (s[i][j] == 'B' ? 'W' : 'B');
			}
		}
	}
	S = ++ntot;
	T = ++ntot;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			if (s[i][j] == 'B') {
				solver.add(S, id[i][j], inf);
			} else if (s[i][j] == 'W') {
				solver.add(id[i][j], T, inf);
			}
		}
	}
	int s = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			if ((i + j) & 1) {
				continue;
			}
			for (int k = 0; k < 4; ++k) {
				int nx = i + dx[k], ny = j + dy[k];
				if (nx < 1 || nx > n || ny < 1 || ny > n) {
					continue;
				}
				int u = ++ntot;
				solver.add(S, u, 1);
				solver.add(u, id[i][j], inf);
				solver.add(u, id[nx][ny], inf);
				u = ++ntot;
				solver.add(u, T, 1);
				solver.add(id[i][j], u, inf);
				solver.add(id[nx][ny], u, inf);
				s += 2;
			}
		}
	}
	printf("%d\n", s - solver.solve());
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:AtCoder,solver,int,flow,193,Zebraness,add,edges,len
From: https://www.cnblogs.com/zltzlt-blog/p/17430921.html

相关文章

  • IEC 60958 && IEC 61937
    IEC60958&&IEC61937IEC60958IEC60958是一种传递数字音频的接口规范,相比I2S,IEC60958通过一根线同时传递时钟信号和数据信号。IEC60958用来传递两channel,16/20/24bits采样深度的PCM数据。IEC60958在传输数据时使用双相符号编码(BiphaseMarkCode),简称BMC,属于一种相位......
  • AtCoder Beginner Contest 302 H. Ball Collector 题解
    AtCoderBeginnerContest302H.BallCollector题意跳过。可以视作将\(a_i,b_i\)之间连了一条边,然后\(a_i,b_i\)之间只能选一个等价于对于一条边只能选择其一个端点。那么对于只包含树的联通块而言,如果都选择儿子节点,那么会有一个根节点无法被选择上;而对于包含至少一个......
  • AtCoder Beginner Contest 302(E,F,G)
    AtCoderBeginnerContest302(E,F,G)E(图,set)E这个题意大致为一开始给出\(n\)个点,没有一条边,后面陆续会有\(q\)次操作,以下两种方式\(1\),输入两个点,代表连接这两个点\(2\),输入一个点,意在把所有和这个点相连的边都删除每一次操作后我的都需要知道操作后还有多少个孤立无援的点(没......
  • AtCoder Regular Contest 132 E Paw
    洛谷传送门AtCoder传送门感觉挺educational的。观察最终形态,发现根据洞分段,有且只有一段没被覆盖,并且左端是向左的脚印,右端是向右的脚印。最终状态就这几种了,直接枚举,概率乘贡献即可。发现左端和右端不影响到对方是对称的,直接求出一边的概率。设\(f_i\)为\(i\)个洞,填......
  • AtCoder Regular Contest 132 F Takahashi The Strongest
    洛谷传送门AtCoder传送门没见过这种在新运算下做卷积的题,感觉挺新奇的。考虑Takahashi成为绝对赢家的必要条件,发现前提是Aoki和Snuke出的要相同。不妨将每种策略映射到一个四进制数(\(P\to1,R\to2,S\to3\)),定义运算\(x\otimesy=\begin{cases}x&x=y\\0......
  • poj-1930
    //144K0MSC++#include<cstdio>#include<cstring>#include<cmath>intgcd(inta,intb){if(b==0){returna;}elseif(a>b){returngcd(b,a%b);}else{returngcd(a,b%a);}}/......
  • AtCoder Beginner Contest 296
    AtCoderBeginnerContest296D题意给出n和m,问\(1\leqi,j\leqn\),使得\(ij\geqm\),求出这个乘积的最小值思路这两个乘数至少有一个在\([1,\sqrt{m}]\),枚举代码voidsolve(){ cin>>n>>m; intx=sqrt(m); if(n>=m){cout<<m<<endl;return;} if(x*x==m)......
  • AtCoder Regular Contest 139 C One Three Nine
    洛谷传送门AtCoder传送门闲话:做这场的B用时跟C差不多不会直接构造,因此这是一个无脑做法。考虑对于\(\forallx\in[1,n],y\in[1,m],(x+3y,3x+y)\)看成一个点,那么选择的\((x,y)\)要满足,不存在一行或一列有超过\(1\)个点。这启发我们对于合法的点\((a......
  • Atcoder 选做
    [ARC103F]DistanceSums(构造,重心)首先显然\(D_i\)的最小值被重心取到,不妨以重心为根。对于一条边连接的两个点\(x,y\),不妨设这条边\(x\)侧的点数为\(siz_x\),\(y\)侧为\(siz_y\)。那么\(D_y=D_x+siz_x-siz_y=D_x+siz_x-(n-siz_x)=D_x+2\timessiz_x-n\)。那么......
  • Atcoder Grand Contest 060 D - Same Descent Set
    先推式子。设\(f(S)\)表示decent集合恰好为\(S\)的排列个数,\(g(S)\)表示\(S\)是\(p\)的decent集合的一个子集的排列\(p\)个数,\(g'(\{a_1,a_2,\cdots,a_k\})=\dfrac{n!}{a_1!(a_2-a_1)!(a_3-a_2)!\cdots(a_k-a_{k-1})!(n-a_k)!}\),那么有:\[\begin{aligned}ans=&\......