Problem p03450 (python)

Fixed submission:
import sys
input = sys.stdin.readline
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)

def main(): 
    n,m = map(int,input().split())
    L = [[] for _ in range(n)]
    R = [[] for _ in range(n)]
    for _ in range(m):
        l,r,d = map(int,input().split())
        l -= 1
        r -= 1
        L[l].append((r,d))
        R[r].append((l,d))
    
    visited = [True] * n
    X = [0] * n

    def dfs(i):
        ans = True
        for r in L[i]:
            j, d = r
            if visited[j]:
                X[j] = X[i] + d
                visited[j] = False
                ans = (ans&dfs(j)) 
            else:
                if X[j] - X[i] != d:
                    ans = False
                    break
        for l in R[i]:
            j,d = l
            if visited[j]:
                X[j] = X[i] - d
                visited[j] = False
                ans = (ans&dfs(j))
            else:
                if X[i] - X[j] != d:
                    ans = False
                    break
        return ans
    
    ans = True
    for i in range(n):
        if visited[i]:
            visited[i] = False
            ans = (ans&dfs(i))
    
    if ans:
        print('Yes')
    else:
        print('No')        
  
if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;

bool dfs(int i, vector<vector<pint>>& L, vector<vector<pint>>& R, vector<bool>& visited , vector<int>& X) {
    bool ans = true;
    for (pint r : L[i]) {
        int j = r.first;
        int d = r.second;
        if (visited[j]) {
            visited[j] = false;
            X[j] = X[i] + d;
            ans = (ans && dfs(j,L,R,visited,X));
        }
        else {
            if (X[j] - X[i] != d) {
                ans = false; break;
            }
        }
    }
    
    for (pint l : R[i]) {
        int j = l.first;
        int d = l.second;
        if (visited[j]) {
            visited[j] = false;
            X[j] = X[i] - d;
            ans = (ans && dfs(j,L,R,visited,X));
        }
        else {
            if (X[i] - X[j] != d) {
                ans = false; break;
            }
        }
    }
    return ans;
}

int main() {
    int n,m; cin >> n >> m;
    vector<vector<pint>> L(n);
    vector<vector<pint>> R(n);

    for (int i = 0; i < m; i ++ ) {
        int l,r,d;
        cin >> l >> r >> d;
        l --; r --;
        L[l].push_back({r,d});
        R[r].push_back({l,d});
    }

    vector<bool> visited(n,true);
    vector<int> X(n,0);
    
    bool ans = true;
    for (int i = 0; i < n; i ++) {
        if (visited[i]) {
            visited[i] = false;
            ans = (ans&&dfs(i,L,R,visited,X));
        }
    }

    if (ans) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque

def main(): 
    n,m = map(int,input().split())
    L = [[] for _ in range(n)]
    R = [[] for _ in range(n)]
    for _ in range(m):
        l,r,d = map(int,input().split())
        l -= 1
        r -= 1
        L[l].append((r,d))
        R[r].append((l,d))
    
    visited = [True] * n
    X = [0] * n

    def dfs(i):
        ans = True
        for r in L[i]:
            j, d = r
            if visited[j]:
                X[j] = X[i] + d
                visited[j] = False
                ans = (ans&dfs(j)) 
            else:
                if X[j] - X[i] != d:
                    ans = False
                    break
        for l in R[i]:
            j,d = l
            if visited[j]:
                X[j] = X[i] - d
                visited[j] = False
                ans = (ans&dfs(j))
            else:
                if X[i] - X[j] != d:
                    ans = False
                    break
        return ans
    
    ans = True
    for i in range(n):
        if visited[i]:
            visited[i] = False
            ans = (ans&dfs(i))
    
    if ans:
        print('Yes')
    else:
        print('No')        
  
if __name__=='__main__':
    main()