2020-2021 ACM-ICPC Brazil Subregional Programming Contest K Between Us

题目描述

https://codeforces.com/gym/102861/problem/K

Solution

我们令 $x_i=0/1$ 表示 $i$ 在哪个集合

如果 $i$ 的度数为奇数,则 $x_i~xor~x_a ~xor~ x_b~xor~ x_c\cdots=1$

如果 $i$ 的度数为偶数,则 $x_a ~xor~ x_b~xor~ x_c\cdots=0$

那么相当于我们求这个异或方程组是否有解,高消即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <cstdio>
#include <vector>
#define maxn 110
using namespace std;

int n, m, du[maxn];

vector<int> G[maxn];

int g[maxn][maxn];
void Gauss() {
for (int i = 1; i <= n; ++i) {
int p = -1;
for (int j = i; j <= n; ++j) if (g[j][i]) p = j;
if (p == -1) continue; swap(g[p], g[i]);
for (int j = 1; j <= n; ++j) {
if (i == j || !g[j][i]) continue;
for (int k = i; k <= n + 1; ++k) g[j][k] ^= g[i][k];
}
}
for (int i = 1; i <= n; ++i) {
int s = 0;
for (int j = i; j <= n; ++j) s |= g[i][j];
if (!s && g[i][n + 1]) return (void) (cout << "N\n");
}
cout << "Y\n";
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y; cin >> x >> y;
++du[x]; ++du[y];
G[x].push_back(y); G[y].push_back(x);
}
for (int i = 1; i <= n; ++i)
if (du[i] % 2 == 0) {
for (auto u : G[i]) g[i][u] = 1;
g[i][n + 1] = 1;
}
else {
for (auto u : G[i]) g[i][u] = 1;
g[i][i] = 1; g[i][n + 1] = 0;
}
Gauss();
return 0;
}