題解/算法 {378. 骑士放置}
@LINK: https://www.acwing.com/problem/content/380/
;
因为(i,j) 与 (i+2,j+1)/(i+1,j+2)
他们的奇偶性是不同的 即(i+j), (i+2+j+1)/(i+1+j+2)
的奇偶性不同; 因此 对于这个棋盘N*M
根据(i+j)&1
的奇偶性 划分为两个集合;
然后将(i,j) - (i+2, j+1)
对应为二分图里的边;
很容易拿这个题 和https://editor.csdn.net/md/?articleId=128546664
做比较, 很相似, 但其实 他俩是不一样的!
#棋盘这个题#: 这个题, 一个骨牌(i,j) - (i+1,j)
他对应的是 二分图中的一条边 他对应的不是点 而是边, 因此要求最大匹配数 (与点覆盖/点独立集无关), 比如[0, X, 2] (X表示非法)
那么你的二分图里 最大匹配是0(因此答案为0), 而他的最大独立集 是等于2(即0,2
);
而这个题 他一个马(i,j)
就对应二分图中的点 而不是边, 你的目的是 选择若干个点(即马) 使得他们之间没有边连接; 他的二分图最大匹配 不是答案, 比如N=2,M=2
此时你无法连边 (最大匹配为0), 但他的答案实际是4;
因此 实际上 你求的是: 这个二分图里 选择尽可能多的点 满足他俩之间 不存在边连接, 也就是 最大的独立集;
很容易认为 答案是N*M - 最大匹配
(即最大独立集);
这是错误的…
这个N*M
里面 是有非法点的, 这些点是不能选的, 即你计算的最大独立集 里面包含非法点, 因此要再去掉他们 即N*M - K - 最大匹配
;
笔记
If we add the edge
a
−
b
a-b
a−b where if
a
=
(
x
,
y
)
a=(x,y)
a=(x,y) then
b
b
b is the endpoint in the direction
(
2
,
1
)
(
1
,
2
)
(
−
2
,
1
)
(
−
1
,
2
)
(
2
,
−
1
)
(
1
,
−
2
)
(
−
2
,
−
1
)
(
−
1
,
−
2
)
(2,1) (1,2)(-2,1) (-1,2)(2,-1) (1,-2)(-2,-1) (-1,-2)
(2,1)(1,2)(−2,1)(−1,2)(2,−1)(1,−2)(−2,−1)(−1,−2), then we would get a undirected-graph
G
G
G; our goal is to choose as the maximum cells such that there is no edge (not path) between any two cells;
We found that the set of cells is a Maximum-Independent-Set, so we consider that whether
G
G
G is a Bipartite; if we dividing all cells in this way:
0 1 0 1 0
1 0 1 0
0 1 0
1 0
0
if( (x + y) & 1){ belongs to one-set}
else{ belongs to anothe-set}
G G G would be a Bipartite;
So, the answer is the Maximum-Independent-Set in a Bipartite;