[2020-2021 ACM-ICPC Brazil Subregional Programming Contest] K. Between Us (高斯消元解異或方程組)
阿新 • • 發佈:2020-12-16
[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\)
當且僅當滿足下式時劃分才合法。
\[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; }