题解:AT_agc059_e [AGC059E] Grid 3-coloring
让我来成为这题的,第一篇 2026 年题解。
感觉题解区大部分题解都讲的有点玄学了。
请先学会前置知识 [ARC182D] Increment Decrement Again,核心思想是:
[AGC059E] Grid 3-coloring
给定
n\times n 矩阵C ,其最外圈的4n−4 个格子已经被 3 染色(顺时针给出),问中间部分能否 3 染色。多测,
3 \le n \le 2 \times 10^5 。
zak 有个猜结论然后 LCT的做法,额没仔细看。
同样的,不要这个取模
问题变为,要给外圈的格子加减若干
问题给到圈内,显然有很多必要条件:
- 两个格子的差值,绝对值
\le 哈夫曼距离。 - 内圈一个格子的奇偶性,是确定的(这个似乎是不重要的)。
啊啊啊这有啥用啊,这些转充要条件后也没用啊。回到原问题,是问你 YES or NO,外圈如何导致 NO?
如果不满足这个哈夫曼距离的必要条件就是 NO,如何证有这个必要条件就一定是 YES:
- 首先,证明 NO 时一定没这个条件,看上去非常难,所以无法反证。
- 这里选择构造证明,
这里的构造比较神秘,当然场上可以直接猜了:-
- 发现相邻两项一定是对的(
这不废话吗),额你自己去分讨去吧孩子。 - 大概就是,有些情况是因为哈夫曼距离那个条件而不存在。
-
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define szsz(x) (int)(x.size())
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
using LL = long long;
using DD = double; // 可改 long double
using ULL = unsigned long long;
using UI = unsigned int;
using PII = pair<int, int>;
using i128 = __int128_t;
void work(){
int n;
cin >> n;
char ch;
vector<int> a(5 * n);
for(int i = 1; i <= 4 * n - 4; i++){
cin >> ch, a[i] = ch - '1';
}
for(int i = 4 * n - 4; i >= 2; i--){
a[i] = (a[i] - a[i - 1] + 1) % 3 ? 1 : -1;
}
for(int i = 2; i <= 4 * n - 4; i++) a[i] += a[i - 1];
if(abs(a[1] - a[4 * n - 4]) != 1) return cout << "NO\n", void();
for(int i = 2; i < n; i++){
if(abs(a[i] - a[3 * n - 1 - i]) >= n || abs(a[n - 1 + i] - a[4 * n - 2 - i]) >= n){
return cout << "NO\n", void();
}
}
cout << "YES\n";
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
// freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
int T = 1;
cin >> T;
while(T--) work();
return 0;
}