[2020-2021 ACM-ICPC Brazil Subregional Programming Contest] K. Between Us (高斯消元解异或方程组)

[2020-2021 ACM-ICPC Brazil Subregional Programming Contest] K. Between Us (高斯消元解异或方程组)

题面:

题意:

给定一个含有\(\mathit n\)个节点\(\mathit m\)个边的无向图,是你是否可以将点集划分为两个部分,原图的边的2个节点都在一个部分中的话继续保留此边。问是否存在某种划分使每一个节点都有奇数个出边。

思路:

我们设点集的划分状态为:\(x_1,x_2,x_3,\dots,x_n\),其中\(x_i=0\or1\)分别代表点\(\mathit i\)被划分到第1个集合和第2个集合中。

对于每个节点\(\mathit i\),如果原图中节点\(\mathit i\)有偶数个出边,分别连向\(u_1,u_2,\dots,u_x\)则:

​ 当且仅当满足下式时划分才合法。

\[x_{u_1}\oplus x_{u_2}\oplus x_{u_3}\oplus \dots\oplus x_{u_x}=1 \]

如果原图中节点\(\mathit i\)有奇数个出边,分别连向\(u_1,u_2,\dots,u_x\)则:

​ 当且仅当满足下式时划分才合法。

\[x_{u_1}\oplus x_{u_2}\oplus x_{u_3}\oplus \dots\oplus x_{u_x}=x_i \]

即:

\[x_i\oplus x_{u_1}\oplus x_{u_2}\oplus x_{u_3}\oplus \dots\oplus x_{u_x}=0 \]

可以发现这是一个含有\(\mathit n\)个变量的异或方程组,我们只需要利用高斯消元算法判断其是否有解即可解决本问题。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
// const int maxn = 1000010;
// const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/

const int maxn = 150;
int m, n, equ, var, a[maxn][maxn],  free_xx[maxn];
//
// equ 表示方程个数
// var 表示变量个数
// a[i] 表示第i个方程,i -> [0,equ-1]
// a[i][j]=1,表示第i个方程中有第j个变量 j -> [0,var-1]
// a[i][var] 表示第i个方程的异或结果,(0 or 1)
// 
int Gauss()
{
    int r = 0, cnt = 0; //cnt表示自由变元个数
    for (int c = 0; r < equ && c < var; ++r, ++c) {
        int Maxr = r;
        for (int i = r + 1; i < equ; ++i)
            if (abs(a[i][c]) > abs(a[Maxr][c])) {
                Maxr = i;
            }
        if (Maxr != r) {
            for (int i = c; i < var + 1; ++i) {
                swap(a[Maxr][i], a[r][i]);
            }
        }
        if (!a[r][c]) {
            --r;
            free_xx[cnt++] = c;
            continue;
        }
        for (int i = r + 1; i < equ; ++i) {
            if (!a[i][c]) { continue; }
            for (int j = c; j < var + 1; ++j) {
                a[i][j] ^= a[r][j];
            }
        }
    }
    for (int i = r; i < equ; ++i)
        if (a[i][var]) {
            return -1;    //无解
        }
    return var - r;       //返回自由变元的个数,cnt=var-r
}
std::vector<int> v[maxn];
int main()
{
    n = readint();
    m = readint();
    repd(i, 1, m) {
        int x = readint(), y = readint();
        v[x].pb(y);
        v[y].pb(x);
    }
    var = n;
    repd(i, 1, n) {
        if (sz(v[i]) & 1) {
            for (auto x : v[i]) {
                a[equ][x - 1] = 1;
            }
            a[equ][i - 1] = 1;
            equ++;
        } else {
            for (auto x : v[i]) {
                a[equ][x - 1] = 1;
            }
            a[equ][n] = 1;
            equ++;
        }
    }
    if (Gauss() == -1) {
        printf("N\n");
    } else {
        printf("Y\n");
    }
    return 0;
}


posted @ 2020-12-16 20:25  茄子Min  阅读(240)  评论(0编辑  收藏  举报