結果

問題 No.3744 XY Tiling
コンテスト
ユーザー hos.lyric
提出日時 2026-09-19 13:45:20
言語 C++14
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 6,247 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,089 ms
コンパイル使用メモリ 161,748 KB
実行使用メモリ 10,048 KB
最終ジャッジ日時 2026-09-19 13:45:36
合計ジャッジ時間 5,596 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
部分点 60 % AC * 9 WA * 10
満点 40 % AC * 31 WA * 29
合計 5 * 0% = 0 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T> ostream &operator<<(ostream &os, const vector<T> &as);
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


int root(vector<int> &uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = root(uf, uf[u]));
}
bool connect(vector<int> &uf, int u, int v) {
  u = root(uf, u);
  v = root(uf, v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}


using Op = pair<pair<int, int>, int>;
vector<Op> transpose(vector<Op> ops) {
  for (Op &op : ops) swap(op.first.first, op.first.second);
  return ops;
}
vector<Op> solve(int M, int N) {
  vector<vector<int>> a(M, vector<int>(N, -1));
  vector<Op> ops;
  auto add = [&](int x, int y) -> void {
    ops.emplace_back(make_pair(x, y), a[x][y]);
  };
  
  if (M == 1) {
    for (int y = 0; y < N; ++y) a[0][y] = (y + 1) / 2;
    for (int y = 0; y < N; ++y) add(0, y);
    return ops;
  }
  if (N == 1) return transpose(solve(N, M));
  if (M == 2) {
    for (int x = 0; x < 2; ++x) for (int y = 0; y < N; ++y) a[x][y] = x;
    for (int y = 0; y < N; ++y) for (int x = 0; x < 2; ++x) add(x, y);
    return ops;
  }
  if (N == 2) return transpose(solve(N, M));
  if (M == 3) {
    for (int x = 0; x < 2; ++x) a[x][0] = 0;
    for (int x = 1; x < 3; ++x) a[x][N - 1] = 1;
    for (int y = 1; y < N; ++y) a[0][y] = 2;
    for (int y = 0; y < 2; ++y) a[2][y] = 3;
    for (int y = 1; y < N - 1; ++y) a[1][y] = 0;
    for (int y = 2; y < N - 1; ++y) a[2][y] = 3 + (y - 1) / 2;
    add(0, 0); add(0, 1);
    add(1, 0); add(2, 0);
    add(1, 1); add(2, 1);
    for (int y = 2; y < N; y += 2) {
      add(0, y); add(1, y);
      add(0, y + 1); add(1, y + 1);
      add(2, y); add(2, y + 1);
    }
    return ops;
  }
  if (N == 3) return transpose(solve(N, M));
  if (M == 4) {
    for (int x = 0; x < 4; ++x) for (int y = 0; y < N; ++y) a[x][y] = (x + 1) / 2;
    for (int y = 0; y < N; ++y) for (int x = 0; x < 4; ++x) add(x, y);
    return ops;
  }
  if (N == 4) return transpose(solve(N, M));
  
  if (M % 2 == 0) {
    for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) a[x][y] = x % 2;
    for (int x = 0; x < M - 1; ++x) a[x][0] = 0;
    for (int x = 1; x < M; ++x) a[x][N - 1] = 1;
    for (int y = 1; y < N; ++y) a[0][y] = 2;
    for (int y = 0; y < N - 1; ++y) a[M - 1][y] = 3;
    for (int x = 2; x < M - 1; ++x) a[x][N - 2] = 0;
    for (int x = 1; x < M - 2; ++x) a[x][1] = 1;
    for (int x = 0; x < M - 2; ++x) for (int y = 0; y < 2; ++y) add(x, y);
    for (int y = 0; y < 2; ++y) for (int x = M - 2; x < M; ++x) add(x, y);
    for (int x = 2; x < M; ++x) for (int y = N - 2; y < N; ++y) add(x, y);
    for (int y = N - 2; y < N; ++y) for (int x = 0; x < 2; ++x) add(x, y);
    for (int y = 2; y < N - 2; ++y) for (int x = 0; x < M; ++x) add(x, y);
    return ops;
  }
  if (N % 2 == 0) return transpose(solve(N, M));
  assert(false);
}

void stress() {
  constexpr int LIM = 10;
  for (int M = 1; M <= LIM; ++M) for (int N = 1; N <= LIM; ++N) if ((M*N) % 2 == 0) {
    cerr << COLOR("93") << "M = " << M << ", N = " << N << COLOR() << endl;
    const auto ops = solve(M, N);
    if (!ops.size()) continue;
    assert((int)ops.size() == M*N);
    vector<string> a(M, string(N, '?')), dir(M, string(N, '?'));
    for (const Op &op : ops) a[op.first.first][op.first.second] = '0' + op.second;
    for (int i = 0; i < M*N; i += 2) {
      const int x0 = ops[i + 0].first.first;
      const int y0 = ops[i + 0].first.second;
      const int x1 = ops[i + 1].first.first;
      const int y1 = ops[i + 1].first.second;
      assert(a[x0][y0] != a[x1][y1]);
      if (x0 == x1) {
        dir[x0][y0] = dir[x1][y1] = '-';
      } else if (y0 == y1) {
        dir[x0][y0] = dir[x1][y1] = '|';
      } else assert(false);
    }
    for (int x = 0; x < M; ++x) cout << a[x] << endl;
    cout << endl;
    for (int x = 0; x < M; ++x) cout << dir[x] << endl;
    cout << endl;
    
    int K = 0;
    for (const Op &op : ops) chmax(K, op.second + 1);
    cerr << "M = " << M << ", N = " << N << ": K = " << K << endl;
    for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) {
      const int k = a[x][y] - '0';
      assert(0 <= k); assert(k < K);
    }
    vector<int> uf(M*N, -1);
    for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) {
      if (x + 1 < M && a[x][y] == a[x + 1][y]) connect(uf, x * N + y, (x+1) * N + y);
      if (y + 1 < N && a[x][y] == a[x][y + 1]) connect(uf, x * N + y, x * N + (y+1));
    }
    vector<int> cs(K, 0);
    for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) {
      const int r = x * N + y;
      if (uf[r] < 0) ++cs[a[x][y] - '0'];
    }
    for (int k = 0; k < K; ++k) assert(cs[k] == 1);
  }
}

int main() {
#ifdef LOCAL
  stress();
#endif
  
  int M, N;
  for (; ~scanf("%d%d", &M, &N); ) {
    const auto ops = solve(M, N);
    int K = 0;
    for (const Op &op : ops) chmax(K, op.second + 1);
    printf("%d\n", K);
    for (int i = 0; i < M*N; ++i) {
      printf("%d %d %d%c", ops[i].first.first + 1, ops[i].first.second + 1, ops[i].second + 1, " \n"[i & 1]);
    }
  }
  return 0;
}
0