Shortest Path Queries (Codeforces 938G)
Tags: Others, dsu

You are given an undirected connected graph with weighted edges. The length of some path between two vertices is the bitwise xor of weights of all edges belonging to this path (if some edge is traversed more than once, then it is included in bitwise xor the same number of times). There are three types of queries you have to process:  1 x y d — add an edge connecting vertex x to vertex y with weight d. It is guaranteed that there is no edge connecting x to y before this query;  2 x y — remove an edge connecting vertex x to vertex y. It is guaranteed that there was such edge in the graph, and the graph stays connected after this query;  3 x y — calculate the length of the shortest path (possibly non-simple) from vertex x to vertex y. Print the answers for all queries of type 3.

Input: The first line contains two numbers n and m (1 ≤ n, m ≤ 200000) — the number of vertices and the number of edges in the graph, respectively.Then m lines follow denoting the edges of the graph. Each line contains three integers x, y and d (1 ≤ x < y ≤ n, 0 ≤ d ≤ 230 - 1). Each pair (x, y) is listed at most once. The initial graph is connected.Then one line follows, containing an integer q (1 ≤ q ≤ 200000) — the number of queries you have to process.Then q lines follow, denoting queries in the following form:  1 x y d (1 ≤ x < y ≤ n, 0 ≤ d ≤ 230 - 1) — add an edge connecting vertex x to vertex y with weight d. It is guaranteed that there is no edge connecting x to y before this query;  2 x y (1 ≤ x < y ≤ n) — remove an edge connecting vertex x to vertex y. It is guaranteed that there was such edge in the graph, and the graph stays connected after this query;  3 x y (1 ≤ x < y ≤ n) — calculate the length of the shortest path (possibly non-simple) from vertex x to vertex y. It is guaranteed that at least one query has type 3.

Output: Print the answers for all queries of type 3 in the order they appear in input.


Sample Inputs:
5 5
1 2 3
2 3 4
3 4 5
4 5 6
1 5 1
5
3 1 5
1 1 3 1
3 1 5
2 1 5
3 1 5

Sample Outputs:
1
1
2

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u << 20 ^ v

def unite(s, t, w):
    while s ^ root[s]:
        w ^= d[s]
        s = root[s]
    while t ^ root[t]:
        w ^= d[t]
        t = root[t]
    if s == t:
        return 0
    if rank[s] < rank[t]:
        (s, t) = (t, s)
    (ra, ro) = (rank[s], root[t])
    if rank[s] == rank[t]:
        rank[s] += 1
    (root[t], d[t]) = (s, w)
    st1.append(f(s, t))
    st2.append(f(ra, ro))
    return 1

def dist(s, t, c0):
    u = s
    while u ^ root[u]:
        color[u] = c0
        u = root[u]
    color[u] = c0
    ans = 0
    while color[t] ^ c0:
        ans ^= d[t]
        t = root[t]
    while s ^ t:
        ans ^= d[s]
        s = root[s]
    return ans

def undo(x, y):
    (s, t) = (x >> 20, x & z)
    (ra, ro) = (y >> 20, y & z)
    (rank[s], root[t], d[t]) = (ra, ro, 0)
    return
(n, m) = map(int, input().split())
u = dict()
for _ in range(m):
    (x, y, d) = map(int, input().split())
    u[f(x, y)] = f(d, 0)
q = int(input())
t = -1
(a, b, c) = ([], [], [])
q1 = []
z = 1048575
for _ in range(q):
    q0 = list(map(int, input().split()))
    (x, y) = (q0[1], q0[2])
    if q0[0] == 1:
        d = q0[3]
        u[f(x, y)] = f(d, t + 1)
    elif q0[0] == 2:
        if u[f(x, y)] & z <= t:
            a.append(f(x, y))
            b.append(f(u[f(x, y)] & z, t))
            c.append(u[f(x, y)] >> 20)
        u[f(x, y)] = -1
    else:
        t += 1
        q1.append(f(x, y))
for i in u:
    if u[i] & z <= t:
        a.append(i)
        b.append(f(u[i] & z, t))
        c.append(u[i] >> 20)
m = t + 1
l1 = pow(2, (m + 1).bit_length())
l2 = 2 * l1
s0 = [0] * l2
for i in b:
    l0 = (i >> 20) + l1
    r0 = (i & z) + l1
    while l0 <= r0:
        if l0 & 1:
            s0[l0] += 1
            l0 += 1
        l0 >>= 1
        if not r0 & 1 and l0 <= r0:
            s0[r0] += 1
            r0 -= 1
        r0 >>= 1
for i in range(1, l2):
    s0[i] += s0[i - 1]
now = [0] + list(s0)
tree = [-1] * now[l2]
for i in range(len(b)):
    l0 = (b[i] >> 20) + l1
    r0 = (b[i] & z) + l1
    while l0 <= r0:
        if l0 & 1:
            tree[now[l0]] = i
            now[l0] += 1
            l0 += 1
        l0 >>= 1
        if not r0 & 1 and l0 <= r0:
            tree[now[r0]] = i
            now[r0] += 1
            r0 -= 1
        r0 >>= 1
root = [i for i in range(n + 1)]
rank = [1 for _ in range(n + 1)]
d = [0] * (n + 1)
s0 = [0] + s0
now = 1
visit = [0] * l2
(cnt1, cnt2) = ([0] * l2, [0] * l2)
(st1, st2) = ([], [])
(color, c0) = ([0] * (n + 1), 0)
b = []
ans = []
while len(ans) ^ m:
    if not visit[now]:
        visit[now] = 1
        for k in range(s0[now], s0[now + 1]):
            i = tree[k]
            (u, v, w0) = (a[i] >> 20, a[i] & z, c[i])
            f0 = unite(u, v, w0)
            if f0:
                cnt1[now] += 1
                continue
            c0 += 1
            w = dist(u, v, c0) ^ w0
            for j in b:
                w = min(w, w ^ j)
            if w:
                b.append(w)
                cnt2[now] += 1
        if now < l1:
            now = now << 1
        else:
            (u, v) = (q1[now ^ l1] >> 20, q1[now ^ l1] & z)
            c0 += 1
            ans0 = dist(u, v, c0)
            for j in b:
                ans0 = min(ans0, ans0 ^ j)
            ans.append(ans0)
    elif now < l1 and (not visit[now << 1 ^ 1]):
        now = now << 1 ^ 1
    else:
        for _ in range(cnt1[now]):
            undo(st1.pop(), st2.pop())
        for _ in range(cnt2[now]):
            b.pop()
        now >>= 1
sys.stdout.write('\n'.join(map(str, ans)))

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u << 20 ^ v

def unite(s, t, w):
    (s, t, w0) = dist(s, t)
    if s == t:
        return 0
    if rank[s] < rank[t]:
        (s, t) = (t, s)
    (ra, ro) = (rank[s], root[t])
    if rank[s] == rank[t]:
        rank[s] += 1
    (root[t], d[t]) = (s, w ^ w0)
    st1.append(f(s, t))
    st2.append(f(ra, ro))
    return 1

def dist(s, t):
    ans = 0
    while s ^ root[s]:
        ans ^= d[s]
        s = root[s]
    while t ^ root[t]:
        ans ^= d[t]
        t = root[t]
    return (s, t, ans)

def undo(x, y):
    (s, t) = (x >> 20, x & z)
    (ra, ro) = (y >> 20, y & z)
    (rank[s], root[t], d[t]) = (ra, ro, 0)
    return
(n, m) = map(int, input().split())
u = dict()
for _ in range(m):
    (x, y, d) = map(int, input().split())
    u[f(x, y)] = f(d, 0)
q = int(input())
t = -1
(a, b, c) = ([], [], [])
q1 = []
z = 1048575
for _ in range(q):
    q0 = list(map(int, input().split()))
    (x, y) = (q0[1], q0[2])
    if q0[0] == 1:
        d = q0[3]
        u[f(x, y)] = f(d, t + 1)
    elif q0[0] == 2:
        if u[f(x, y)] & z <= t:
            a.append(f(x, y))
            b.append(f(u[f(x, y)] & z, t))
            c.append(u[f(x, y)] >> 20)
        u[f(x, y)] = -1
    else:
        t += 1
        q1.append(f(x, y))
for i in u:
    if u[i] & z <= t:
        a.append(i)
        b.append(f(u[i] & z, t))
        c.append(u[i] >> 20)
m = t + 1
l1 = pow(2, (m + 1).bit_length())
l2 = 2 * l1
s0 = [0] * l2
for i in b:
    l0 = (i >> 20) + l1
    r0 = (i & z) + l1
    while l0 <= r0:
        if l0 & 1:
            s0[l0] += 1
            l0 += 1
        l0 >>= 1
        if not r0 & 1 and l0 <= r0:
            s0[r0] += 1
            r0 -= 1
        r0 >>= 1
for i in range(1, l2):
    s0[i] += s0[i - 1]
now = [0] + list(s0)
tree = [-1] * now[l2]
for i in range(len(b)):
    l0 = (b[i] >> 20) + l1
    r0 = (b[i] & z) + l1
    while l0 <= r0:
        if l0 & 1:
            tree[now[l0]] = i
            now[l0] += 1
            l0 += 1
        l0 >>= 1
        if not r0 & 1 and l0 <= r0:
            tree[now[r0]] = i
            now[r0] += 1
            r0 -= 1
        r0 >>= 1
root = [i for i in range(n + 1)]
rank = [1 for _ in range(n + 1)]
d = [0] * (n + 1)
s0 = [0] + s0
now = 1
visit = [0] * l2
(cnt1, cnt2) = ([0] * l2, [0] * l2)
(st1, st2) = ([], [])
b = []
ans = []
while len(ans) ^ m:
    if not visit[now]:
        visit[now] = 1
        for k in range(s0[now], s0[now + 1]):
            i = tree[k]
            (u, v, w0) = (a[i] >> 20, a[i] & z, c[i])
            f0 = unite(u, v, w0)
            if f0:
                cnt1[now] += 1
                continue
            (_, _, w1) = dist(u, v)
            w = w0 ^ w1
            for j in b:
                w = min(w, w ^ j)
            if w:
                b.append(w)
                cnt2[now] += 1
        if now < l1:
            now = now << 1
        else:
            (u, v) = (q1[now ^ l1] >> 20, q1[now ^ l1] & z)
            (_, _, ans0) = dist(u, v)
            for j in b:
                ans0 = min(ans0, ans0 ^ j)
            ans.append(ans0)
    elif now < l1 and (not visit[now << 1 ^ 1]):
        now = now << 1 ^ 1
    else:
        for _ in range(cnt1[now]):
            undo(st1.pop(), st2.pop())
        for _ in range(cnt2[now]):
            b.pop()
        now >>= 1
sys.stdout.write('\n'.join(map(str, ans)))

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u << 20 ^ v

def unite(s, t, w):
    (s, t, w0) = dist(s, t)
    if s == t:
        return 0
    if rank[s] < rank[t]:
        (s, t) = (t, s)
    (ra, ro) = (rank[s], root[t])
    if rank[s] == rank[t]:
        rank[s] += 1
    (root[t], d[t]) = (s, w ^ w0)
    st1.append(f(s, t))
    st2.append(f(ra, ro))
    return 1

def dist(s, t):
    ans = 0
    while s ^ root[s]:
        ans ^= d[s]
        s = root[s]
    while t ^ root[t]:
        ans ^= d[t]
        t = root[t]
    return (s, t, ans)

def undo(x, y):
    (s, t) = (x >> 20, x & z)
    (ra, ro) = (y >> 20, y & z)
    (rank[s], root[t], d[t]) = (ra, ro, 0)
    return
(n, m) = map(int, input().split())
u = dict()
for _ in range(m):
    (x, y, d) = map(int, input().split())
    u[f(x, y)] = f(d, 0)
q = int(input())
t = -1
(a, b, c) = ([], [], [])
q1 = []
z = 1048575
for _ in range(q):
    q0 = list(map(int, input().split()))
    (x, y) = (q0[1], q0[2])
    if q0[0] == 1:
        d = q0[3]
        u[f(x, y)] = f(d, t + 1)
    elif q0[0] == 2:
        if u[f(x, y)] & z <= t:
            a.append(f(x, y))
            b.append(f(u[f(x, y)] & z, t))
            c.append(u[f(x, y)] >> 20)
        u[f(x, y)] = -1
    else:
        t += 1
        q1.append(f(x, y))
for i in u:
    if u[i] & z <= t:
        a.append(i)
        b.append(f(u[i] & z, t))
        c.append(u[i] >> 20)
m = t + 1
l1 = pow(2, (m + 1).bit_length())
l2 = 2 * l1
s0 = [0] * l2
for i in b:
    l0 = (i >> 20) + l1
    r0 = (i & z) + l1
    while l0 <= r0:
        if l0 & 1:
            s0[l0] += 1
            l0 += 1
        l0 >>= 1
        if not r0 & 1 and l0 <= r0:
            s0[r0] += 1
            r0 -= 1
        r0 >>= 1
for i in range(1, l2):
    s0[i] += s0[i - 1]
now = [0] + list(s0)
tree = [-1] * now[l2]
for i in range(len(b)):
    l0 = (b[i] >> 20) + l1
    r0 = (b[i] & z) + l1
    while l0 <= r0:
        if l0 & 1:
            tree[now[l0]] = i
            now[l0] += 1
            l0 += 1
        l0 >>= 1
        if not r0 & 1 and l0 <= r0:
            tree[now[r0]] = i
            now[r0] += 1
            r0 -= 1
        r0 >>= 1
root = [i for i in range(n + 1)]
rank = [1 for _ in range(n + 1)]
d = [0] * (n + 1)
s0 = [0] + s0
now = 1
visit = [0] * l2
(cnt1, cnt2) = ([0] * l2, [0] * l2)
(st1, st2) = ([], [])
b = []
ans = []
while len(ans) ^ m:
    if not visit[now]:
        visit[now] = 1
        for k in range(s0[now], s0[now + 1]):
            i = tree[k]
            (u, v, w0) = (a[i] >> 20, a[i] & z, c[i])
            f0 = unite(u, v, w0)
            if f0:
                cnt1[now] += 1
                continue
            (_, _, w1) = dist(u, v)
            w = w0 ^ w1
            for j in b:
                w = min(w, w ^ j)
            if w:
                b.append(w)
                cnt2[now] += 1
        if now < l1:
            now = now << 1
        else:
            (u, v) = (q1[now ^ l1] >> 20, q1[now ^ l1] & z)
            (_, _, ans0) = dist(u, v)
            for j in b:
                ans0 = min(ans0, ans0 ^ j)
            ans.append(ans0)
    elif now < l1 and (not visit[now << 1 ^ 1]):
        now = now << 1 ^ 1
    else:
        for _ in range(cnt1[now]):
            undo(st1.pop(), st2.pop())
        for _ in range(cnt2[now]):
            b.pop()
        now >>= 1
sys.stdout.write('\n'.join(map(str, ans)))