Problem:

Weighted Union Find Trees

  There is a sequence $A = a_0, a_1, ..., a_{n-1}$. You are given the following information and questions.

relate$(x, y, z)$: $a_y$ is greater than $a_x$ by $z$

diff$(x, y)$: report the difference between $a_x$ and $a_y$ $(a_y - a_x)$

Input

$n \; q$
$query_1$
$query_2$
:
$query_q$

  In the first line, $n$ and $q$ are given. Then, $q$ information/questions are given in the following format.

0 $x \; y\; z$

  or

1 $x \; y$

where '0' of the first digit denotes the relate information and '1' denotes the diff question.

Output

For each diff question, print the difference between $a_x$ and $a_y$ $(a_y - a_x)$.

Constraints

  $2 \leq n \leq 100,000$

  $1 \leq q \leq 200,000$

  $0 \leq x, y < n$

  $x \ne y$

  $0 \leq z \leq 10000$

There are no inconsistency in the given information

Sample Input

5 6
0 0 2 5
0 1 2 3
1 0 1
1 1 3
0 1 4 8
1 0 4

Sample Output

2
?
10



Sample input:
5 6
0 0 2 5
0 1 2 3
1 0 1
1 1 3
0 1 4 8
1 0 4


Sample output:
2
?
10

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())

nodes = [x for x in range(n)]
weight = [0 for x in range(n)]

def unite(x, y, w) :   
    if nodes[x] < nodes[y] :
        nodes[y] = x
        weight[y] = w
    else :
        nodes[x] = y
        weight[x] = -w
        
def get_root(idx) :
    if nodes[idx] == idx :
        return idx
    else :
        r = get_root(nodes[idx])
        weight[idx] += weight[nodes[idx]]
        nodes[idx] = r
        return r

for i in range(q) :
    ip = input()
    
    # union
    if ip[0] == "0" :
        q, a, b, w = map(int, ip.split())
        unite(a, b, w)
        
    # evaluate
    else :
        q, a, b = map(int, ip.split())
        
        if get_root(a) == get_root(b) :
            print(weight[b] - weight[a])
        else :
            print("?")

Submission status: Wrong Answer

Solution code:
MAX = 10003 
N, Q =  (int(x) for x in  input().split())
ws = [0 for i in range(MAX)] 
par = [-1 for i in range(N)]

def weight(x):
    root(x)
    return ws[x]

def root(x):
    if par[x] < 0: return x
    p = root(par[x])
    ws[x] = ws[x] + ws[par[x]]
    par[x] = p
    return par[x]
    return y


def unite(y, x, z):
    z = z + weight(x)
    z = z - weight(y)
    x = root(x)
    y = root(y)
    if x == y: return 0
    if par[y] < par[x]:
        tmp = x
        x = y 
        y = tmp 
        z = -z
     
    par[x] = par[x] + par[y]
    par[y] = x
    ws[y] = z
    return 1 

def diff(x,y):
    if root(x) != root(y) :return 1
    ans = ws[x] - ws[y]
    return ans



for q in range(Q):
    t, *cmd =  (int(x) for x in  input().split())
    if t:
        x, y = cmd
        z = diff(x, y)
        if z==1:print("?")
        else :print(z)
    else:
        x, y, z = cmd
        unite(x, y, z)

Submission status: Wrong Answer

Solution code:
MAX = 10003 
N, Q =  (int(x) for x in  input().split())
ws = [0 for i in range(MAX)] 
par = [-1 for i in range(N)]

def weight(x):
    root(x)
    return ws[x]

def root(x):
    if par[x] < 0: return x
    p = root(par[x])
    ws[x] = ws[x] + ws[par[x]]
    par[x] = p
    return par[x]
    return y


def unite(y, x, z):
    z = z + weight(x)
    z = z - weight(y)
    x = root(x)
    y = root(y)
    if x == y: return 0
    if par[y] < par[x]:
        tmp = x
        x = y 
        y = tmp 
        z = -z
     
    par[x] = par[x] + par[y]
    par[y] = x
    ws[y] = z
    return 1 

def diff(x,y):
    if root(x) != root(y) :return -1
    ans = ws[x] - ws[y]
    return ans



for q in range(Q):
    t, *cmd =  (int(x) for x in  input().split())
    if t:
        x, y = cmd
        z = diff(x, y)
        if z==-1:print("?")
        else :print(z)
    else:
        x, y, z = cmd
        unite(x, y, z)

Submission status: Wrong Answer

Solution code:
MAX = 100003 
N, Q =  (int(x) for x in  input().split())
ws = [0 for i in range(MAX)] 
par = [-1 for i in range(N)]

def weight(x):
    root(x)
    return ws[x]

def root(x):
    if par[x] < 0: return x
    p = root(par[x])
    ws[x] = ws[x] + ws[par[x]]
    par[x] = p
    return par[x]
    return y


def unite(y, x, z):
    z = z + weight(x)
    z = z - weight(y)
    x = root(x)
    y = root(y)
    if x == y: return 0
    if par[y] < par[x]:
        tmp = x
        x = y 
        y = tmp 
        z = -z
     
    par[x] = par[x] + par[y]
    par[y] = x
    ws[y] = z
    return 1 

def diff(x,y):
    if root(x) != root(y) :return False
    ans = ws[x] - ws[y]
    return ans



for q in range(Q):
    t, *cmd =  (int(x) for x in  input().split())
    if t:
        x, y = cmd
        z = diff(x, y)
        if z==False:print("?")
        else :print(z)
    else:
        x, y, z = cmd
        unite(x, y, z)

Submission status: Wrong Answer

Solution code:
MAX = 100000
N, Q =  (int(x) for x in  input().split())
ws = [0 for i in range(MAX)] 
par = [-1 for i in range(N)]

def weight(x):
    root(x)
    return ws[x]

def root(x):
    if par[x] < 0: return x
    p = root(par[x])
    ws[x] = ws[x] + ws[par[x]]
    par[x] = p
    return par[x]
    return y


def unite(y, x, z):
    z = z + weight(x)
    z = z - weight(y)
    x = root(x)
    y = root(y)
    if x == y: return 0
    if par[y] < par[x]:
        tmp = x
        x = y 
        y = tmp 
        z = -z
     
    par[x] = par[x] + par[y]
    par[y] = x
    ws[y] = z
    return 1 

def diff(x,y):
    if root(x) != root(y) :return False
    ans = ws[x] - ws[y]
    return ans



for q in range(Q):
    t, *cmd =  (int(x) for x in  input().split())
    if t:
        x, y = cmd
        z = diff(x, y)
        if z==False:print("?")
        else :print(z)
    else:
        x, y, z = cmd
        unite(x, y, z)

Submission status: Wrong Answer

Solution code:
MAX = 100000
N, Q =  (int(x) for x in  input().split())
ws = [0 for i in range(MAX)] 
par = [-1 for i in range(MAX)]

def weight(x):
    root(x)
    return ws[x]

def root(x):
    if par[x] < 0: return x
    p = root(par[x])
    ws[x] = ws[x] + ws[par[x]]
    par[x] = p
    return par[x]
    return y


def unite(y, x, z):
    z = z + weight(x)
    z = z - weight(y)
    x = root(x)
    y = root(y)
    if x == y: return 0
    if par[y] < par[x]:
        tmp = x
        x = y 
        y = tmp 
        z = -z
     
    par[x] = par[x] + par[y]
    par[y] = x
    ws[y] = z
    return 1 

def diff(x,y):
    if root(x) != root(y) :return False
    ans = ws[x] - ws[y]
    return ans



for q in range(Q):
    t, *cmd =  (int(x) for x in  input().split())
    if t:
        x, y = cmd
        z = diff(x, y)
        if z==False:print("?")
        else :print(z)
    else:
        x, y, z = cmd
        unite(x, y, z)

Submission status: Wrong Answer

Solution code:
import sys

def simplefindset(x,element,resultPos=0):
    if(x != element[x]):
        resultPos = simplefindset(element[x],element,resultPos)
    else:
        resultPos = x
    return resultPos

def findset(x,element,weight,resultPos=0,sumW=0):
    if(x != element[x]):
        resultPos,sumW = findset(element[x],element,weight,resultPos,sumW)
    else:
        resultPos = x
    sumW += weight[x]
    return resultPos,sumW

def union(x,y,element,weight,w):
    x,wx = findset(x,element,weight)
    y,wy = findset(y,element,weight)
    link(x,wx,y,wy,w,element)

def link(x,wx,y,wy,z,element):
    element[x] = y
    weight[x] = z - (wx+wy)

n,q = map(int, input().split());

element = [i for i in range(n)]
weight = [0 for i in range(n)]

for i in range(q):
    inp = map(int, input().split());
    inp = list(inp)    
    if(inp[0] == 0):
        x,y,w = inp[1],inp[2],inp[3]
        union(x,y,element,weight,w)
    else:
        x,y = inp[1],inp[2]
        if(simplefindset(x,element) == simplefindset(y,element)):
            x,wx = findset(x,element,weight)
            y,wy = findset(y,element,weight)
            print(wx-wy)
        else:
            print('?')

Submission status: Wrong Answer

Solution code:
import sys

def simplefindset(x,element,resultPos=0):
    if(x != element[x]):
        resultPos = simplefindset(element[x],element,resultPos)
    else:
        resultPos = x
    return resultPos

def findset(x,element,weight,resultPos=0,sumW=0):
    if(x != element[x]):
        resultPos,sumW = findset(element[x],element,weight,resultPos,sumW)
    else:
        resultPos = x
    sumW += weight[x]
    return resultPos,sumW

def union(x,y,element,weight,w):
    x,wx = findset(x,element,weight)
    y,wy = findset(y,element,weight)
    link(x,wx,y,wy,w,element)

def link(x,wx,y,wy,z,element):
    if(z > (wx+wy)):
        z = z - (wx+wy)
    else:
        z = (wx+wy) - z

    element[x] = y
    weight[x] = z

n,q = map(int, input().split());

element = [i for i in range(n)]
weight = [0 for i in range(n)]

for i in range(q):
    inp = map(int, input().split());
    inp = list(inp)    
    if(inp[0] == 0):
        x,y,w = inp[1],inp[2],inp[3]
        union(x,y,element,weight,w)
    else:
        x,y = inp[1],inp[2]
        if(simplefindset(x,element) == simplefindset(y,element)):
            x,wx = findset(x,element,weight)
            y,wy = findset(y,element,weight)
            print(x,y,wx,wy,element,weight)
            print(wx-wy)
        else:
            print('?')

Submission status: Wrong Answer

Solution code:
import sys

def simplefindset(x,element,resultPos=0):
    if(x != element[x]):
        resultPos = simplefindset(element[x],element,resultPos)
    else:
        resultPos = x
    return resultPos

def findset(x,element,weight,resultPos=0,sumW=0):
    if(x != element[x]):
        resultPos,sumW = findset(element[x],element,weight,resultPos,sumW)
    else:
        resultPos = x
    sumW += weight[x]
    return resultPos,sumW

def union(x,y,element,weight,w):
    x,wx = findset(x,element,weight)
    y,wy = findset(y,element,weight)
    link(x,wx,y,wy,w,element)

def link(x,wx,y,wy,z,element):
    if(z > (wx+wy)):
        z = z - (wx+wy)
    else:
        z = (wx+wy) - z

    element[x] = y
    weight[x] = z

n,q = map(int, input().split());

element = [i for i in range(n)]
weight = [0 for i in range(n)]

for i in range(q):
    inp = map(int, input().split());
    inp = list(inp)    
    if(inp[0] == 0):
        x,y,w = inp[1],inp[2],inp[3]
        union(x,y,element,weight,w)
    else:
        x,y = inp[1],inp[2]
        if(simplefindset(x,element) == simplefindset(y,element)):
            x,wx = findset(x,element,weight)
            y,wy = findset(y,element,weight)
            print(wx-wy)
        else:
            print('?')

Submission status: Wrong Answer

Solution code:
import sys

def simplefindset(x,element,resultPos=0):
    if(x != element[x]):
        resultPos = simplefindset(element[x],element,resultPos)
    else:
        resultPos = x
    return resultPos

def findset(x,element,weight,resultPos=0,sumW=0):
    if(x != element[x]):
        resultPos,sumW = findset(element[x],element,weight,resultPos,sumW)
    else:
        resultPos = x
    sumW += weight[x]
    return resultPos,sumW

def union(x,y,element,weight,w):
    x,wx = findset(x,element,weight)
    y = simplefindset(y,element)
    link(x,wx,y,w,element)

def link(x,wx,y,z,element):
    if(z > wx):
        z = z - wx
    else:
        z = wx - z

    element[x] = y
    weight[x] = z

n,q = map(int, input().split());

element = [i for i in range(n)]
weight = [0 for i in range(n)]

for i in range(q):
    inp = map(int, input().split());
    inp = list(inp)    
    if(inp[0] == 0):
        x,y,w = inp[1],inp[2],inp[3]
        union(x,y,element,weight,w)
    else:
        x,y = inp[1],inp[2]
        if(simplefindset(x,element) == simplefindset(y,element)):
            x,wx = findset(x,element,weight)
            y,wy = findset(y,element,weight)
            print(wx-wy)
        else:
            print('?')

Submission status: Wrong Answer

Solution code:
import sys

def simplefindset(x,element,resultPos=0):
    if(x != element[x]):
        resultPos = simplefindset(element[x],element,resultPos)
    else:
        resultPos = x
    return resultPos

def findset(x,element,weight,resultPos=0,sumW=0):
    if(x != element[x]):
        resultPos,sumW = findset(element[x],element,weight,resultPos,sumW)
    else:
        resultPos = x
    sumW += weight[x]
    return resultPos,sumW

def union(x,y,element,weight,w):
    x,wx = findset(x,element,weight)
    y = simplefindset(y,element)
    link(x,wx,y,w,element)

def link(x,wx,y,z,element):
    if(z > wx):
        z = z - wx
    else:
        z = wx - z

    element[x] = y
    weight[x] = z

n,q = map(int, input().split());

element = [i for i in range(n)]
weight = [0 for i in range(n)]

for i in range(q):
    inp = map(int, input().split());
    inp = list(inp)    
    if(inp[0] == 0):
        x,y,w = inp[1],inp[2],inp[3]
        union(x,y,element,weight,w)
    else:
        x,y = inp[1],inp[2]
        if(simplefindset(x,element) == simplefindset(y,element)):
            x,wx = findset(x,element,weight)
            y,wy = findset(y,element,weight)
            print(abs(wx-wy))
        else:
            print('?')

Submission status: Wrong Answer

Solution code:
import sys

def simplefindset(x,element,resultPos=0):
    if(x != element[x]):
        resultPos = simplefindset(element[x],element,resultPos)
    else:
        resultPos = x
    return resultPos

def findset(x,element,weight,resultPos=0,sumW=0):
    if(x != element[x]):
        resultPos,sumW = findset(element[x],element,weight,resultPos,sumW)
    else:
        resultPos = x
    sumW += weight[x]
    return resultPos,sumW

def union(x,y,element,weight,w):
    x,wx = findset(x,element,weight)
    y,wy = findset(y,element,weight)
    link(x,wx,y,wy,w,element)

def link(x,wx,y,wy,z,element):
    if(z > wx):
        z = z - wx+wy
    else:
        z = wx-wy - z

    element[x] = y
    weight[x] = z

n,q = map(int, input().split());

element = [i for i in range(n)]
weight = [0 for i in range(n)]

for i in range(q):
    inp = map(int, input().split());
    inp = list(inp)    
    if(inp[0] == 0):
        x,y,w = inp[1],inp[2],inp[3]
        union(x,y,element,weight,w)
    else:
        x,y = inp[1],inp[2]
        if(simplefindset(x,element) == simplefindset(y,element)):
            x,wx = findset(x,element,weight)
            y,wy = findset(y,element,weight)
            print(abs(wx-wy))
        else:
            print('?')

Submission status: Wrong Answer

Solution code:
import sys

def simplefindset(x,element,resultPos=0):
    if(x != element[x]):
        resultPos = simplefindset(element[x],element,resultPos)
    else:
        resultPos = x
    return resultPos

def findset(x,element,weight,resultPos=0,sumW=0):
    if(x != element[x]):
        resultPos,sumW = findset(element[x],element,weight,resultPos,sumW)
    else:
        resultPos = x
    sumW += weight[x]
    return resultPos,sumW

def union(x,y,element,weight,w):
    x,wx = findset(x,element,weight)
    y,wy = findset(y,element,weight)
    link(x,wx,y,wy,w,element)

def link(x,wx,y,wy,z,element):
    if(z > wx):
        z = z - wx+wy
    else:
        z = wx-wy - z

    element[x] = y
    weight[x] = z

n,q = map(int, input().split());

element = [i for i in range(n)]
weight = [0 for i in range(n)]

for i in range(q):
    inp = map(int, input().split());
    inp = list(inp)    
    if(inp[0] == 0):
        x,y,w = inp[1],inp[2],inp[3]
        union(x,y,element,weight,w)
    else:
        x,y = inp[1],inp[2]
        if(simplefindset(x,element) == simplefindset(y,element)):
            x,wx = findset(x,element,weight)
            y,wy = findset(y,element,weight)
            print(wx-wy)
        else:
            print('?')

Submission status: Wrong Answer

Solution code:

n, q = map(int, input().split())

p = [i for i in range(n)]
rank = [0 for i in range(n)]
diff_weight = [0 for i in range(n)]


def link(x, y, z):
 if rank[x] > rank[y]:
  p[y] = x
  diff_weight[y] = z
 else:
  p[x] = y
  diff_weight[x] = -z
  if rank[x] == rank[y]:
   rank[y] = rank[y] + 1

def findSet(x):
 if x != p[x]:
  p[x] = findSet(p[x])
  diff_weight[x] += diff_weight[p[x]]
 return p[x]


def relate(x, y, z):
 rootx = findSet(x)
 rooty = findSet(y)
 z += diff_weight[x]
 z -= diff_weight[y]
 link(rootx, rooty, z)


def same(x, y):
 x = findSet(x)
 y = findSet(y)

 if x == y:
  return 1
 else :
  return 0

def weight(x):
 findSet(x)
 return diff_weight[x]

def diff(x, y):
 if same(x, y) != 1:
  return '?'
 else :
  return diff_weight[y] - diff_weight[x]


output = []

for i in range(q):
 line = list(map(int, input().split()))

 if line[0] == 0:
  relate(line[1], line[2], line[3])

 else :
  output.append(diff(line[1],line[2]))


for ans in output:
 print(ans)

Submission status: Wrong Answer

Solution code:

def union(x, y, t):
    x, t_x = findSet(x)
    y, t_y = findSet(y)
    link(x, t_x, y, t_y, t)


def link(x, d_x, y, d_y, t):
    print('', 'x', 'y')
    print('', x, y)
    print('', d_x, d_y, t)
    if r[x] > r[y]:
        p[y] = x
        d[y] = d_x + d_y - t
        print('', y, '->', p[y], '=', d[y], '\n')

    else:
        p[x] = y
        d[x] = d_y - d_x + t
        if r[x] == r[y]: r[y] = r[y] + 1
        print('', x, '->', p[x], '=', d[x], '\n')


def findSet(x):
    if x != p[x]:
        p[x], t = findSet(p[x])
        d[x] += t
    return p[x], d[x]


def diff(x, y):
    x, d_x = findSet(x)
    y, d_y = findSet(y)
    if x == y:
        return d_x - d_y

    else:
        return '?'




if __name__ == '__main__':

    p, r, d = [], [], []

    n_set, n_quare = map(int, input().split())

    for i in range(n_set):
        p.append(i)
        r.append(0)
        d.append(0)


    for i in range(n_quare):
        l = input().split()

        if len(l) == 4:
            _, x, y, t = map(int, l)
            union(x, y, t)

        else:
            _, x, y = map(int, l)
            print(diff(x, y))

Submission status: Wrong Answer

Solution code:

def union(x, y, t):
    x, t_x = findSet(x)
    y, t_y = findSet(y)
    link(x, t_x, y, t_y, t)


def link(x, d_x, y, d_y, t):
    #print('', 'x', 'y')
    #print('', x, y)
    #print('', d_x, d_y, t)
    if r[x] > r[y]:
        p[y] = x
        d[y] = d_x + d_y - t
        #print('', y, '->', p[y], '=', d[y], '\n')

    else:
        p[x] = y
        d[x] = d_y - d_x + t
        if r[x] == r[y]: r[y] = r[y] + 1
        #print('', x, '->', p[x], '=', d[x], '\n')


def findSet(x):
    if x != p[x]:
        p[x], t = findSet(p[x])
        d[x] += t
    return p[x], d[x]


def diff(x, y):
    x, d_x = findSet(x)
    y, d_y = findSet(y)
    if x == y:
        return d_x - d_y

    else:
        return '?'




if __name__ == '__main__':

    p, r, d = [], [], []

    n_set, n_quare = map(int, input().split())

    for i in range(n_set):
        p.append(i)
        r.append(0)
        d.append(0)


    for i in range(n_quare):
        l = input().split()

        if len(l) == 4:
            _, x, y, t = map(int, l)
            union(x, y, t)

        else:
            _, x, y = map(int, l)
            print(diff(x, y))

Submission status: Wrong Answer

Solution code:

n, q = map(int, input().split())

p = [i for i in range(n)]
rank = [0 for i in range(n)]
diff_weight = [0 for i in range(n)]


def link(x, y, z):
 if rank[x] >= rank[y]:
  p[y] = x
  diff_weight[y] = z
  if rank[x] == rank[y]:
   rank[x] = rank[x] + 1
 else:
  p[x] = y
  diff_weight[x] = -z

def findSet(x):
 if x != p[x]:
  p[x] = findSet(p[x])
  diff_weight[x] += diff_weight[p[x]]
 return p[x]


def relate(x, y, z):
 rootx = findSet(x)
 rooty = findSet(y)
 z += diff_weight[x]
 z -= diff_weight[y]
 link(rootx, rooty, z)


def same(x, y):
 x = findSet(x)
 y = findSet(y)

 if x == y:
  return 1
 else :
  return 0

def weight(x):
 findSet(x)
 return diff_weight[x]

def diff(x, y):
 if same(x, y) != 1:
  return '?'
 else :
  return diff_weight[y] - diff_weight[x]


output = []

for i in range(q):
 line = list(map(int, input().split()))

 if line[0] == 0:
  relate(line[1], line[2], line[3])

 else :
  output.append(diff(line[1],line[2]))


for ans in output:
 print(ans)

Submission status: Wrong Answer

Solution code:

n, q = map(int, input().split())

p = [i for i in range(n)]
rank = [0 for i in range(n)]
diff_weight = [0 for i in range(n)]


def link(x, y, z):
 if rank[x] > rank[y]:
  p[y] = x
  diff_weight[y] = z
 else:
  p[x] = y
  diff_weight[x] = -z
  if rank[x] == rank[y]:
   rank[y] = rank[y] + 1

def findSet(x):
 if x != p[x]:
  p[x] = findSet(p[x])
  diff_weight[x] += diff_weight[p[x]]
 return p[x]


def relate(x, y, z):
 rootx = findSet(x)
 rooty = findSet(y)
 z += diff_weight[x]
 z -= diff_weight[y]
 if rootx != rooty:
  link(rootx, rooty, z)


def same(x, y):
 x = findSet(x)
 y = findSet(y)

 if x == y:
  return 1
 else :
  return 0

def weight(x):
 findSet(x)
 return diff_weight[x]

def diff(x, y):
 if same(x, y) != 1:
  return '?'
 else :
  return diff_weight[y] - diff_weight[x]


output = []

for i in range(q):
 line = list(map(int, input().split()))

 if line[0] == 0:
  relate(line[1], line[2], line[3])

 else :
  output.append(diff(line[1],line[2]))
  print(diff(line[1],line[2]))


for ans in output:
 print(ans)

Submission status: Wrong Answer

Solution code:

n, q = map(int, input().split())

p = [i for i in range(n)]
rank = [0 for i in range(n)]
diff_weight = [0 for i in range(n)]


def link(x, y, z):
 if rank[x] > rank[y]:
  p[y] = x
  diff_weight[y] = z
 else:
  p[x] = y
  diff_weight[x] = -z
  if rank[x] == rank[y]:
   rank[y] = rank[y] + 1

def findSet(x):
 if x != p[x]:
  p[x] = findSet(p[x])
  diff_weight[x] += diff_weight[p[x]]
 return p[x]


def relate(x, y, z):
 rootx = findSet(x)
 rooty = findSet(y)
 z += diff_weight[x]
 z -= diff_weight[y]
 if rootx != rooty:
  link(rootx, rooty, z)


def same(x, y):
 x = findSet(x)
 y = findSet(y)

 if x == y:
  return 1
 else :
  return 0

def weight(x):
 findSet(x)
 return diff_weight[x]

def diff(x, y):
 if same(x, y) != 1:
  return '?'
 else :
  return diff_weight[y] - diff_weight[x]


output = []

for i in range(q):
 line = list(map(int, input().split()))

 if line[0] == 0:
  relate(line[1], line[2], line[3])

 else :
  output.append(diff(line[1],line[2]))
  #print(diff(line[1],line[2]))


for ans in output:
 print(ans)

Submission status: Wrong Answer

Solution code:
# Disjoint Set: Union Find Tree
[p, rank, d] = [[], [], []]
[n, q] = list(map(int, input().split()))
time = 0

def makeSet(x):
    global p, rank, w
    p.append(x)
    rank.append(0)

for i in range(n):
    makeSet(i)

w = [[] for i in range(n)]

def findSet(x):
    global p
    if x != p[x]:
        p[x] = findSet(p[x])
    return p[x]

def link(x, y):
    global p, rank
    if rank[x] > rank[y]:
        p[y] = x
    else:
        p[x] = y
        if rank[x] == rank[y]:
            rank[y] += 1

def relate(x, y, z):
    global w
    w[x].append((y, z))
    union(x, y)

def cost(x, y, n, total):
    global w, d, time
    time += 1
    if time > 5:
        return None
    for i in range(len(w[x])):
        if d[w[x][i][0]] == 0:
            #print(x, w[x][i][0])
            d[w[x][i][0]] = 1
            if y == w[x][i][0]:
                total += w[x][i][1]
                return total
            result = cost(w[x][i][0], y, n, total)
            if result != None:
                total = result + w[x][i][1]
                return total

    for i in range(n):
        for j in range(len(w[i])):
            if w[i][j][0] == x and d[i] == 0:
                #print(i, x)
                d[i] = 1
                if y == i:
                    total -= w[i][j][1]
                    return total
                result = cost(i, y, n, total)
                if result != None:
                    total = result - w[i][j][1]
                    return total

'''
        if ((x, i) in w.keys()) == True and d[i] == 0:
            d[i] = 1
            if i == y:
                total += w[(x, i)]
                return total
            result = cost(i, y, n, total)
            if result != None:
                total = result + w[(x, i)]
                return total     

        if ((i, x) in w.keys()) == True and d[i] == 0:
            d[i] = 1
            if i == y:
                total -= w[(i, x)]
                return total
            result = cost(i, y, n, total)
            if result != None:
                total = result - w[(i, x)]
                return total
                '''

def diff(x, y, n):
    if same(x, y) == 1:
        global d
        total = 0
        d = [0 for i in range(n)]
        d[x] = 1
        return cost(x, y, n, total)
    else:
        return "?"     

def union(x, y):
    link(findSet(x), findSet(y))    

def same(x, y):
    if findSet(x) == findSet(y):
        return 1
    else:
        return 0   

'''
print(p)
print(rank)
print(w)
print("\n")
'''

for i in range(q):
    data = list(map(int, input().split()))
    if data[0] == 0:
        relate(data[1], data[2], data[3])
    else:
        print(diff(data[1], data[2], n))
    '''
    print("\n")
    print(p)
    print(rank)
    print(w)
    print("\n")
    '''

Submission status: Wrong Answer

Solution code:
# Disjoint Set: Union Find Trees
[p, rank] = [[], []]
[n, q] = list(map(int, input().split()))
w = [0 for i in range(n)]

def makeSet(x):
    global p, rank
    p.append(x)
    rank.append(0)

def findSet(x):
    global p
    if x != p[x]:
        p[x] = findSet(p[x])
    return p[x]

def link(x, y):
    global p, rank
    if rank[x] > rank[y]:
        p[y] = x
    else:
        p[x] = y
        if rank[x] == rank[y]:
            rank[y] += 1

def union(x, y):
    link(findSet(x), findSet(y))

def same(x, y):
    if findSet(x) == findSet(y):
        return 1
    else:
        return 0   

def relate(x, y, z):
    global w
    #print(w)
    if w[y] == 0:
        w[y] = w[x] + z
    else:
        w[x] = w[y] - z
    #print("cost:", w[y])
    union(x, y)
'''
def cost(x, y, n, total):
    global w, d
    #time += 1
    #if time > 5:
    #    return None
    for i in range(len(w[x])):
        if d[w[x][i][0]] == 0:
            #print(x, w[x][i][0])
            d[w[x][i][0]] = 1
            if y == w[x][i][0]:
                total += w[x][i][1]
                return total
            result = cost(w[x][i][0], y, n, total)
            if result != None:
                total = result + w[x][i][1]
                return total

    for i in range(n):
        for j in range(len(w[i])):
            if w[i][j][0] == x and d[i] == 0:
                #print(i, x)
                d[i] = 1
                if y == i:
                    total -= w[i][j][1]
                    return total
                result = cost(i, y, n, total)
                if result != None:
                    total = result - w[i][j][1]
                    return total
'''

def diff(x, y):
    if same(x, y) == 1:
        global w
        return w[y] - w[x]
        '''
        global d
        total = 0
        d = [0 for i in range(n)]
        d[x] = 1
        return cost(x, y, n, total)
        '''
    else:
        return "?"     

'''
print(p)
print(rank)
print(w)
print("\n")
'''

for i in range(n):
    makeSet(i)

#w = [0 for i in range(n)]

for i in range(q):
    data = list(map(int, input().split()))
    if data[0] == 0:
        relate(data[1], data[2], data[3])
    else:
        print(diff(data[1], data[2]))
    '''
    print("\n")
    print(p)
    print(rank)
    print(w)
    print("\n")
    '''

Submission status: Wrong Answer

Solution code:
# Disjoint Set: Union Find Trees
[p, rank] = [[], []]
[n, q] = list(map(int, input().split()))
w = [0 for i in range(n)]
path_c = [[] for i in range(n)]

def makeSet(x):
    global p, rank
    p.append(x)
    rank.append(0)

def findSet(x):
    global p
    if x != p[x]:
        p[x] = findSet(p[x])
    return p[x]

def link(x, y):
    global p, rank
    if rank[x] > rank[y]:
        p[y] = x
    else:
        p[x] = y
        if rank[x] == rank[y]:
            rank[y] += 1

def union(x, y):
    link(findSet(x), findSet(y))

def same(x, y):
    if findSet(x) == findSet(y):
        return 1
    else:
        return 0   

def relate(x, y, z):
    global w, path_c
    path_c[x].append((y, z))
    #print(w)
    if w[y] == 0:
        w[y] = w[x] + z
    else:
        update(x, y, z)
        #w[x] = w[y] - z
    #print("cost:", w[y])
    union(x, y)

def update(x, y, z):
    global w, path_c
    w[x] = w[y] - z
    for i in range(len(path_c[x])):
        update(path_c[x][i][0], x, path_c[x][i][1])


def diff(x, y):
    if same(x, y) == 1:
        global w
        return w[y] - w[x]
        '''
        global d
        total = 0
        d = [0 for i in range(n)]
        d[x] = 1
        return cost(x, y, n, total)
        '''
    else:
        return "?"     

'''
print(p)
print(rank)
print(w)
print("\n")
'''

for i in range(n):
    makeSet(i)

#w = [0 for i in range(n)]

for i in range(q):
    data = list(map(int, input().split()))
    if data[0] == 0:
        relate(data[1], data[2], data[3])
    else:
        print(diff(data[1], data[2]))
    '''
    print("\n")
    print(p)
    print(rank)
    print(w)
    print("\n")
    '''

Submission status: Wrong Answer

Solution code:
# Disjoint Set: Union Find Trees
[p, rank] = [[], []]
[n, q] = list(map(int, input().split()))
w = [0 for i in range(n)]
path_c = [[] for i in range(n)]

def makeSet(x):
    global p, rank
    p.append(x)
    rank.append(0)

def findSet(x):
    global p
    if x != p[x]:
        p[x] = findSet(p[x])
    return p[x]

def link(x, y):
    global p, rank
    if rank[x] > rank[y]:
        p[y] = x
    else:
        p[x] = y
        if rank[x] == rank[y]:
            rank[y] += 1

def union(x, y):
    link(findSet(x), findSet(y))

def same(x, y):
    if findSet(x) == findSet(y):
        return 1
    else:
        return 0   

def relate(x, y, z):
    global w, path_c
    path_c[x].append((y, z))
    #print(w)
    if w[y] == 0:
        w[y] = w[x] + z
    else:
        update(x, y, z)
        #w[x] = w[y] - z
    #print("cost:", w[y])
    union(x, y)

def update(x, y, z):
    global w, path_c
    w[x] = w[y] - z
    for i in range(len(path_c[x])):
        update(path_c[x][i][0], x, path_c[x][i][1])


def diff(x, y):
    if same(x, y) == 1:
        global w
        return abs(w[y] - w[x])
        '''
        global d
        total = 0
        d = [0 for i in range(n)]
        d[x] = 1
        return cost(x, y, n, total)
        '''
    else:
        return "?"     

'''
print(p)
print(rank)
print(w)
print("\n")
'''

for i in range(n):
    makeSet(i)

#w = [0 for i in range(n)]

for i in range(q):
    data = list(map(int, input().split()))
    if data[0] == 0:
        relate(data[1], data[2], data[3])
    else:
        print(diff(data[1], data[2]))
    '''
    print("\n")
    print(p)
    print(rank)
    print(w)
    print("\n")
    '''