文章目录
K. Between Us
题目链接:Between Us
题目大意:一个图,n个点,m条边。问最多将n个点分成两组,是否有方法让每一个点的同组相邻的点个数都是奇数。
数据范围:
1
≤
n
≤
100
,
1
≤
m
≤
n
×
(
n
−
1
)
/
2
1\le n \le 100,1\le m\le n×(n−1)/2
1≤n≤100,1≤m≤n×(n−1)/2
题解:我们考虑用
a
[
i
]
a[i]
a[i]来表示第
i
i
i个点的分组,即
a
[
i
]
=
1
a[i]=1
a[i]=1表示
i
i
i是第
1
1
1组的,
a
[
i
]
=
0
a[i]=0
a[i]=0表示
i
i
i是第
0
0
0组的。之后会有4种情况。
- i i i的度数为偶数, a [ i ] = 1 a[i]=1 a[i]=1,即 ⨁ ( i , t o ) ∈ E a [ t o ] = 1 \bigoplus\limits_{(i,to)\in E}a[to]=1 (i,to)∈E⨁a[to]=1
- i i i的度数为偶数, a [ i ] = 0 a[i]=0 a[i]=0,即 ⨁ ( i , t o ) ∈ E a [ t o ] = 1 \bigoplus\limits_{(i,to)\in E}a[to]=1 (i,to)∈E⨁a[to]=1
- i i i的度数为奇数, a [ i ] = 1 a[i]=1 a[i]=1,即 ⨁ ( i , t o ) ∈ E a [ t o ] = 1 \bigoplus\limits_{(i,to)\in E}a[to]=1 (i,to)∈E⨁a[to]=1
- i i i的度数为奇数, a [ i ] = 0 a[i]=0 a[i]=0,即 ⨁ ( i , t o ) ∈ E a [ t o ] = 0 \bigoplus\limits_{(i,to)\in E}a[to]=0 (i,to)∈E⨁a[to]=0
可以发现
i
i
i的度数为偶数时:
⨁
(
i
,
t
o
)
∈
E
a
[
t
o
]
=
1
\bigoplus\limits_{(i,to)\in E}a[to]=1
(i,to)∈E⨁a[to]=1,为奇数时:
⨁
(
i
,
t
o
)
∈
E
a
[
t
o
]
=
a
[
i
]
\bigoplus\limits_{(i,to)\in E}a[to]=a[i]
(i,to)∈E⨁a[to]=a[i]
我们可以由此建立n个异或方程式。然后使用高斯消元来判定方程是否存在解。
AC代码:
#include<bits/stdc++.h>
#define ld long double
#define ll long long
using namespace std;
template<class T>
void read(T& x)
{
T res = 0, f = 1; char c = getchar();
while (!isdigit(c)) {
if (c == '-')f = -1; c = getchar();
}
while (isdigit(c)) {
res = (res << 3) + (res << 1) + c - '0'; c = getchar();
}
x = res * f;
}
const ll N = 200 + 10;
const int mod = 1e9 + 7;
int a[N][N],n,m;
bool Guss()
{
int line = 1;
for (int i = 1; i <= n; i++)
{
int mx = -1;
for (int j = line; j <= n; j++)
{
if (a[j][i])
{
mx = j; break;
}
}
if (mx == -1)continue;
swap(a[mx], a[line]);
for (int j = 1; j <= n; j++)
{
if (j == line)continue;
if (a[j][i])
{
for (int k = i; k <= n + 1; k++)
{
a[j][k] ^= a[line][k];
}
}
}
line++;
}
for (int i =line; i <= n; i++)
{
if (a[i][n + 1])return 0;
}
return 1;
}
int du[N];
int main()
{
//ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif // ONLINE_JUDGE
read(n), read(m);
for (int i = 1; i <= m; i++)
{
int x, y; read(x), read(y);
a[x][y] = a[y][x] = 1; du[x]++, du[y] ++ ;
}
for (int i = 1; i <= n; i++)
{
if (du[i] % 2 == 0)
a[i][n + 1] = 1;
else
a[i][i] = 1;
}
printf("%s\n", Guss() ? "Y" : "N");
return 0;
}