版本比较
题解:P14662 异邻或居
正文
增加 62 行删除 8 行
12345624 行未改动31323334353637383931 行未改动717273747576777879808182838485868788899091929394954 行未改动100101102103104105106本题解的主要目的是给出一个边数最小的构造并进行证明。# 1. 题意对于**无向简单连通图** $G=(V,E)$,设24 行未改动S(n)=\bigoplus_{i=1}^n i=\left\{\begin{matrix}n & n \equiv 0 \pmod 4 \\1 & n \equiv 1 \pmod 4 \\n+1 & n \equiv 2 \pmod 4 \\0 &n \equiv 3 \pmod 4\end{matrix}\right.$$:::success[证明]31 行未改动- $C$ 集合的编号异或和 $c=S(n-2) \oplus 1 \oplus 2=(n-2) \oplus 1 \oplus 2 = n+1$。- 因此 $a=b=c$。取补图显然成立。::::边数为 $2 \times 2 +2(n-4)+2(n-4)=4n-12$。需补充最优性证明。## 2.3 $n \equiv 1 \pmod 4$依旧存在分组做法 $A=\{1\},B=\{2,3\},C=\{4,5,\cdots,n\}$,边数 $3n-7$,不是最优解。最优构造来自[这篇题解](https://www.luogu.com.cn/article/qdzgapy0)。取若干段区间 $A_i=[4i-6,4i-3]$,约定 $A_1=\{1\}$。对于区间 $A_i=\{4i-6,4i-5,4i-4,4i-3\}$ 内部连边 $(4i-6,4i-4),(4i-6,4i-3),(4i-5,4i-4),(4i-5,4i-3)$。对于 $A_1$,将 $i=2,3,\cdots,n$ 都连边 $(i,1)$。::::success[正确性]{open}- $f(1)=S(n) \oplus 1=1 \oplus 1 =0$。- $f(4i-5)=f(4i-6)=(4i-4) \oplus (4i-3) \oplus 1=(4i-4) \oplus (4i-4)=0$。- $f(4i-3)=f(4i-4)=(4i-6) \oplus (4i-5) \oplus 1=(4i-6) \oplus (4i-6)=0$。::::边数为 $2n-2$。需补充最优性证明。## 2.4 $n \equiv 0 \pmod 4$分为三组 $A=\{n\},B=\{\operatorname{lowbit}(n),n-\operatorname{lowbit}(n)\},C=V \backslash A \backslash B$。组内的点相互之间不连边,组与组之间两两连边。4 行未改动- $C$ 集合的编号异或和 $c=S(n) \oplus a \oplus b=S(n)=1$。- 因此 $a=b=c$。取补图显然成立。::::边数为 $3n-7$。需补充最优性证明。# 3. 代码123424 行未改动29303132333435363731 行未改动697071727374757677787980818283848586878889909192934 行未改动9899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160# 1. 题意对于**无向简单连通图** $G=(V,E)$,设24 行未改动S(n)=\bigoplus_{i=1}^n i=\left\{\begin{matrix}n & n \equiv 0 \pmod 4 \\1 & n \equiv 1 \pmod 4 \\n+1 & n \equiv 2 \pmod 4 \\0 &n \equiv 3 \pmod 4\end{matrix}\right.$$:::success[证明]31 行未改动- $C$ 集合的编号异或和 $c=S(n-2) \oplus 1 \oplus 2=(n-2) \oplus 1 \oplus 2 = n+1$。- 因此 $a=b=c$。取补图显然成立。::::边数为 $2 \times 2 +2(n-4)+2(n-4)=4n-12$。## 2.3 $n \equiv 1 \pmod 4$依旧存在分组做法 $A=\{1\},B=\{2,3\},C=\{4,5,\cdots,n\}$,边数 $1\times 2+1 \times (n-3)+2\times (n-3)=3n-7$,不是最优解。更优构造来自[这篇题解](https://www.luogu.com.cn/article/qdzgapy0)。取若干段区间 $A_i=[4i-6,4i-3]$,约定 $A_1=\{1\}$。对于区间 $A_i=\{4i-6,4i-5,4i-4,4i-3\}$ 内部连边 $(4i-6,4i-4),(4i-6,4i-3),(4i-5,4i-4),(4i-5,4i-3)$。对于 $A_1$,将 $i=2,3,\cdots,n$ 都连边 $(i,1)$。::::success[正确性]{open}- $f(1)=S(n) \oplus 1=1 \oplus 1 =0$。- $f(4i-5)=f(4i-6)=(4i-4) \oplus (4i-3) \oplus 1=(4i-4) \oplus (4i-4)=0$。- $f(4i-3)=f(4i-4)=(4i-6) \oplus (4i-5) \oplus 1=(4i-6) \oplus (4i-6)=0$。::::边数为 $n-1+4 \times \dfrac{n-1}{4}=2n-2$。## 2.4 $n \equiv 0 \pmod 4$分为三组 $A=\{n\},B=\{\operatorname{lowbit}(n),n-\operatorname{lowbit}(n)\},C=V \backslash A \backslash B$。组内的点相互之间不连边,组与组之间两两连边。4 行未改动- $C$ 集合的编号异或和 $c=S(n) \oplus a \oplus b=S(n)=1$。- 因此 $a=b=c$。取补图显然成立。::::边数为 $1\times 2+1 \times (n-3)+2\times (n-3)=3n-7$。# 3. 代码```cppconst int N = 105;int n;vector<pair<int, int>> vec;void A(int u, int v) {vec.emplace_back(u, v);}void E() {cout << vec.size() << '\n';for (const auto& e : vec) cout << e << '\n';}void P(vector<int> a, vector<int> b, vector<int> c) {for (int i : a) for (int j : b) A(i, j);for (int i : b) for (int j : c) A(i, j);for (int i : a) for (int j : c) A(i, j);}void _main() {cin >> n;if (n == 1) return cout << 0 << '\n', void();if ((1 << lg(n)) == n) return cout << -1 << '\n', void();vec.clear();switch (n & 3) {case 0: {vector<int> c;for (int i = 1; i < n; i++) {if (i != lowbit(n) && i != n - lowbit(n)) c.emplace_back(i);}P({n}, {lowbit(n), n - lowbit(n)}, c);break;}case 1: {// vector<int> c;// for (int i = 4; i <= n; i++) c.emplace_back(i);// P({1}, {2, 3}, c);for (int i = 2; i <= n; i += 4) A(i, i + 2), A(i, i + 3), A(i + 1, i + 2), A(i + 1, i + 3);for (int i = 2; i <= n; i++) A(1, i);break;}case 2: {vector<int> c;for (int i = 3; i < n - 1; i++) c.emplace_back(i);P({1, n}, {2, n - 1}, c);break;}case 3:for (int i = 1; i < n; i++) A(i, n);break;} E();}```# 4. 关于最优性我尝试证明以上构造使得边数最小化,或者给出更优构造,但都没有思路。求助各位大手子如何证明,或者给出更优方案。