#include<iostream> #include<cstring> #include<vector> #include<queue> #define int long long usingnamespace std; int T; int n, m, c[2005], f[2005], dis[2005], vis[2005], cnt[2005];
boolspfa(){ memset(dis, 0x3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); memset(cnt, 0, sizeof(cnt)); while (!q.empty()) q.pop(); dis[0] = 0; vis[0] = 1; cnt[0] = 0; q.push(0); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for (Edge nxt : g[u]) { int v = nxt.v; int w = nxt.w; if (dis[v] > dis[u] + w) { dis[v] = dis[u] + w; if (!vis[v]) { cnt[v]++; if (cnt[v] > n) return1; vis[v] = 1; q.push(v); } } } } return0; }
voidsolve(){ cin >> n >> m; for (int i = 2; i <= n; i++) { cin >> f[i]; g[0].push_back({i, 0}); } for (int i = 1; i <= m; i++) { int op; cin >> op; if (op == 1) { int a, l, r; cin >> a >> l >> r; g[a].push_back({f[a], r}); g[f[a]].push_back({a, -l}); } else { int x, y, z; cin >> x >> y >> z; g[x].push_back({f[y], z}); g[f[y]].push_back({x, -z}); } } if (spfa()) cout << "No\n"; else cout << "Yes\n"; for (int i = 0; i <= n; i++) g[i].clear(); }
signedmain(){ cin >> T; while (T--) { solve(); } return0; } /* c[u] - c[f[u]] <= r c[u] - c[f[u]] >= l c[x] - c[f[y]] <= z c[x] - c[f[y]] >= z
dis_v - dis_u <= w c[u] - c[f[u]] <= r c[f[u]] - c[u] <= -l c[x] - c[f[y]] <= z c[f[y]] - c[x] <= -z