---
id: 2267
lang: en
translation_of: index.md
source_sha256: 82037e0eb34575219453fbafede97d7a3790503858378344626b3bca02b3022a
title: "P4380: [USACO18OPEN] Multiplayer Moo S — Solution"
description: "Doing the second part directly takes Θ(n^4) time; instead, collect the Θ(n^2) bordering positions, group them, and handle them with a DSU."
tags: ["OI","Disjoint-Set Union"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The tuple $(x, y, c_a, c_y)$ keeps the source's c_a (presumably meant c_x) unchanged."
---

## Main Idea

This problem is kind of <span class="slang" data-zh="nb" data-gloss="Pinyin initials of 牛逼 (niúbī), “cow” + a vulgar word. Crude slang for awesome or impressive.">badass</span>.

Logically speaking, doing the second part directly would take $\Theta(n^4)$ time, and it doesn't look like there's much to work with either. Consider computing all the bordering positions $(x, y, c_a, c_y)$, meaning that the point with color $c_x$ and index $x$ borders the point with color $c_y$ and index $y$. There are only $\Theta(n^2)$ such bordering positions, so we just sort them into groups and then handle them with a DSU.

<!--more-->

## Code

```cpp
// P4380.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 255;

int n, mat[MAX_N][MAX_N], mem[MAX_N * MAX_N], siz[MAX_N * MAX_N], ntot, psiz[MAX_N * MAX_N];
bool appear[MAX_N * MAX_N];

struct node
{
    int x, y, colx, coly;
    bool operator<(const node &rhs) const { return colx < rhs.colx || (colx == rhs.colx && coly < rhs.coly); }
} nodes[MAX_N * MAX_N * MAX_N];

int getId(int x, int y) { return (x - 1) * n + y; }

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

int find(int x) { return x == mem[x] ? x : mem[x] = find(mem[x]); }

void newNode(int x, int y, int colx, int coly)
{
    if (colx > coly)
        swap(x, y), swap(colx, coly);
    nodes[++ntot] = node{x, y, colx, coly};
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            scanf("%d", &mat[i][j]), mem[getId(i, j)] = getId(i, j);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            if (check(i - 1, j) && mat[i][j] == mat[i - 1][j] && find(getId(i, j)) != find(getId(i - 1, j)))
                mem[find(getId(i, j))] = find(getId(i - 1, j));
            if (check(i, j - 1) && mat[i][j] == mat[i][j - 1] && find(getId(i, j)) != find(getId(i, j - 1)))
                mem[find(getId(i, j))] = find(getId(i, j - 1));
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            siz[find(getId(i, j))]++;
    int ans1 = 0, ans2 = 0;
    memcpy(psiz, siz, sizeof(siz));
    for (int i = 1; i <= n * n; i++)
        ans1 = max(ans1, siz[i]);
    printf("%d\n", ans1);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            if (check(i - 1, j) && mat[i][j] != mat[i - 1][j])
                newNode(find(getId(i, j)), find(getId(i - 1, j)), mat[i][j], mat[i - 1][j]);
            if (check(i, j - 1) && mat[i][j] != mat[i][j - 1])
                newNode(find(getId(i, j)), find(getId(i, j - 1)), mat[i][j], mat[i][j - 1]);
        }
    sort(nodes + 1, nodes + 1 + ntot);
    for (int i = 1; i <= ntot; i++)
    {
        vector<int> idx;
        idx.push_back(nodes[i].x), idx.push_back(nodes[i].y);
        appear[nodes[i].x] = appear[nodes[i].y] = true;
        if (find(nodes[i].x) != find(nodes[i].y))
        {
            psiz[find(nodes[i].y)] += psiz[find(nodes[i].x)];
            mem[find(nodes[i].x)] = find(nodes[i].y);
        }
        while (i + 1 <= ntot && nodes[i + 1].colx == nodes[i].colx && nodes[i + 1].coly == nodes[i].coly)
        {
            i++;
            if (!appear[nodes[i].x])
                appear[nodes[i].x] = true, idx.push_back(nodes[i].x);
            if (!appear[nodes[i].y])
                appear[nodes[i].y] = true, idx.push_back(nodes[i].y);
            if (find(nodes[i].x) != find(nodes[i].y))
            {
                psiz[find(nodes[i].y)] += psiz[find(nodes[i].x)];
                mem[find(nodes[i].x)] = find(nodes[i].y);
            }
        }
        for (int i = 0, siz_ = idx.size(); i < siz_; i++)
            ans2 = max(ans2, psiz[idx[i]]);
        for (int i = 0, siz_ = idx.size(); i < siz_; i++)
            mem[idx[i]] = idx[i], psiz[idx[i]] = siz[idx[i]], appear[idx[i]] = false;
    }
    printf("%d\n", ans2);
    return 0;
}
```
