Problem:


Score : 600 points

Problem Statement

Kenkoooo found a simple connected graph.
The vertices are numbered 1 through n.
The i-th edge connects Vertex u_i and v_i, and has a fixed integer s_i.

Kenkoooo is trying to write a positive integer in each vertex so that the following condition is satisfied:

For every edge i, the sum of the positive integers written in Vertex u_i and v_i is equal to s_i.

Find the number of such ways to write positive integers in the vertices.

Constraints

2 \leq n \leq 10^5

1 \leq m \leq 10^5

1 \leq u_i < v_i \leq n

2 \leq s_i \leq 10^9

If i\neq j, then u_i \neq u_j  or v_i \neq v_j.

The graph is connected.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m
u_1 v_1 s_1
:
u_m v_m s_m

Output

Print the number of ways to write positive integers in the vertices so that the condition is satisfied.

Sample Input 1

3 3
1 2 3
2 3 5
1 3 4

Sample Output 1

1

The condition will be satisfied if we write 1,2 and 3 in vertices 1,2 and 3, respectively.
There is no other way to satisfy the condition, so the answer is 1.

Sample Input 2

4 3
1 2 6
2 3 7
3 4 5

Sample Output 2

3

Let a,b,c and d be the numbers to write in vertices 1,2,3 and 4, respectively.
There are three quadruples (a,b,c,d) that satisfy the condition:

(a,b,c,d)=(1,5,2,3)

(a,b,c,d)=(2,4,3,2)

(a,b,c,d)=(3,3,4,1)

Sample Input 3

8 7
1 2 1000000000
2 3 2
3 4 1000000000
4 5 2
5 6 1000000000
6 7 2
7 8 1000000000

Sample Output 3

0



Sample input:
3 3
1 2 3
2 3 5
1 3 4


Sample output:
1

Submission status: Wrong Answer

Solution code:
def is_bipartite(G):
    v_min = [0, float('inf')]
    value = [-1]*n
    color = [-1]*n
    que = deque([0])
    color[0] = 0
    value[0] = 0
    while que:
        v = que.pop()
        val = value[v]
        c = color[v]
        for nv, s in G[v]:
            if color[nv]<0:
                color[nv] = 1-c
                value[nv] = s-val
                v_min[1-c] = min(v_min[1-c], s-val)
                que.append(nv)
            elif color[nv] == c:
                return (False, v_min, value[nv], s-val, nv)
    return (True, v_min, 0, 0, 0)
def C(v, c):
    value = [-1]*n
    que = deque([v])
    value[v] = c
                                                                                
    while que:
        v = que.pop()
        val = value[v]
        for nv, s in G[v]:
            if val<=0:
                return False
            if value[nv]<0:
                value[nv] = s-val
                que.append(nv)
            elif value[nv] != s-val:
                return False
    return True

n, m = map(int, input().split())
G = [[] for _ in range(n)]
for _ in range(m):
    u, v, s = map(int, input().split())
    G[u-1].append((v-1, s))
    G[v-1].append((u-1, s))
color = [-1]*n
from collections import deque
flag, v_min, a, b, v = is_bipartite(G)
if flag:
    a, b = v_min
    if a+b<=1:
        print(0)
    else:
        print(a+b-1)
else:
    if (a+b)%2 != 0:
        print(0)
    else:
        c = (a+b)//2
        if C(v, c):
            print(1)
        else:
            print(0)

Submission status: Wrong Answer

Solution code:
def is_bipartite(G):
    v_min = [0, float('inf')]
    value = [-1]*n
    color = [-1]*n
    que = deque([0])
    color[0] = 0
    value[0] = 0
    while que:
        v = que.pop()
        val = value[v]
        c = color[v]
        for nv, s in G[v]:
            if color[nv]<0:
                color[nv] = 1-c
                value[nv] = s-val
                v_min[1-c] = min(v_min[1-c], s-val)
                que.append(nv)
            elif color[nv] == c:
                return (False, v_min, value[nv], s-val, nv)
    return (True, v_min, 0, 0, 0)
def C(v, c):
    value = [-1]*n
    que = deque([v])
    value[v] = c
                                                                                
    while que:
        v = que.pop()
        val = value[v]
        for nv, s in G[v]:
            if val<0:
                return False
            if value[nv]<0:
                value[nv] = s-val
                que.append(nv)
            elif value[nv] != s-val:
                return False
    return True

n, m = map(int, input().split())
G = [[] for _ in range(n)]
for _ in range(m):
    u, v, s = map(int, input().split())
    G[u-1].append((v-1, s))
    G[v-1].append((u-1, s))
color = [-1]*n
from collections import deque
flag, v_min, a, b, v = is_bipartite(G)
if flag:
    a, b = v_min
    if a+b<=1:
        print(0)
    else:
        print(a+b-1)
else:
    if (a+b)%2 != 0:
        print(0)
    else:
        c = (a+b)//2
        if C(v, c):
            print(1)
        else:
            print(0)

Submission status: Wrong Answer

Solution code:
from collections import deque


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1] * (n + 1)
        self.rnk = [0] * (n + 1)

    def find_root(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]

    def unite(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1

    def isSameGroup(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def size(self, x):
        return -self.root[self.find_root(x)]


n, m = map(int, input().split())
adj = [[] for _ in range(n+1)]
others = []
UF = UnionFind(n)
for _ in range(m):
    u, v, s = map(int, input().split())
    if not UF.isSameGroup(u, v):
        adj[u].append([v, s])
        adj[v].append([u, s])
        UF.unite(u, v)
    else:
        others.append([u, v, s])

que = deque()
que.append(1)
seen = [0] * (n+1)
seen[1] = 1
val = [0] * (n+1)
plus_min = float('inf')
minus_min = float('inf')
while que:
    v = que.pop()
    for us in adj[v]:
        u, s = us
        if seen[u] == 0:
            que.append(u)
            seen[u] = -seen[v]
            val[u] = s - val[v]
            if seen[u] == -1:
                minus_min = min(minus_min, val[u])
                if minus_min <= 1:
                    print(0)
                    exit()
            else:
                plus_min = min(plus_min, val[u])

x = []
for u, v, s in others:
    if seen[u] != seen[v]:
        if val[u] + val[v] != s:
            print(0)
            exit()
    else:
        if (s - val[u] - val[v]) % 2 == 1:
            print(0)
            exit()
        x_cand = ((s - val[u] - val[v]) // 2) * seen[u]
        if x_cand <= 0:
            print(0)
            exit()
        if x:
            if x_cand != x[0]:
                print(0)
                exit()
        else:
            x.append(x_cand)

if x:
    print(1)
else:
    if plus_min >= 0:
        print(max(0, minus_min - 1))
    else:
        print(max(0, minus_min - 1 - (-plus_min + 1) + 1))

Submission status: Wrong Answer

Solution code:
from collections import deque


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1] * (n + 1)
        self.rnk = [0] * (n + 1)

    def find_root(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]

    def unite(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1

    def isSameGroup(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def size(self, x):
        return -self.root[self.find_root(x)]


n, m = map(int, input().split())
adj = [[] for _ in range(n+1)]
others = []
UF = UnionFind(n)
for _ in range(m):
    u, v, s = map(int, input().split())
    if not UF.isSameGroup(u, v):
        adj[u].append([v, s])
        adj[v].append([u, s])
        UF.unite(u, v)
    else:
        others.append([u, v, s])

que = deque()
que.append(1)
seen = [0] * (n+1)
seen[1] = 1
val = [0] * (n+1)
plus_min = float('inf')
minus_min = float('inf')
while que:
    v = que.pop()
    for us in adj[v]:
        u, s = us
        if seen[u] == 0:
            que.append(u)
            seen[u] = -seen[v]
            val[u] = s - val[v]
            if seen[u] == -1:
                minus_min = min(minus_min, val[u])
                if minus_min <= 1:
                    print(0)
                    exit()
            else:
                plus_min = min(plus_min, val[u])

x = []
for u, v, s in others:
    if seen[u] != seen[v]:
        if val[u] + val[v] != s:
            print(0)
            exit()
    else:
        if (s - val[u] - val[v]) % 2 == 1:
            print(0)
            exit()
        x_cand = ((s - val[u] - val[v]) // 2) * seen[u]
        if x_cand <= 0:
            print(0)
            exit()
        if x:
            if x_cand != x[0]:
                print(0)
                exit()
        else:
            x.append(x_cand)

if x:
    print(1)
else:
    print(max(0, minus_min - 1 - (-min(plus_min, 0) + 1) + 1))

Submission status: Wrong Answer

Solution code:
from collections import deque


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1] * (n + 1)
        self.rnk = [0] * (n + 1)

    def find_root(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]

    def unite(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1

    def isSameGroup(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def size(self, x):
        return -self.root[self.find_root(x)]


n, m = map(int, input().split())
adj = [[] for _ in range(n+1)]
others = []
UF = UnionFind(n)
for _ in range(m):
    u, v, s = map(int, input().split())
    if not UF.isSameGroup(u, v):
        adj[u].append([v, s])
        adj[v].append([u, s])
        UF.unite(u, v)
    else:
        others.append([u, v, s])

que = deque()
que.append(1)
seen = [0] * (n+1)
seen[1] = 1
val = [0] * (n+1)
plus_min = float('inf')
minus_min = float('inf')
while que:
    v = que.pop()
    for us in adj[v]:
        u, s = us
        if seen[u] == 0:
            que.append(u)
            seen[u] = -seen[v]
            val[u] = s - val[v]
            if seen[u] == -1:
                minus_min = min(minus_min, val[u])
                if minus_min <= 1:
                    print(0)
                    exit()
            else:
                plus_min = min(plus_min, val[u])

x = []
for u, v, s in others:
    if seen[u] != seen[v]:
        if val[u] + val[v] != s:
            print(0)
            exit()
    else:
        if (s - val[u] - val[v]) % 2 == 1:
            print(0)
            exit()
        x_cand = seen[u] * (s - val[u] - val[v]) // 2
        if x_cand <= 0:
            print(0)
            exit()
        if x:
            if x_cand != x[0]:
                print(0)
                exit()
        else:
            x.append(x_cand)

if x:
    print(1)
else:
    print(max(0, minus_min - 1 - (-min(plus_min, 0) + 1) + 1))

Submission status: Wrong Answer

Solution code:
from collections import deque


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1] * (n + 1)
        self.rnk = [0] * (n + 1)

    def find_root(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]

    def unite(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1

    def isSameGroup(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def size(self, x):
        return -self.root[self.find_root(x)]


n, m = map(int, input().split())
adj = [[] for _ in range(n+1)]
others = []
UF = UnionFind(n)
for _ in range(m):
    u, v, s = map(int, input().split())
    if not UF.isSameGroup(u, v):
        adj[u].append([v, s])
        adj[v].append([u, s])
        UF.unite(u, v)
    else:
        others.append([u, v, s])

que = deque()
que.append(1)
seen = [0] * (n+1)
seen[1] = 1
val = [0] * (n+1)
plus_min = float('inf')
minus_min = float('inf')
while que:
    v = que.pop()
    for us in adj[v]:
        u, s = us
        if seen[u] == 0:
            que.append(u)
            seen[u] = -seen[v]
            val[u] = s - val[v]
            if seen[u] == -1:
                minus_min = min(minus_min, val[u])
            else:
                plus_min = min(plus_min, val[u])

x = []
for u, v, s in others:
    if seen[u] != seen[v]:
        if val[u] + val[v] != s:
            print(0)
            exit()
    else:
        if (s - val[u] - val[v]) % 2 == 1:
            print(0)
            exit()
        x_cand = seen[u] * (s - val[u] - val[v]) // 2
        if x_cand <= 0:
            print(0)
            exit()
        if x:
            if x_cand != x[0]:
                print(0)
                exit()
        else:
            x.append(x_cand)

if x:
    print(1)
else:
    print(max(0, minus_min - 1 - (-min(plus_min, 0) + 1) + 1))

Submission status: Wrong Answer

Solution code:
from collections import deque


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1] * (n + 1)
        self.rnk = [0] * (n + 1)

    def find_root(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]

    def unite(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1

    def isSameGroup(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def size(self, x):
        return -self.root[self.find_root(x)]


n, m = map(int, input().split())
adj = [[] for _ in range(n+1)]
others = []
UF = UnionFind(n)
for _ in range(m):
    u, v, s = map(int, input().split())
    if not UF.isSameGroup(u, v):
        adj[u].append([v, s])
        adj[v].append([u, s])
        UF.unite(u, v)
    else:
        others.append([u, v, s])

que = deque()
que.append(1)
seen = [0] * (n+1)
seen[1] = 1
val = [0] * (n+1)
plus_min = float('inf')
minus_min = float('inf')
while que:
    v = que.pop()
    for us in adj[v]:
        u, s = us
        if seen[u] == 0:
            que.append(u)
            seen[u] = -seen[v]
            val[u] = s - val[v]
            if seen[u] == -1:
                minus_min = min(minus_min, val[u])
            else:
                plus_min = min(plus_min, val[u])

x = []
for u, v, s in others:
    if seen[u] != seen[v]:
        if val[u] + val[v] != s:
            print(0)
            exit()
    else:
        if (s - val[u] - val[v]) % 2 == 1:
            print(0)
            exit()
        x_cand = seen[u] * (s - val[u] - val[v]) // 2
        if x_cand < 0:
            print(0)
            exit()
        if x:
            if x_cand != x[0]:
                print(0)
                exit()
        else:
            x.append(x_cand)

if x:
    print(1)
else:
    print(max(0, minus_min - 1 - (-min(plus_min, 0) + 1) + 1))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)

def dfs(v):
  checked[v]=1
  for u,s in g[v]:
    if checked[u]==1:
      continue
    vl,vr=lrs[v]
    if vl==1 and vr==0:
      lrs[u]=(1,0)
      continue
    ul=s-vr
    if ul<=0:
      ul=1
    ur=s-vl
    if ur<=0:
      ur=0
    lrs[u]=(ul,ur)
    dfs(u)

n,m=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n+1)]
for u,v,s in arr:
  g[u].append((v,s))
  g[v].append((u,s))
lrs=[(0,0) for _ in range(n+1)]
lrs[1]=(1,g[1][0][1]-1)
checked=[0]*(n+1)
dfs(1)
for u,v,s in arr:
  ul,ur=lrs[u]
  vl,vr=lrs[v]
  nul=s-vr
  if nul<=0:
    nul=1
  nur=s-vl
  if nur<=0:
    nur=0
  nvl=s-ur
  if nvl<=0:
    nvl=1
  nvr=s-ul
  if nvr<=0:
    nvr=0
  lrs[u]=(max(ul,nul),min(ur,nur))
  lrs[v]=(max(vl,nvl),min(vr,nvr))
ans=10**18
for l,r in lrs:
  if l==0 and r==0:
    continue
  ans=min(ans,(r-l+1))
if ans>0:
  print(ans)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)

def dfs(v):
  checked[v]=1
  for u,s in g[v]:
    if checked[u]==1:
      continue
    vl,vr=lrs[v]
    if vl==1 and vr==0:
      lrs[u]=(1,0)
      continue
    ul=s-vr
    if ul<=0:
      ul=1
    ur=s-vl
    if ur<=0:
      ur=0
    lrs[u]=(ul,ur)
    dfs(u)

n,m=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n+1)]
for u,v,s in arr:
  g[u].append((v,s))
  g[v].append((u,s))
lrs=[(0,0) for _ in range(n+1)]
lrs[1]=(1,g[1][0][1]-1)
checked=[0]*(n+1)
dfs(1)
print(lrs)
for u,v,s in arr:
  ul,ur=lrs[u]
  vl,vr=lrs[v]
  nul=s-vr
  if nul<=0:
    nul=1
  nur=s-vl
  if nur<=0:
    nur=0
  nvl=s-ur
  if nvl<=0:
    nvl=1
  nvr=s-ul
  if nvr<=0:
    nvr=0
  lrs[u]=(max(ul,nul),min(ur,nur))
  lrs[v]=(max(vl,nvl),min(vr,nvr))
print(lrs)
ans=10**18
for l,r in lrs:
  if l==0 and r==0:
    continue
  ans=min(ans,(r-l+1))
if ans>0:
  print(ans)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)

def dfs(v):
  checked[v]=1
  for u,s in g[v]:
    if checked[u]==1:
      continue
    vl,vr=lrs[v]
    ul=s-vr
    if ul<=0:
      ul=1
    ur=s-vl
    if ur<=0:
      ur=0
    lrs[u]=(ul,ur)
    dfs(u)

n,m=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n+1)]
for u,v,s in arr:
  g[u].append((v,s))
  g[v].append((u,s))
lrs=[(0,0) for _ in range(n+1)]
lrs[1]=(1,g[1][0][1]-1)
checked=[0]*(n+1)
dfs(1)
for u,v,s in arr:
  ul,ur=lrs[u]
  vl,vr=lrs[v]
  nul=s-vr
  if nul<=0:
    nul=1
  nur=s-vl
  if nur<=0:
    nur=0
  nvl=s-ur
  if nvl<=0:
    nvl=1
  nvr=s-ul
  if nvr<=0:
    nvr=0
  lrs[u]=(max(ul,nul),min(ur,nur))
  lrs[v]=(max(vl,nvl),min(vr,nvr))
ans=10**18
for l,r in lrs:
  if l==0 and r==0:
    continue
  ans=min(ans,(r-l+1))
if ans>0:
  print(ans)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter, deque
def inpl(): return list(map(int, input().split()))

N, M = inpl()
D = defaultdict(list)
for _ in range(M):
    u, v, s = inpl()
    D[u].append((v, s))
    D[v].append((u, s))

L = [1] * (N+1)
U = [10**9] * (N+1)
searched = [0] * (N+1)
Q = deque([1])

while Q:
    p = Q.popleft()
    for q, s in D[p]:
        updated = False
        if L[q] < s - U[p]:
            L[q] = s - U[p]
            updated = True
        if U[q] > s - L[p]:
            U[q] = s - L[p]
            updated = True
        if updated:#not searched[q]:
            searched[q] = True
            Q.append(q)

print(max(min([u-l+1 for u, l in zip(U, L)]), 0))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter, deque
def inpl(): return list(map(int, input().split()))

N, M = inpl()
D = defaultdict(list)
for _ in range(M):
    u, v, s = inpl()
    D[u].append((v, s))
    D[v].append((u, s))

L = [1] * (N+1)
U = [10**9] * (N+1)
searched = [0] * (N+1)
Q = deque([1])

while Q:
    p = Q.pop()
    for q, s in D[p]:
        updated = False
        if L[q] < s - U[p]:
            L[q] = s - U[p]
            updated = True
        if U[q] > s - L[p]:
            U[q] = s - L[p]
            updated = True
        if not searched[q]:
            searched[q] = True
            Q.append(q)

print(max(min([u-l+1 for u, l in zip(U, L)]), 0))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter, deque
def inpl(): return list(map(int, input().split()))

N, M = inpl()
D = defaultdict(list)
for _ in range(M):
    u, v, s = inpl()
    D[u].append((v, s))
    D[v].append((u, s))

L = [1] * (N+1)
U = [10**9] * (N+1)
Q = deque([1])

while Q:
    p = Q.pop()
    for q, s in D[p]:
        updated = False
        if L[q] < s - U[p]:
            L[q] = s - U[p]
            updated = True
        if U[q] > s - L[p]:
            U[q] = s - L[p]
            updated = True
        if updated:
            Q.append(q)

print(max(min([u-l+1 for u, l in zip(U, L)]), 0))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
def input():
    return sys.stdin.readline()[:-1]
n, m = map(int, input().split())
cyc = [0 for _ in range(n)]
num = [10**30 for _ in range(n)]
adj = [[] for _ in range(n)]
used = [False for _ in range(m)]
for i in range(m):
    u, v, s = map(int, input().split())
    adj[u-1].append([v-1, s, i])
    adj[v-1].append([u-1, s, i])

revised_count = 0 #奇数ループで調整したかどうか
#revesed_2 = False
novo = -1
vera = -10**30

def dfs(x):
    global revised_count, novo, vera
    for v, s, e in adj[x]:
        #print(x, v, s, e)
        if used[e]:
            #print("hoge0")
            continue
        elif num[v] > 10**20:
            cyc[v] = cyc[x]+1
            num[v] = s-num[x]
            used[e] = True
            dfs(v)
        elif num[v]+num[x] == s:
            used[e] = True
            continue
        elif (cyc[v]-cyc[x])%2 == 1:
            print(0)
            #print("hoge1")
            sys.exit()
        elif revised_count > 0:
            print(0)
            #print(num)
            #print("hoge2")
            sys.exit()
        elif (s-num[x]+num[v])%2 == 1:
            print(0)
            #print("hoge3")
            #print(s, num[x], num[v])
            sys.exit()
        else:
            novo = v
            vera = (s-num[x]+num[v])//2
            #revised = True
            revised_count += 1
            used[e] = True
    return

num[0] = 0
dfs(0)

if revised_count > 1:
    print(0)
    sys.exit()
elif revised_count == 1:
    #print("revised")
    #revised = False
    #revesed_2 = True
    cyc = [0 for _ in range(n)]
    num = [10**30 for _ in range(n)]
    num[novo] = vera
    cyc[novo] = 0
    used = [False for _ in range(n)]
    dfs(novo)
    for i in range(n):
        if num[i] < 0:
            print(0)
            sys.exit()
    print(1)
else:
    even = min([num[i] for i in range(n) if cyc[i]%2 == 0])
    odd = min([num[i] for i in range(n) if cyc[i]%2 == 1])
    print(max(0, even+odd-1))

Submission status: Wrong Answer

Solution code:
from math import ceil,floor
N,M=map(int,input().split())
Graph=[[] for i in range(N)]
for i in range(M):
  a,b,s=map(int,input().split())
  Graph[a-1].append((b-1,s))
  Graph[b-1].append((a-1,s))
inf=float("inf")  
node=[(0,0) for i in range(N)]
visited=[False]*N
node[0],visited[0]=(1,0),True
L,R=1,inf
stack=[0]
while stack:
  n=stack.pop()
  x,y=node[n]
  visited[n]=True
  for i,s in Graph[n]:
    if visited[i]:
      continue
    p,q=node[i]
    stack.append(i)
    if p:
      if p+x==0:
        if y+q!=s:
          print(0)
          exit()
      elif (s-y-q)%(p+x) or (not L<=(s-y-q)/(p+x)<=R):
        print(-1)
      else:
        L=R=(s-y-q)//(p+x)
    else:
      p,q=-x,s-y
      if p>0:
        L=max(L,floor(-q/p)+1)
      else:
        R=min(R,ceil(-q/p)-1)
      node[i]=(-x,s-y)
print(max(R-L+1,0))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**6) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,m = li()

INF = float("inf")
graph = [[] for _ in range(n)]

for _ in range(m):
    u,v,s = li()
    u -= 1
    v -= 1
    graph[u].append((v, s))
    graph[v].append((u, s))
    
a = [INF]*n
b = [INF]*n
a[0] = 1
b[0] = 0

def dfs(graph, cur, x):
    for nex, edge in graph[cur]:
        if a[nex] == INF:
            a[nex] = -a[cur]
            b[nex] = edge - b[cur]
            
            res, x = dfs(graph, nex, x)
            
            if not res:
                return False, None
            
        else:
            atemp = -a[cur]
            btemp = edge - b[cur]
            
            if atemp == a[nex]:
                if btemp != b[nex]:
                    return False, None
                
            else:
                if x is not None:
                    if (atemp * x + btemp != a[nex] * x + b[nex])\
                    or (atemp * x + btemp) <= 0:
                        return False, None
                    
                else:
                    if (btemp-b[nex])%(a[nex]-atemp) > 0\
                    or (btemp-b[nex])//(a[nex]-atemp) <= 0:
                        return False, None
                    
                    else:
                        x = (btemp-b[nex])//(a[nex]-atemp)

    return True, x

def find_range(a, b):
    upper = INF
    lower = -INF
    for ai, bi in zip(a,b):
        if ai < 0:
            upper = min(upper, bi)
        else:
            lower = max(lower, -bi)
            
    return max(0, upper-lower-1)

res, x = dfs(graph, 0, None)

if not res:
    print(0)
    
elif x is not None:
    print(1)
    
else:
    print(find_range(a,b))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    u, v, s = map(int, input().split())
    u -= 1
    v -= 1
    edges[u].append((v, s))
    edges[v].append((u, s))

visited = [False] * n
visited[0] = True
mem = [[] for _ in range(n)]
mem[0].append((0, False))
stack = []
stack.append((0, 0, False))
while stack:
    node, score, turn = stack.pop()
    for to, s in edges[node]:
        if not visited[to]:
            visited[to] = True
            mem[to].append((s - score, not turn))
            stack.append((to, s - score, not turn))
        else:
            mem[to].append((s - score, not turn))

h, l = 10 ** 20, 10 ** 20
decide = None
for lst in mem:
    trues = set()
    falses = set()
    for v, f in lst:
        if f:
            trues.add(v)
        else:
            falses.add(v)
    if len(trues) >= 2 or len(falses) >= 2:
        print(0)
        break

    elif len(trues) == len(falses) == 1:
        _a = list(trues)[0] - list(falses)[0]
        if _a >= 2 and _a % 2 == 0:
            a = _a // 2
            if decide == None:
                decide = a
            elif decide != a:
                print(0)
                break

        else:
            print(0)
            break

    elif len(trues) == 1:
        h = min(h, list(trues)[0])
    elif len(falses) == 1:
        l = min(l, list(falses)[0])

else:
    if decide != None:
        print(1)
    else:
        if l <= 0:
            x = h + l - 1
        else:
            x = h - 1
        if x > 0:
            print(x)
        else:
            print(0)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    u, v, s = map(int, input().split())
    u -= 1
    v -= 1
    edges[u].append((v, s))
    edges[v].append((u, s))

visited = [False] * n
visited[0] = True
mem = [[] for _ in range(n)]
mem[0].append((0, False))
stack = []
stack.append((0, 0, False))
while stack:
    node, score, turn = stack.pop()
    for to, s in edges[node]:
        if not visited[to]:
            visited[to] = True
            mem[to].append((s - score, not turn))
            stack.append((to, s - score, not turn))
        else:
            mem[to].append((s - score, not turn))

h, l = 10 ** 20, 10 ** 20
decide = None
for lst in mem:
    trues = set()
    falses = set()
    for v, f in lst:
        if f:
            trues.add(v)
        else:
            falses.add(v)
    if len(trues) >= 2 or len(falses) >= 2:
        print(0)
        break

    elif len(trues) == len(falses) == 1:
        _a = list(trues)[0] - list(falses)[0]
        if _a >= 2 and _a % 2 == 0:
            a = _a // 2
            if decide == None:
                decide = a
            elif decide != a:
                print(0)
                break

        else:
            print(0)
            break

    elif len(trues) == 1:
        h = min(h, list(trues)[0])
    elif len(falses) == 1:
        l = min(l, list(falses)[0])

else:
    if decide != None:
        print(1)
    else:
        print(l, h)
        if l <= 0:
            x = h + l - 1
        else:
            x = h + l - 2
        if x > 0:
            print(x)
        else:
            print(0)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    u, v, s = map(int, input().split())
    u -= 1
    v -= 1
    edges[u].append((v, s))
    edges[v].append((u, s))

visited = [False] * n
visited[0] = True
mem = [[] for _ in range(n)]
mem[0].append((0, False))
stack = []
stack.append((0, 0, False))
while stack:
    node, score, turn = stack.pop()
    for to, s in edges[node]:
        if not visited[to]:
            visited[to] = True
            mem[to].append((s - score, not turn))
            stack.append((to, s - score, not turn))
        else:
            mem[to].append((s - score, not turn))
h, l = 10 ** 20, 10 ** 20
decide = None
for lst in mem:
    trues = set()
    falses = set()
    for v, f in lst:
        if f:
            trues.add(v)
        else:
            falses.add(v)
    if len(trues) >= 2 or len(falses) >= 2:
        print(0)
        break

    elif len(trues) == len(falses) == 1:
        _a = list(trues)[0] - list(falses)[0]
        if _a >= 2 and _a % 2 == 0:
            a = _a // 2
            if decide == None:
                decide = a
            elif decide != a:
                print(0)
                break

        else:
            print(0)
            break

    elif len(trues) == 1:
        h = min(h, list(trues)[0])
    elif len(falses) == 1:
        l = min(l, list(falses)[0])

else:
    if decide != None:
        print(1)
    else:
        if l >= 0:
            x = h + l - 1
        else:
            x = h + l
        if x > 0:
            print(x)
        else:
            print(0)