K. Between Us

文章目录

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 1n100,1mn×(n1)/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种情况。

  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)Ea[to]=1
  2. 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)Ea[to]=1
  3. 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)Ea[to]=1
  4. 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)Ea[to]=0

可以发现 i i i的度数为偶数时: ⨁ ( i , t o ) ∈ E a [ t o ] = 1 \bigoplus\limits_{(i,to)\in E}a[to]=1 (i,to)Ea[to]=1,为奇数时: ⨁ ( i , t o ) ∈ E a [ t o ] = a [ i ] \bigoplus\limits_{(i,to)\in E}a[to]=a[i] (i,to)Ea[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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值