Codeforces 142B(二分染色搜索)

Posted alphawa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 142B(二分染色搜索)相关的知识,希望对你有一定的参考价值。

要点

  • 会发现本质上棋盘分成了若干个独立集,本集合内的点放不放棋子并不影响其他集合内的
  • 集合的划分方式就是满棋盘跳马步直到全跳过了,然后每个集合就分成两队,我们选人多的那队放棋子,人少那队当禁区
const int maxn = 1e3 + 5;
const int nx[] = -2, -2, -1, -1, 1, 1, 2, 2;
const int ny[] = -1, 1, -2, 2, -2, 2, -1, 1;

int n, m;
bool grid[maxn][maxn];
int ans, cnt[2];

void dfs(int i, int j, int c) 
    cnt[c]++;
    grid[i][j] = 1;
    rep(k, 0, 7) 
        int x = i + nx[k], y = j + ny[k];
        if (x < 1 || x > n || y < 1 || y > m || grid[x][y]) continue;
        dfs(x, y, 1 - c);
    


int main() 
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    rep(i, 1, n)
        rep(j, 1, m)
            if (!grid[i][j]) 
                cnt[0] = cnt[1] = 0;
                dfs(i, j, 0);
                ans += max(cnt[0], cnt[1]);
            
    cout << ans << '\n';
    return 0;

以上是关于Codeforces 142B(二分染色搜索)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #311 (Div. 2) D. Vitaly and Cycle(二分图染色,奇环)

CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)

Edge coloring of bipartite graph CodeForces - 600F (二分图染色)

Codeforces 1093D. Beautiful Graph二分图染色+组合数

Codeforces1161 D. Palindrome XOR(思维,并查集缩点+二分图染色)

CF 85E Guard Towers——二分图染色