Problem:
K - XOR回廊
問題文
KU大学では一風変わったラリーゲームが人気である．
このゲームの舞台であるサーキットには，
N
個の休憩所と
M
個の道があり，
i
番目の道は
f
i
番目の休憩所と
t
i
番目の休憩所の間にある．
すべての道にはチェックポイントが一つずつ存在し，道
i
のチェックポイントを通過するとどちらの向きで通っても
p
i
の得点がXORで加算される．
XORで
加算される．大事なことなので2回言いました．
このゲームでは，同じ休憩所や道を何度通っても良いし，
ゴールにたどり着いてもそのままラリーを続行することができるが，
道の途中にあるチェックポイントを迂回して別の休憩所へ行くことは禁止されている．
そのため，どのような道順をたどれば高い得点を取ることができるかを頑張って考えなければならない点が人気のある所以である．
今回は
Q
人の参加者がいて，
j
番目の参加者は
a
j
の休憩所からスタートして，ゴールである
b
j
の休憩所まで行くことになっているようだ．
運営側の人間としてそれぞれの参加者の最高得点を前もって調べておくことにした．
入力形式
入力は以下の形式で与えられる．なお休憩所の番号は0-indexedである．
N
M
Q
f
1
t
1
p
1
...
f
M
t
M
p
M
a
1
b
1
...
a
Q
b
Q
出力形式
Q
行出力し，
j
行目には
a
j
番目の休憩所から
b
j
番目の休憩所への経路で得られる得点の最大値を出力せよ．
制約
1 ≤ N ≤ 10
5
0 ≤ M ≤ 2 × 10
5
1 ≤ Q ≤ 10
5
0 ≤ f
i
, t
i
< N
,
f
i
≠ t
i
0 ≤ p
i
< 2
60
0 ≤ a
j
, b
j
< N
どの休憩所からでも，道を辿っていけば任意の休憩所へたどり着ける．
休憩所
f
i
,t
i
間を結ぶ道は高々一つしか存在しない．
入力値はすべて整数である．
この問題の判定には，60 点分のテストケースのグループが設定されている．このグループに含まれるテストケースは上記の制約に加えて下記の制約も満たす．
M ≤ 20
入出力例
入力例1
5 5 3
0 1 7
1 2 22
2 3 128
3 4 128
4 2 128
0 1
0 0
3 4
出力例1
135
128
128
Writer : 森槙悟
Tester : 田村和範

Submitted solutions:

code_snippet: // AOJ #2416 XOR Cloister
// 2025.3.17

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

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n = 0; int c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

ll Cinll() {  // 整数の入力
	ll n = 0; int c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(ll n) { // 整数の表示（出力）
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

struct Edge {
    int from, to;
    ll cost;
    Edge(int f, int t, ll c) : from(f), to(t), cost(c) {}
};

using Edges = vector<Edge>;
using Graph = vector<Edges>;

vector<ll> cycleXor;

int visited[100000];
ll  xorDist[100000];

ll top(ll value) { return 1LL << (63 - __builtin_clzll(value)); }

void insertIntoBasis(vector<ll>& basis, ll a) {
    for (ll &b : basis) if (top(b) & a) a ^= b;
    if (a == 0) return;
    ll t = top(a);
    for (ll &b : basis) if (t & b) b ^= a;
    basis.push_back(a);
}

vector<ll> buildBasis(vector<ll>& nums) {
    vector<ll> basis;
    for (ll x : nums) insertIntoBasis(basis, x);
    sort(basis.rbegin(), basis.rend());
    return basis;
}

void dfs(int v, const Graph &G) {
    visited[v] = 1;
    for (const Edge &edge : G[v]) {
        int next = edge.to;
        if (visited[next]) cycleXor.push_back(xorDist[v] ^ xorDist[next] ^ edge.cost);
        else {
            xorDist[next] = xorDist[v] ^ edge.cost;
            dfs(next, G);
        }
    }
}

int main(){
    int N = Cin(), M = Cin(), Q = Cin();

    Graph G(N);
    for (int i = 0; i < M; i++){
        int f = Cin(), t = Cin();
        ll p = Cinll();
        G[f].emplace_back(f, t, p);
        G[t].emplace_back(t, f, p);
    }

    dfs(0, G);

    vector<ll> basis = buildBasis(cycleXor);

    while (Q--){
        int a = Cin(), b = Cin();
        ll ans = xorDist[a] ^ xorDist[b];
        for (ll b_val : basis) {
            if ((ans ^ b_val) > ans) ans ^= b_val;
        }
        Cout(ans);
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 22340
final_rank: 6

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<pair<int, ll>>> g(n);
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        ll z;
        cin >> z;
        g[x].push_back({y, z});
        g[y].push_back({x, z});
    }
    vector<ll> memo(n);
    vector<int> used(n);
    used[0] = true;
    vector<ll> val;
    auto dfs = [&](auto dfs, int s) -> void {
        for (auto to : g[s]) {
            int t = to.first;
            if (!used[t]) {
                used[t] = true;
                memo[t] = memo[s] ^ to.second;
                dfs(dfs, t);
            } else {
                val.push_back(memo[s] ^ memo[t] ^ to.second);
            }
        }
    };
    dfs(dfs, 0);
    vector<ll> basis;
    for (ll x : val) {
        for (ll y : basis) {
            x = min(x, y ^ x);
        }
        if (x) {
            basis.push_back(x);
        }
    }
    for (ll &x : basis) {
        for (ll y : basis) {
            if (x == y) continue;
            x = min(x, x ^ y);
        }
    }
    while (q--) {
        int x, y;
        cin >> x >> y;
        ll res = memo[x] ^ memo[y];
        for (ll x : basis) {
            res = max(res, res ^ x);
        }
        cout << res << "\n";
    }
}
accuracy: 1
time_ms: 70
memory_kb: 25388
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

long long N;
long long M, S[200009], T[200009], C[200009];
long long Q, A[200009], B[200009];
long long Depth[200009];
vector<pair<int, long long>> G[200009];
vector<long long> XOR_Cand;

// Gauss Jordan
vector<long long> Gauss(vector<long long> Z) {
	for (int i = 59; i >= 0; i--) {
		int pivot = -1;
		for (int j = 0; j < (int)Z.size(); j++) {
			if (Z[j] >= (2LL << i)) continue;
			if (Z[j] >= (1LL << i)) { pivot = j; break; }
		}
		if (pivot == -1) continue;
		for (int j = 0; j < (int)Z.size(); j++) {
			if (j == pivot) continue;
			if ((Z[j] / (1LL << i)) % 2LL == 0LL) continue;
			Z[j] ^= Z[pivot];
		}
	}
	sort(Z.begin(), Z.end());
	Z.erase(unique(Z.begin(), Z.end()), Z.end());
	reverse(Z.begin(), Z.end());
	return Z;
}

// DFS and Calculate Depth
void dfs(int pos, long long cur) {
	Depth[pos] = cur;
	for (int i = 0; i < (int)G[pos].size(); i++) {
		int to = G[pos][i].first;
		long long cost = G[pos][i].second;
		if (Depth[to] != -1) continue;
		dfs(to, (cur ^ cost));
	}
}

// Answer query is "BASE XOR" is val
long long query(long long val) {
	for (int i = 0; i < (int)XOR_Cand.size(); i++) {
		long long cand_val = (val ^ XOR_Cand[i]);
		if (val < cand_val) val = cand_val;
	}
	return val;
}

int main() {
	// Input
	cin >> N >> M >> Q;
	for (int i = 0; i < M; i++) {
		cin >> S[i] >> T[i] >> C[i];
		G[S[i]].push_back(make_pair(T[i], C[i]));
		G[T[i]].push_back(make_pair(S[i], C[i]));
	}
	for (int i = 0; i < Q; i++) cin >> A[i] >> B[i];
	
	// DFS
	for (int i = 0; i < N; i++) Depth[i] = -1;
	dfs(0, 0);
	
	// Get XOR
	for (int i = 0; i < M; i++) {
		long long tmp = ((Depth[S[i]] ^ Depth[T[i]]) ^ C[i]);
		XOR_Cand.push_back(tmp);
	}
	XOR_Cand = Gauss(XOR_Cand);
	
	// Query
	for (int i = 0; i < Q; i++) {
		long long tmp = (Depth[A[i]] ^ Depth[B[i]]);
		cout << query(tmp) << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 310
memory_kb: 27520
final_rank: 14

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  if(x == 0) return 0;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;
  BIT(){}
  BIT(int n){
    N = n;
    node.resize(N+10);
  }
  void build(int n) {
    N = n;
    node.resize(N+10);
  }
  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash{

    using ull = unsigned long long;
    const ull mod = (1ULL << 61) - 1;
    const ull MASK30 = (1ULL << 30) - 1;
    const ull MASK31 = (1ULL << 31) - 1;

    const ull MASK61 = mod;

    ull base;
    int n;
    vector<ull> hash, pow;

    RollingHash(const string &s)
    {
        random_device rnd;
        mt19937_64 mt(rnd());
        base = 1001;
        
        n = (int)s.size();
        hash.assign(n+1, 0);
        pow.assign(n+1, 1);
        
        for(int i=0; i<n; i++){
            hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);
            pow[i+1] = calc_mod(mul(pow[i], base));
        }
    }

    ull calc_mod(ull x){
        ull xu = x >> 61;
        ull xd = x & MASK61;
        ull res = xu + xd;
        if(res >= mod) res -= mod;
        return res;
    }

    ull mul(ull a, ull b){
        ull au = a >> 31;
        ull ad = a & MASK31;
        ull bu = b >> 31;
        ull bd = b & MASK31;
        ull mid = ad * bu + au * bd;
        ull midu = mid >> 30;
        ull midd = mid & MASK30;
        return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);
    }

    //[l,r)のハッシュ値
    inline ull get(int l, int r){
        ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));
        return res;
    }
    //string tのハッシュ値
    inline ull get(string t){
        ull res = 0;
        for(int i=0; i<t.size(); i++){
            res = calc_mod(mul(res, base)+t[i]);
        }
        return res;
    }
    //string s中のtの数をカウント
    inline int count(string t) {
        if(t.size() > n) return 0;
        auto hs = get(t);
        int res = 0;
        for(int i=0; i<n-t.size()+1; i++){
            if(get(i, i+t.size()) == hs) res++; 
        }
        return res;
    }

    /* 
        concat 
        @verify https://codeforces.com/problemset/problem/514/C
    */
    inline ull concat(ull h1, ull h2, int h2len){
      return calc_mod(h2 + mul(h1, pow[h2len]));
    }

    // LCPを求める S[a:] T[b:]
    inline int LCP(int a, int b){
        int len = min((int)hash.size()-a, (int)hash.size()-b);
        
        int lb = -1, ub = len;
        while(ub-lb>1){
            int mid = (lb+ub)/2;

            if(get(a, a+mid) == get(b, b+mid)) lb = mid;
            else ub = mid;
        }
        return lb;
    }
};
vector<vector<pair<ll,ll>>> g(101010);
bool visited[101010];
ll memo[101010];
int depth[101010];
vector<ll> c;
void dfs(int v, int p) {
  visited[v] = true;
  for(auto [u, w]: g[v]) {
    if(!visited[u]) {
      depth[u] = depth[v] + 1;
      memo[u] = memo[v] ^ w;
      dfs(u, v);
    }else if(depth[u] < depth[v] && u != p){
      c.push_back(memo[u]^memo[v]^w);
    }
  }
}
int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  
  ll n, m, q; cin >> n >> m >> q;

  REP(i,m) {
    ll f, t, p; cin >> f >> t >> p;
    g[f].push_back({t, p});
    g[t].push_back({f, p});
  }

  dfs(0, -1);
  vector<ll> basis;
  for(auto e: c) {
    ll a = e;
    for(auto u: basis) {
      a = min(a, a^u);
    }
    if(a != 0) basis.push_back(a);
  }
  REP(i,q) {
    int a, b; cin >> a >> b;
    ll ans = memo[a] ^ memo[b];
    for(int j=0; j<basis.size(); j++) {
      ans = max(ans, ans^basis[j]);
    }
    cout << ans << '\n';
  }
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 18256
final_rank: 2

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename CostType>
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &x) const {
    return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src;
  }
  inline bool operator<=(const Edge &x) const { return !(x < *this); }
  inline bool operator>(const Edge &x) const { return x < *this; }
  inline bool operator>=(const Edge &x) const { return !(*this < x); }
};

template <int D>
struct Basis {
  std::vector<std::bitset<D>> v;
  std::vector<int> msb;

  bool add(std::bitset<D> val) {
    int n = v.size();
    if (n == D) return false;
    for (int i = 0; i < n; ++i) {
      if (val[msb[i]]) val ^= v[i];
    }
    if (val.none()) return false;
    int m = D - 1;
    while (!val[m]) --m;
    if (v.empty()) {
      v.emplace_back(val);
      msb.emplace_back(m);
      return true;
    }
    int idx = n - std::distance(msb.rbegin(), std::upper_bound(msb.rbegin(), msb.rend(), m));
    v.emplace(v.begin() + idx, val);
    msb.emplace(msb.begin() + idx, m);
    for (int i = idx + 1; i <= n; ++i) {
      if (v[idx][msb[i]]) v[idx] ^= v[i];
    }
    for (int i = idx - 1; i >= 0; --i) {
      if (v[i][m]) v[i] ^= v[idx];
    }
    return true;
  }

  int rank() const { return v.size(); }

  inline bool operator<(const Basis &x) const {
    int n = v.size();
    if (n != x.v.size()) return n < x.v.size();
    if (n == D) return false;
    for (int i = 0; i < n; ++i) {
      if (msb[i] != x.msb[i]) return msb[i] < x.msb[i];
    }
    for (int i = 0; i < n; ++i) {
      if (v[i] != x.v[i]) {
        for (int j = msb[i] - 1; ; --j) {
          if (v[i][j] != x.v[i][j]) return v[i][j] < x.v[i][j];
        }
      }
    }
    return false;
  }
};

int main() {
  constexpr int D = 60;
  int n, m, q;
  std::cin >> n >> m >> q;
  std::vector<std::vector<Edge<long long>>> graph(n);
  while (m--) {
    int f, t;
    long long p;
    std::cin >> f >> t >> p;
    graph[f].emplace_back(f, t, p);
    graph[t].emplace_back(t, f, p);
  }
  std::vector<long long> x(n, -1);
  x[0] = 0;
  Basis<D> basis;
  std::function<void(int, int)> dfs = [&graph, &x, &basis, &dfs](int par, int ver) {
    for (const Edge<long long> &e : graph[ver]) {
      if (e.dst != par) {
        if (x[e.dst] == -1) {
          x[e.dst] = x[ver] ^ e.cost;
          dfs(ver, e.dst);
        } else {
          basis.add(x[ver] ^ x[e.dst] ^ e.cost);
        }
      }
    }
  };
  dfs(-1, 0);
  while (q--) {
    int a, b;
    std::cin >> a >> b;
    std::bitset<D> ans(x[a] ^ x[b]);
    for (int i = 0; i < basis.v.size(); ++i) {
      if (!ans[basis.msb[i]]) ans ^= basis.v[i];
    }
    std::cout << ans.to_ulong() << '\n';
  }
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 23736
final_rank: 10

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

using CostType = ll;
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &rhs) const {
    return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
  }
  inline bool operator<=(const Edge &rhs) const { return !(rhs < *this); }
  inline bool operator>(const Edge &rhs) const { return rhs < *this; }
  inline bool operator>=(const Edge &rhs) const { return !(*this < rhs); }
};

template <int D>
struct Basis {
  vector<bitset<D> > v;
  vector<int> msb;

  bool add(bitset<D> val) {
    int n = v.size();
    if (n == D) return false;
    REP(i, n) {
      if (val[msb[i]]) val ^= v[i];
    }
    if (val.none()) return false;
    int m = D - 1;
    while (!val[m]) --m;
    if (v.empty()) {
      v.emplace_back(val);
      msb.emplace_back(m);
      return true;
    }
    int idx = n - distance(msb.rbegin(), upper_bound(msb.rbegin(), msb.rend(), m));
    v.emplace(v.begin() + idx, val);
    msb.emplace(msb.begin() + idx, m);
    for (int i = n; i > idx; --i) {
      if (v[idx][msb[i]]) v[idx] ^= v[i];
    }
    for (int i = idx - 1; i >= 0; --i) {
      if (v[i][m]) v[i] ^= v[idx];
    }
    return true;
  }

  bool add(ll val) { return add(bitset<D>(val)); }

  int rank() const { return v.size(); }

  inline bool operator<(const Basis &x) const {
    int n = v.size();
    if (n != x.v.size()) return n < x.v.size();
    if (n == D) return false;
    REP(i, n) {
      if (msb[i] != x.msb[i]) return msb[i] < x.msb[i];
    }
    REP(i, n) {
      if (v[i] != x.v[i]) {
        for (int j = msb[i] - 1; ; --j) {
          if (v[i][j] != x.v[i][j]) return v[i][j] < x.v[i][j];
        }
      }
    }
    return false;
  }
};

int main() {
  const int D = 60;
  int n, m, q; cin >> n >> m >> q;
  vector<vector<Edge> > graph(n);
  while (m--) {
    int f, t; ll p; cin >> f >> t >> p;
    graph[f].emplace_back(f, t, p);
    graph[t].emplace_back(t, f, p);
  }
  vector<ll> x(n, -1);
  x[0] = 0;
  Basis<D> basis;
  function<void(int, int)> dfs = [&](int par, int ver) {
    for (const Edge &e : graph[ver]) {
      if (e.dst == par) continue;
      if (x[e.dst] == -1) {
        x[e.dst] = x[ver] ^ e.cost;
        dfs(ver, e.dst);
      } else {
        basis.add(x[ver] ^ x[e.dst] ^ e.cost);
      }
    }
  };
  dfs(-1, 0);
  while (q--) {
    int a, b; cin >> a >> b;
    bitset<D> ans(x[a] ^ x[b]);
    REP(i, basis.v.size()) {
      if (!ans[basis.msb[i]]) ans ^= basis.v[i];
    }
    cout << ans.to_ulong() << '\n';
  }
  return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 25288
final_rank: 12

code_snippet: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int v; ll w; };
 
ll MOD = 1000000007;
ll _MOD = 1000000009;
ll INF = LLONG_MAX / 3;
double EPS = 1e-10;

void dfs(int u, vector<vector<edge> >& G, vector<ll>& a) {
	for (int i = 0; i < G[u].size(); i++) {
		edge e = G[u][i];
		if (a[e.v] != -1) continue;
		a[e.v] = a[u] ^ e.w;
		dfs(e.v, G, a);
	}
}

int main() {
	int N, M, Q; cin >> N >> M >> Q;
	vector<vector<edge> > G(N);
	while (M--) {
		int x, y; ll w; scanf("%d%d%lld", &x, &y, &w);
		edge e = {y, w};
		G[x].push_back(e);
		edge _e = {x, w};
		G[y].push_back(_e);
	}
	vector<ll> a(N, -1);
	a[0] = 0;
	dfs(0, G, a);
	vector<ll> b;
	for (int u = 0; u < N; u++)
		for (int i = 0; i < G[u].size(); i++) {
			edge e = G[u][i];
			b.push_back(a[u] ^ a[e.v] ^ e.w);
		}
	int n = b.size(), k = 0;
	for (ll x = 1LL<<60; x; x >>= 1)
		for (int i = k; i < n; i++)
			if (b[i] & x) {
				swap(b[k], b[i]);
				for (int i = k + 1; i < n; i++)
					if (b[i] & x)
						b[i] ^= b[k];
				k++;
				break;
			}
	while (Q--) {
		int x, y; scanf("%d%d", &x, &y);
		ll z = a[x] ^ a[y];
		for (int i = 0; i < k; i++)
			if (z < (z ^ b[i]))
				z ^= b[i];
		printf("%lld\n", z);
	}
}
accuracy: 1
time_ms: 140
memory_kb: 18488
final_rank: 3

code_snippet: #include<stdio.h>
#include<algorithm>
#include<vector>
#define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
 
#define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);

using namespace std;
vector<pair<int,long long> >g[210000];
long long val[410000];
long long kt[410000];
int v[410000];
int sz;
void dfs(int a,int b){
	v[a]=1;
	for(int i=0;i<g[a].size();i++){
		if(g[a][i].first==b)continue;
		if(!v[g[a][i].first]){
			val[g[a][i].first]=(val[a]^g[a][i].second);
			dfs(g[a][i].first,a);
		}else{
			kt[sz++]=(val[a]^val[g[a][i].first]^g[a][i].second);
		}
	}
}
int main(){
	BEGIN_STACK_EXTEND(120*1024*1024);
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	for(int i=0;i<b;i++){
		int p,q;
		long long r;
		scanf("%d%d%lld",&p,&q,&r);
		g[p].push_back(make_pair(q,r));
		g[q].push_back(make_pair(p,r));
	}
	sz=0;
	dfs(0,-1);
	int now=0;
	for(int i=59;i>=0;i--){
		int at=-1;
		for(int j=now;j<sz;j++){
			if(kt[j]&(1LL<<i)){
				at=j;break;
			}
		}
		if(!~at)continue;
		if(at!=now)swap(kt[at],kt[now]);
		for(int j=0;j<sz;j++){
			if(now==j)continue;
			if(kt[j]&(1LL<<i))kt[j]^=kt[now];
		}
		now++;
	}
	//for(int i=0;i<a;i++)printf("%lld\n",val[i]);
	//for(int i=0;i<now;i++)printf("%lld\n",kt[i]);
	for(int i=0;i<c;i++){
		int p,q;scanf("%d%d",&p,&q);
		long long ret=(val[p]^val[q]);
		for(int j=0;j<now;j++){
			if(ret<(ret^kt[j]))ret^=kt[j];
		}
		printf("%lld\n",ret);
	}
	END_STACK_EXTEND;
}
accuracy: 1
time_ms: 170
memory_kb: 21300
final_rank: 9

code_snippet: #include<stdio.h>
#include<algorithm>
#include<vector>
#define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
 
#define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);

using namespace std;
vector<pair<int,long long> >g[110000];
long long val[110000];
long long kt[110000];
int v[110000];
int sz;
void dfs(int a,int b){
	v[a]=1;
	for(int i=0;i<g[a].size();i++){
		if(g[a][i].first==b)continue;
		if(!v[g[a][i].first]){
			val[g[a][i].first]=(val[a]^g[a][i].second);
			dfs(g[a][i].first,a);
		}else{
			kt[sz++]=(val[a]^val[g[a][i].first]^g[a][i].second);
		}
	}
}
int main(){
	BEGIN_STACK_EXTEND(60*1024*1024);
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	for(int i=0;i<b;i++){
		int p,q;
		long long r;
		scanf("%d%d%lld",&p,&q,&r);
		g[p].push_back(make_pair(q,r));
		g[q].push_back(make_pair(p,r));
	}
	sz=0;
	dfs(0,-1);
	int now=0;
	for(int i=59;i>=0;i--){
		int at=-1;
		for(int j=now;j<sz;j++){
			if(kt[j]&(1LL<<i)){
				at=j;break;
			}
		}
		if(!~at)continue;
		if(at!=now)swap(kt[at],kt[now]);
		for(int j=0;j<sz;j++){
			if(now==j)continue;
			if(kt[j]&(1LL<<i))kt[j]^=kt[now];
		}
		now++;
	}
	//for(int i=0;i<a;i++)printf("%lld\n",val[i]);
	//for(int i=0;i<now;i++)printf("%lld\n",kt[i]);
	for(int i=0;i<c;i++){
		int p,q;scanf("%d%d",&p,&q);
		long long ret=(val[p]^val[q]);
		for(int j=0;j<now;j++){
			if(ret<(ret^kt[j]))ret^=kt[j];
		}
		printf("%lld\n",ret);
	}
	END_STACK_EXTEND;
}
accuracy: 0.421875
time_ms: 70
memory_kb: 12004
final_rank: 17

code_snippet: #include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<pair<int,long long> >g[110000];
long long val[110000];
long long kt[110000];
int v[110000];
int sz;
void dfs(int a,int b){
	v[a]=1;
	for(int i=0;i<g[a].size();i++){
		if(g[a][i].first==b)continue;
		if(!v[g[a][i].first]){
			val[g[a][i].first]=(val[a]^g[a][i].second);
			dfs(g[a][i].first,a);
		}else{
			kt[sz++]=(val[a]^val[g[a][i].first]^g[a][i].second);
		}
	}
}
int main(){
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	for(int i=0;i<b;i++){
		int p,q;
		long long r;
		scanf("%d%d%lld",&p,&q,&r);
		g[p].push_back(make_pair(q,r));
		g[q].push_back(make_pair(p,r));
	}
	sz=0;
	dfs(0,-1);
	int now=0;
	for(int i=59;i>=0;i--){
		int at=-1;
		for(int j=now;j<sz;j++){
			if(kt[j]&(1LL<<i)){
				at=j;break;
			}
		}
		if(!~at)continue;
		if(at!=now)swap(kt[at],kt[now]);
		for(int j=0;j<sz;j++){
			if(now==j)continue;
			if(kt[j]&(1LL<<i))kt[j]^=kt[now];
		}
		now++;
	}
	//for(int i=0;i<a;i++)printf("%lld\n",val[i]);
	//for(int i=0;i<now;i++)printf("%lld\n",kt[i]);
	for(int i=0;i<c;i++){
		int p,q;scanf("%d%d",&p,&q);
		long long ret=(val[p]^val[q]);
		for(int j=0;j<now;j++){
			if(ret<(ret^kt[j]))ret^=kt[j];
		}
		printf("%lld\n",ret);
	}
}
accuracy: 0.421875
time_ms: 40
memory_kb: 11996
final_rank: 16

code_snippet: #include <stdio.h>
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair
typedef long long Int;

int N, M, Q;
vector<pair<int, Int> > g[200000];
Int hi[200000], cy[64];
bool vis[200000];

void add(Int b) {
    for (int x = 63; b; x--) if (b&(1LL<<x)) {
        if (cy[x] == 0) { cy[x] = b; return ; }
        b ^= cy[x];
    }
}

void rec(int at, int pre, Int bit) {
    if (vis[at]) {
        add(bit^hi[at]);
        return ;
    }
    vis[at] = 1;
    hi[at] = bit;
    rep (i, g[at].size()) {
        const int to = g[at][i].first;
        if (to == pre) continue;
        rec(to, at, bit^g[at][i].second);
    }
}

void build() {
    rec(0, -1, 0);
}

Int solve(int u, int v) {
    Int ans = hi[u]^hi[v];
    for (int x = 63; x >= 0; x--) if (!(ans&(1LL<<x))) ans ^= cy[x];
    return ans;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N >> M >> Q;
    rep (i, M) {
        int f, t;
        Int p;
        cin >> f >> t >> p;
        g[f].push_back(mp(t, p));
        g[t].push_back(mp(f, p));
    }
    build();
    rep (_, Q) {
        int a, b;
        cin >> a >> b;
        cout << solve(a, b) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 16660
final_rank: 4

code_snippet: #include <stdio.h>
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair
typedef long long Int;

int N, M, Q;
vector<pair<int, Int> > g[200000];
Int hi[200000], cy[64];
bool vis[200000];

void add(Int b) {
    for (int x = 63; b; x--) if (b&(1LL<<x)) {
        if (cy[x] == 0) { cy[x] = b; return ; }
        b ^= cy[x];
    }
}

void rec(int at, int pre, Int bit) {
    if (vis[at]) {
        add(bit^hi[at]);
        return ;
    }
    vis[at] = 1;
    hi[at] = bit;
    rep (i, g[at].size()) {
        const  int to = g[at][i].first;
        if (to == pre) continue;
        rec(to, at, bit^g[at][i].second);
    }
}

void build() {
    rec(0, -1, 0);
}

Int solve(int u, int v) {
    Int ans = hi[u]^hi[v];
    for (int x = 63; x >= 0; x--) if (cy[x] && (ans&(1LL<<x)) == 0) {
        ans ^= cy[x];
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N >> M >> Q;
    rep (i, M) {
        int f, t;
        Int p;
        cin >> f >> t >> p;
        g[f].push_back(mp(t, p));
        g[t].push_back(mp(f, p));
    }
    build();
    rep (_, Q) {
        int a, b;
        cin >> a >> b;
        cout << solve(a, b) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 16660
final_rank: 4

code_snippet: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <set>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

struct Edge {
  int src;
  int dest;
  ll cost;
  Edge() {;}
  Edge(int src, int dest, ll cost) : src(src), dest(dest), cost(cost) {;}
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int n, m, q;
Graph g;
int visit[100010];
ll dist[100010];
ll base[100];

void Push(ll v) {
  int first = -1;
  for (int i = 60; i >= 0; i--) {
    if (v & (1LL << i)) {
      v ^= base[i];
      if (v & (1LL << i)) { first = max(first, i); }
    }
  }
  assert(first != -1 || v == 0);
  if (first != -1) { base[first] = v; }
}

void dfs(int from, int parent, ll cost) {
  if (visit[from] == 2) { return; }
  if (visit[from] == 1) {
    Push(cost ^ dist[from]);
    return;
  }
  visit[from] = 1;
  dist[from] = cost;
  FORIT(it, g[from]) {
    int to = it->dest;
    if (to == parent) { continue; }
    dfs(to, from, cost ^ it->cost);
  }
  visit[from] = 2;
}

int main() {
  while (scanf("%d %d %d", &n, &m, &q) > 0) {
    MEMSET(visit, 0);
    MEMSET(dist, -1);
    MEMSET(base, 0);
    g = Graph(n);
    REP(i, m) {
      int f, t;
      ll w;
      int v = scanf("%d %d %lld", &f, &t, &w);
      assert(v == 3);
      g[f].push_back(Edge(f, t, w));
      g[t].push_back(Edge(t, f, w));
    }
    dfs(0, 0, 0);
    REP(iter, q) {
      int f, t;
      int v = scanf("%d %d", &f, &t);
      assert(v == 2);
      ll ans = dist[f] ^ dist[t];
      for (int i = 60; i >= 0; i--) {
        if (!(ans & (1LL << i))) { ans ^= base[i]; }
      }
      printf("%lld\n", ans);
    }
  }
}
accuracy: 1
time_ms: 180
memory_kb: 16000
final_rank: 1

code_snippet: #include<cstdio>
#include<vector>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long ll;

struct edge{
	int u,v;
	ll cost;
};

int n;
vector<edge> G[100000];

vector<edge> T[100000]; // G の全域木

bool vis[100000];
void dfs1(int u){
	vis[u]=true;
	rep(i,G[u].size()){
		edge e=G[u][i];
		if(!vis[e.v]){
			T[u].push_back(e);
			dfs1(e.v);
		}
	}
}

ll dist[100000]; // dist[u] := ( T における頂点 0-u 間の距離 )
void dfs2(int u,ll sum){
	dist[u]=sum;
	rep(i,T[u].size()){
		edge e=T[u][i];
		dfs2(e.v,sum^e.cost);
	}
}

int main(){
	int m,q; scanf("%d%d%d",&n,&m,&q);
	rep(i,m){
		int u,v;
		ll cost; scanf("%d%d%lld",&u,&v,&cost);
		G[u].push_back((edge){u,v,cost});
		G[v].push_back((edge){v,u,cost});
	}

	dfs1(0);
	dfs2(0,0);

	ll basis[60]={};
	rep(u,n) rep(i,G[u].size()) {
		edge e=G[u][i];
		ll sum=dist[e.u]^dist[e.v]^e.cost;
		int i0=-1;
		for(int i=59;i>=0;i--){
			if(sum&(1LL<<i)){
				sum^=basis[i];
				if(sum&(1LL<<i)){ i0=i; break; }
			}
		}
		if(i0!=-1) basis[i0]=sum;
	}

	while(q--){
		int u,v; scanf("%d%d",&u,&v);
		ll ans=dist[u]^dist[v];
		for(int i=59;i>=0;i--){
			if((ans&(1LL<<i))==0){
				ans^=basis[i];
			}
		}
		printf("%lld\n",ans);
	}

	return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 19372
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <memory.h>
using namespace std;

#define rep(i, n) for(int i = 0; i< n; i++)
typedef long long ll;
typedef pair<ll, ll> P;
const int maxn = 100010;

ll dist[maxn];
vector<P> G[maxn];

vector<ll> bfs(){
  queue<ll> que;
  vector<ll> res;
  memset(dist, -1, sizeof(dist));
  dist[0] = 0;
  que.push(0);
  
  while(!que.empty()){
    int v = que.front(); que.pop();
    rep(i, (int)G[v].size()){
      int to = G[v][i].first;
      ll cost = G[v][i].second;
      if(dist[to] == -1){
	dist[to] = dist[v] ^ cost;
	que.push(to);
      }else{
	if(dist[to] ^ dist[v] ^ cost) res.push_back(dist[to] ^ dist[v] ^ cost);
      }
    }
  }
  return res;
}

vector<ll> collect(vector<ll> cycle){
  int n = cycle.size();
  vector<ll> res(60, 0);
  for(int i = 59; i >= 0; i--){
    rep(j, n){
      if((cycle[j] >> i) & 1){
	res[i] = cycle[j];
	rep(k, n){
	  if(k != j && ((cycle[k] >> i) & 1)) cycle[k] ^= cycle[j];
	}
	cycle[j] = 0;
	break;
      }
    }
  }
  return res;
}

ll calc(ll res, const vector<ll> &base){
  for(int i = 59; i >= 0; i--){
    if(((base[i] ^ res) >> i) & 1) res ^= base[i]; 
  }
  return res;
}

int main(){
  ll N, M, Q, a, b, f, t, p;
  
  cin >> N >> M >> Q;
  rep(i, M) {
    cin >> f >> t >> p;
    G[f].push_back(P(t, p));
    G[t].push_back(P(f, p));
  }
  
  vector<ll> cycle = bfs();
  vector<ll> base = collect(cycle);
  
  rep(i, Q){
    cin >> a >> b;
    cout << calc(dist[a] ^ dist[b], base) << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 470
memory_kb: 15060
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <memory.h>
using namespace std;

#define rep(i, n) for(int i = 0; i< n; i++)
typedef long long ll;
typedef pair<ll, ll> P;
const int maxn = 100010;

bool used[maxn];
ll dist[maxn];
vector<P> G[maxn];
vector<ll> cycle;
vector<ll> base;

void dfs(int v, int pre, ll d, ll *dist, vector<ll> &cycle){
  used[v] = true;
  dist[v] = d;
  rep(i, (int)G[v].size()){
    int to = G[v][i].first;
    ll cost = G[v][i].second;    
    if(to == pre) continue;
    if(!used[to]){
      dfs(to, v, d ^ cost, dist, cycle);
    }else{
      cycle.push_back(dist[to] ^ dist[v] ^ cost);
    }
  }
}

vector<ll> collect(vector<ll> cycle){
  int n = cycle.size();
  vector<ll> res(60, 0);
  for(int i = 59; i >= 0; i--){
    rep(j, n){
      if((cycle[j] >> i) & 1){
	res[i] = cycle[j];
	rep(k, n){
	  if(k != j && ((cycle[k] >> i) & 1)) cycle[k] ^= cycle[j];
	}
	cycle[j] = 0;
	break;
      }
    }
  }
  return res;
}

ll calc(ll res, const vector<ll> &base){
  for(int i = 59; i >= 0; i--){
    if(((base[i] ^ res) >> i) & 1) res ^= base[i]; 
  }
  return res;
}

int main(){
  ll N, M, Q, a, b, f, t, p;
  memset(used, false, sizeof(used));
  memset(dist, 0, sizeof(dist));

  cin >> N >> M >> Q;
  rep(i, M) {
    cin >> f >> t >> p;
    G[f].push_back(P(t, p));
    G[t].push_back(P(f, p));
  }
  
  dfs(0, -1, 0, dist, cycle);
  base = collect(cycle);
  
  rep(i, Q){
    cin >> a >> b;
    cout << calc(dist[a] ^ dist[b], base) << endl;
  }
  return 0;
}
accuracy: 0.625
time_ms: 340
memory_kb: 17772
final_rank: 15

code_snippet: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

struct Edge {
  int f;
  int t;
  ll p;
  Edge(int f, int t, ll p) : f(f),t(t),p(p) {}
};
vector<Edge> g[100000];
int visited[100000];
ll dist[100000];
ll base[100];

void add(ll w) {
  for (int i=59; i>=0; --i) {
    if (w>>i&1) {
      if (base[i]) {
        w ^= base[i];
      } else {
        base[i] = w;
        return;
      }
    }
  }
}

void dfs(int now, ll w) {
  if (visited[now] == 2) return;
  if (visited[now] == 1) {
    // loop
    // cout << now << " " << w << " " << dist[now] << " "  << (dist[now]^w) << endl;
    add(dist[now] ^ w);
    return;
  }
  visited[now] = 1;
  dist[now] = w;
  FOR(it, g[now]) {
    dfs(it->t,w^it->p);
  }
  visited[now] = 2;
}

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  REP(i,m) {
    int f,t;
    ll p;
    cin >> f >> t >> p;
    g[f].push_back(Edge(f,t,p));
    g[t].push_back(Edge(t,f,p));
  }
  memset(visited,0,sizeof(visited));
  memset(base,0,sizeof(base));
  dfs(0,0);
  while(q--) {
    int a, b;
    cin >> a >> b;
    ll w = dist[a]^dist[b];
    for (int i=59; i>=0; --i) {
      if (!(w>>i&1)) {
        w ^= base[i];
      }
    }
    cout << w << endl;
  }
}
accuracy: 1
time_ms: 900
memory_kb: 11000
final_rank: 13