CF724G. Xor-matic Number of the Graph

The meaning of problems

http://codeforces.com/contest/724/problem/G
defined triplet \ ((U, V, S) (U <V) \) : \ (U \) to \ (V \) path \ (XOR \) and as \ (S \) (may not be a simple path), \ ((U, V, S) \) different iff \ (u, v, s \ ) a different one of the parameters.
All the different requirements of triplets \ (S \) and values.
\ (n \ in [1,10 ^ 5], m \ in [0, 2 \ cdot 10 ^ 5], w \ in [0,10 ^ {18}] \)

answer

Unicom apparently with a block, this block Unicom all rings are to be taken. And each path is a simple path with a certain number of rings \ (XOR \) and composition.
Linear portion of the loop may be utilized to count the group may be composed of different \ (XOR \) value of the number.
\ (DFS \) a search tree, and prefix trees do, then a simple path \ (XOR \) and is \ (S [U] \ Oplus S [V] \) , and all the way to the processing loop \ (XOR \) and thrown in a linear group.
Since \ (xor \) to consider so you do not affect each bit processing.
For the first \ (K \) bits, the \ (S \) partitioned into a first \ (K \) bit \ (0 \) and to \ (1 \) , the number referred to as \ (C_0 \) and \ ( c_1 \)
can be divided into \ (3 \) cases:

  • \ (0 \ Oplus 0 \) , co \ (\ frac {c_0 \ times (c_0-1)} {2} \) species.
  • \ (. 1 \ Oplus. 1 \) , co \ (\ frac {c_1 \ times (c_1-1)} {2} \) species.
  • \ (1 \ oplus 0 \) , a total of \ (c_0 \ times c_1 \) species.

At the same time also with a first linear group \ (K \) bit \ (0 \) and \ (K \) bit \ (1 \) in both cases.
Several cases were combined look like.
Group is a linear set \ (B \) . There

  • \ (b [k] = 0 \) when, in the case of three different contributions \ (XOR \) and to \ (2 ^ {| b | } \) species. In the case of a situation and two, contribute to the \ (0 \) .
  • \ (b [k] = 1 \) when, in the case of three different contributions \ (XOR \) and to \ (2 ^ {| b | -1} \) species. In the case where two and a contribution to \ (2 ^ {| b | -1} \) species.

Complexity \ (O (n \ log ^ 2 \ max (w_i)) \)

#include <bits/stdc++.h>
using namespace std;

namespace io {
char buf[1<<21], *p1 = buf, *p2 = buf, buf1[1<<21];
inline char gc() {
    if(p1 != p2) return *p1++;
    p1 = buf;
    p2 = p1 + fread(buf, 1, 1 << 21, stdin);
    return p1 == p2 ? EOF : *p1++;
}
#define G gc

#ifndef ONLINE_JUDGE
#undef G
#define G getchar
#endif

template<class I>
inline void read(I &x) {
    x = 0; I f = 1; char c = G();
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = G(); }
    while(c >= '0' && c <= '9') {x = x * 10LL + c - '0'; c = G(); }
    x *= f;
}

template<class I>
inline void write(I x) {
    if(x == 0) {putchar('0'); return;}
    I tmp = x > 0 ? x : -x;
    if(x < 0) putchar('-');
    int cnt = 0;
    while(tmp > 0) {
        buf1[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while(cnt > 0) putchar(buf1[--cnt]);
}

#define in(x) read(x)
#define outn(x) write(x), putchar('\n')
#define out(x) write(x), putchar(' ')

} using namespace io;


#define ll long long
const int N = 100010;
const ll mod = 1e9 + 7;

int cnt = 1, head[N];
struct edge {int to, nxt; ll v;} e[N<<2];
int n, m, tot;
ll s[N], b[N], ans, pw[N];
bool vis[N];

struct lb {
    
    ll p[64];
    vector<ll> a;
    
    void clear() {
        a.clear();
        memset(p, 0, sizeof(p));
    }
    
    void insert(ll x) {
        for(ll i = 63; i >= 0; --i) 
            if((x >> i) & 1LL) {
                if(p[i]) x ^= p[i];
                else {
                    p[i] = x;
                    return;
                }
            }
    }
    
    void init() {
        for(int i = 0; i <= 63; ++i) {
            if(p[i])
            for(int j = i - 1; j >= 0; --j) {
                if((p[j] >> i) & 1) p[i] ^= p[j];
            }
        }
        for(int i = 0; i <= 63; ++i) 
            if(p[i]) a.push_back(p[i]);
    }
    
    ll solve(ll k, ll c, ll v) {
        bool flag = 0; v %= mod;
        for(int i = 0; i < (int)a.size(); ++i) {
            if((a[i] >> k) & 1) flag = 1;
        }
        if(c) {
            if(flag) return 1LL * pw[(int)a.size() - 1] * v % mod;
            return 1LL * pw[(int)a.size()] * v % mod;
        } else {
            if(flag) return 1LL * pw[(int)a.size() - 1] * v % mod;
            return 0;
        }
    }
    
}B;

void ins(int u, int v, ll w) {
    e[++cnt] = {v, head[u], w};
    head[u] = cnt;
}

void dfs(int u, int in_edge) {
    vis[u] = 1;
    b[++tot] = s[u];
    for(int i = head[u]; i; i = e[i].nxt) {
        int v = e[i].to;
        if(in_edge == (i ^ 1)) continue;
        if(vis[v]) {
            B.insert(s[u] ^ s[v] ^ e[i].v);
            continue;
        }
        s[v] = s[u] ^ e[i].v;
        dfs(v, i);
    }
}

ll power(ll x, ll y) { ll ans = 1;
    while(y) {
        if(y & 1) ans = ans * x % mod;
        x = x * x % mod; y >>= 1;
    } return ans;
}
ll inv2 = power(2, mod - 2);

void solve() {
    ll c[2] = {0, 0};
    for(ll k = 0; k < 64; ++k) {
        c[0] = c[1] = 0;
        for(int i = 1; i <= tot; ++i) c[(b[i] >> k) & 1LL]++;
        if(c[0]) (ans += B.solve(k, 0, (1LL << k) % mod * c[0] % mod * (c[0] - 1LL) % mod) * inv2 % mod) %= mod;
        if(c[1]) (ans += B.solve(k, 0, (1LL << k) % mod * c[1] % mod * (c[1] - 1LL) % mod) * inv2 % mod) %= mod;
        (ans += B.solve(k, 1, (1LL << k) % mod * c[0] % mod * c[1] % mod)) %= mod;
    }
}

int main() {
    read(n); read(m);
    for(int u, v, i = 1; i <= m; ++i) {
        ll w;
        read(u); read(v); read(w);
        ins(u, v, w), ins(v, u, w);
    }
    pw[0] = 1;
    for(int i = 1; i <= n; ++i) pw[i] = pw[i - 1] * 2LL % mod;
    for(int i = 1; i <= n; ++i) {
        if(!vis[i]) {
            B.clear(); tot = 0; 
            dfs(i, 0);
            B.init();
            solve();
        }
    }
    outn((ans%mod+mod)%mod);
}
/*
10 20
1 2 0
4 3 3
6 8 7
10 1 4
3 8 0
10 7 0
9 7 9
7 1 10
6 7 2
8 5 10
4 5 7
10 4 2
6 9 10
6 10 10
10 5 5
4 1 4
9 8 0
2 3 7
4 7 4
3 1 7
*/

Guess you like

Origin www.cnblogs.com/henry-1202/p/11429038.html