[Bubble Cup 12 - Finals [Div. 1]] -D. Xor Spanning Tree (仙人掌图扣环,FWT)
思路:
首先用类似Tarjan算法的方法把图中的每一个环单独的取出来。
我们知道不在环中的边一定要取的,所以可以求出(base)代表不在环中的边的权值异或和。
然后每个环我们只需要删除一个边即可,也可以很简单的求出环中的删除一条边后剩余边权值的异或和。
设共有(num)个环,
那么问题就转化为,从(num+1)个集合中,每一集合选择一个数,将异或起来的权值最小为多少,有多少种方案数?
其中前(num)个集合都是环,最后一个集合只有一个元素为(base),那么这是一个经典的异或卷积(FWT)算法的应用。
同时我们注意到,题目要求输出方案数,且给定了一个模数,
因为卷积涉及到求和运算,那么在卷积的过程中会存在本来方案数大于0的取模后变为0,这样会影响我们对答案即异或得到的最小权值的求解。
所有我们用2个模数求分别(FWT),只要有一个得到的异或成(ans)的方案数不为0,即代表可以得到该值。(此处类似双hash的思想。)
代码:
#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), ' ', 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("
");}}}
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("
");}}}
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 ? '
' : ' ');}}
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 ? '
' : ' ');}}
const int maxn = (1 << 17);
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n, m;
struct edge
{
int f, t, val;
edge() {}
edge(int ff, int tt, int vv)
{
f = ff;
t = tt;
val = vv;
}
};
std::vector<edge> e[maxn];
int vis[maxn];
int loop = 0;
std::vector<int> c[maxn];
int far[maxn];
int far_w[maxn];
int cnt = 0;
int base = 0;
void dfs(int x, int pre)
{
vis[x] = ++cnt;
for (auto &y : e[x])
{
if (vis[y.t] == 0)
{
far[y.t] = x;
far_w[y.t] = y.val;
dfs(y.t, x);
} else if (vis[y.t] > vis[x])
{
int now = y.t;
loop++;
c[loop].pb(y.val);
base ^= y.val;
while (now != x)
{
c[loop].pb(far_w[now]);
base ^= far_w[now];
now = far[now];
}
}
}
}
ll a[maxn];
ll b[maxn];
void FWT_xor(ll *a, int N, int opt, ll MOD, ll inv2) // ll * a 根据题目来确定。
{
for (int i = 1; i < N; i <<= 1)
for (int p = i << 1, j = 0; j < N; j += p)
for (int k = 0; k < i; ++k)
{
int X = a[j + k], Y = a[i + j + k];
a[j + k] = (X + Y) % MOD; a[i + j + k] = (X + MOD - Y) % MOD;
if (opt == -1)a[j + k] = 1ll * a[j + k] * inv2 % MOD, a[i + j + k] = 1ll * a[i + j + k] * inv2 % MOD;
}
}
const ll MOD = 1000000007 ;
const ll inv2 = powmod(2, MOD - 2ll, MOD);
const ll mod2 = 998244353 ;
const ll inv_22 = powmod(2, mod2 - 2ll, mod2);
ll yeji[maxn];
ll d[maxn];
int main()
{
#if DEBUG_Switch
freopen("C:\code\input.txt", "r", stdin);
#endif
//freopen("C:\code\output.txt","w",stdout);
n = readint();
m = readint();
int tot = 0;
repd(i, 1, m)
{
int u = readint();
int v = readint();
int w = readint();
e[u].pb(edge(u, v, w));
e[v].pb(edge(v, u, w));
base ^= w;
}
dfs(1, 1);
a[base] = 1;
d[base] = 1;
FWT_xor(a, maxn, 1, MOD, inv2);
FWT_xor(d, maxn, 1, mod2, inv_22);
for (int i = 1; i <= loop; ++i)
{
int now = 0;
for (auto &y : c[i])
{
now ^= y;
}
MS0(b);
MS0(yeji);
for (auto &y : c[i])
{
b[now ^ y] ++;
yeji[now ^ y]++;
}
FWT_xor(b, maxn, 1, MOD, inv2);
FWT_xor(yeji, maxn, 1, mod2, inv_22);
repd(j, 0, maxn - 1)
{
a[j] = a[j] * b[j] % MOD ;
d[j] = d[j] * yeji[j] % mod2;
}
}
FWT_xor(a, maxn, -1, MOD, inv2);
FWT_xor(d, maxn, -1, mod2, inv_22);
for (int i = 0; i < maxn; ++i)
{
if (a[i] > 0 || d[i] > 0)
{
printf("%d %lld
", i, a[i] );
break;
}
}
return 0;
}