We need to determine if there exists an assignment of integer coordinates x_i (0 <= x_i <= 10^9) such that for each given relation (L,R,D): x_R - x_L = D.
This is a classic constraint satisfaction problem: we have difference constraints. Since the constraints are all of the form x_R - x_L = D, we can treat them as a system of linear equations over integers. But also we have the domain constraints that x_i must be between 0 and 10^9 (inclusive) and integer. However, the problem doesn't ask for specific values, just whether any assignment exists that satisfies all constraints. Usually, if the system of equations is consistent (no contradictions) and we can assign x_i accordingly, we can shift all coordinates to satisfy the bounds? Let's check: the bounds are 0 <= x_i <= 10^9. They are not automatically satisfied if the system is consistent; we need to check if there exists a solution within these bounds.
But is it necessary to check bounds? The problem statement says: "For every i, x_i is an integer between 0 and 10^9 (inclusive)." That is a condition of the problem; we need to decide if there exists a set of values (x_1,...,x_N) that are within that range and satisfy the constraints.
Thus we must consider the domain. However, many solutions online for AtCoder ABC 087 D (or similar) typically ignore the 0..10^9 bounds because if a solution exists, we can shift all coordinates to meet the bounds if the graph is connected? Let's recall the AtCoder problem "People on a Line" (ABC 087 D). Indeed that problem has the same statement. I've solved it before. The typical solution is to use DFS or union-find with distance to check consistency, and they don't explicitly check bounds because if there is a solution, we can shift all positions to be within [0,10^9]? Actually, let's verify: In the AtCoder problem, the constraints are the same, and the accepted solutions simply check for contradictions in the distances (i.e., whether there is a cycle with sum != 0). They don't check the range 0..10^9. So either the bounds are automatically satisfied, or the problem's official solution might ignore them because they are not restrictive: if there is any integer solution, we can adjust by adding a constant to all x_i to bring them into the range [0,10^9] because the constraints are only relative differences. However, adding a constant changes each x_i by same amount, preserving differences. But we also have the condition that x_i must be between 0 and 10^9 inclusive. If we have a solution, we can shift it by a constant to bring the minimal value to 0, resulting in all x_i >= 0. Then we need to ensure that the maximum is <= 10^9. If after shifting the max is > 10^9, we cannot scale, only shift. So if the range of values (max - min) is > 10^9, then no solution can satisfy the bounds because shifting only changes min and max by same amount, preserving range. So we need to check that max - min <= 10^9. But is that possible? The coordinates are integer and constraints have D up to 10000, N up to 100000. The differences between any two nodes in a component are determined by sum of D along paths. The maximum possible difference within a component could be up to (N-1)10000 ~ 10^9 (since N=100k, that product is 1e9). Actually 9999910000 = 999,990,000 < 1e9, so it might be exactly 1e9 if we have a chain of length 100000 with D=10000 each step, the total difference would be 99999*10000 = 999,990,000, which is less than 10^9 (1,000,000,000). So the range could be as high as about 10^9, but not exceed 10^9? Let's compute max: N=100000, maximum number of edges in a component can be up to N-1 if it's a tree. The sum of D along a path can be at most (N-1)10000 = 9999910000 = 999,990,000, which is less than 1e9. So if the component is a tree, the maximum distance between any two nodes is at most that. However, if there are cycles, distances are constrained by the equations, and may not exceed that. But could there be a component where the max distance is larger than 1e9? Possibly if multiple paths combine? The distance between two nodes is determined by the constraints, but if there are multiple paths, they must be consistent; so the actual distance is fixed. So the maximum absolute difference within a component is fixed. Since each D <= 10000 and the number of edges in any simple path is at most N-1, the maximum possible absolute difference is at most (N-1)*10000, which is under 1e9. Therefore, if we shift so that the minimum becomes 0, the maximum becomes the range (max - min). Since that range <= (N-1)*10000 < 1e9, we can always shift such that all coordinates are in [0, 1e9]? Wait, (N-1)*10000 = 999,990,000, which is less than 1e9. So if we set min = 0, then max = range <= 999,990,000 < 1e9. So the max is <= 1e9. Actually 999,990,000 < 1,000,000,000, so it's within bounds. However, what if the range equals exactly 1,000,000,000? Since 1e9 is allowed inclusive. (N-1)*10000 = 999,990,000 is the maximum possible if we have N-1 edges each with D=10000. That's less than 1e9, so range < 1e9 always. But wait, we could have multiple edges between same nodes? The problem says no duplicate edges (both directions considered distinct? Actually they say: If i ≠ j, then (L_i, R_i) ≠ (L_j, R_j) and (L_i, R_i) ≠ (R_j, L_j). So each unordered pair can have at most one directed edge (either direction) but not both. So the graph is simple in terms of directed edges, but we can treat it as undirected with signed differences. Anyway, the maximum simple path length in terms of number of edges is at most N-1, because there are N nodes, and if the graph is a tree. However, we could have a component where nodes are connected by many edges, but any path that determines the difference between two nodes will be some path; there could be multiple paths but they must be consistent. The longest possible difference between any two nodes would be the maximum distance in the weighted graph when we interpret edges with direction. Since constraints are of the form x_R - x_L = D, we can assign each edge a direction and weight. For any two nodes, the difference is determined by following any path (since it's consistent). The maximum difference magnitude could be larger than (N-1)*10000 if there is a cycle that yields a large difference? But cycles must satisfy sum=0, so they don't add extra possibilities. The actual value of x_i is determined up to an additive constant per connected component. If we choose the additive constant, we can set the minimum value in the component to 0. The maximum value will be the maximum distance from that reference node. The distances from a reference node are determined by the graph; they are sums of D along paths. Since each edge contributes at most 10000, and the maximum number of edges in any simple path from reference to any node is at most N-1 (since no cycles would increase distance because if there is a cycle, the distance to a node is determined by the unique path? Actually if the graph has cycles, the distances to nodes might be defined via multiple paths but they must be consistent, so the distances are still uniquely determined given a root; the value at each node can be computed via any path, but if the graph is not a tree, we can still pick a spanning tree; the distances computed along the tree edges will be the actual coordinates because any other path yields the same value. So the coordinate differences are the sums of weights along any path. Therefore, the maximum distance from the root is bounded by the maximum sum of absolute weights along a simple path. But weights can be negative if we traverse opposite direction? Actually edges have a direction; if we traverse from L to R, we add D; if from R to L, we subtract D. So the magnitude of contribution per edge when traversed in either direction could be up to 10000. So the maximum absolute difference between two nodes could be as high as the sum of absolute values of weights along a path, but is that bounded? Since each edge has D_i between 0 and 10000, when traversed in the forward direction it's +D_i, in the reverse it's -D_i. So the absolute contribution per edge is D_i which is <= 10000. So the maximum absolute difference between any two nodes is at most the sum of D_i along a simple path, which is at most (N-1)10000. So indeed, range <= 999,990,000 < 1e9. Therefore, shifting to make min=0 ensures all coordinates are in [0, 1e9] because max = range <= 999,990,000 < 1e9. Actually careful: The range could be exactly 1e9? Suppose N=100001? No N max is 100000, so max edges in a simple path is 99999. 9999910000 = 999,990,000, which is 10,000,000 less than 1e9. So indeed it's less. So if we shift such that min = 0, max <= 999,990,000, which is <= 1e9, so condition satisfied.
But wait: there is also the lower bound 0. Could the computed coordinates be negative? Yes, initially they could be. But we can add a constant to make min = 0. This preserves differences. Then all coordinates become non-negative. As argued, max after shifting is min+range = range, and range < 1e9, so they are also <= 1e9. So any solution that satisfies the equations can be shifted to satisfy the bounds. Therefore, we don't need to explicitly check bounds; existence of any real/integer solution to the equations is sufficient. However, we must also ensure that coordinates are integers. Since all D_i are integers, if we assign an integer to one node in a component, all others become integers. So integer condition is satisfied.
Thus the problem reduces to checking consistency of equations x_R - x_L = D_i.
We can treat it as a graph with directed edges from L to R with weight D, and also implicitly from R to L with weight -D if we want undirected traversal. We need to check if there's a contradiction. Use DFS or union-find with distances.
Standard solution: Use union-find with potential (also called weighted union-find). For each node, we maintain its relative distance from its set's representative. When merging two nodes with constraint x_v - x_u = D (assuming edge (u,v) with direction u->v weight D), we find roots ru, rv, and if they are same, check that the current difference (dist[v] - dist[u]) equals D; otherwise, merge by setting parent[rv] = ru and adjusting the potential.
Alternatively, do BFS/DFS: assign x to an arbitrary node in each component as 0, then propagate using the constraints, and if conflict arises, output No.
Given M up to 200,000, N up to 100,000, both approaches are fine.
I'll implement union-find with potential because it's clean and ensures O(alpha(N)) per operation.
Let's design:
We have parent array size N+1 (1-indexed). Also an array diff where diff[i] is the weight from i to its parent: that is, if parent[i] = p, then we maintain that x_i = x_p + diff[i]. Actually typical weighted union-find: we maintain relation: x_i = x_root + weight[i] (or weight from root). There are two common ways. I'll define an array d where d[i] is the distance from i to its parent (the parent's value minus child's? Need to be consistent). Let's define: If parent[i] = p, then we have x_i + d[i] = x_p? Or x_i + diff[i] = x_p? Actually let's define so that we can easily compute relation between nodes.
Standard approach: Maintain an array "pot" such that pot[i] is the difference from the root: x_i = x_root + pot[i] (or x_root - pot[i]? Let's pick). Simpler: define w[i] as the weight from i to parent: x[parent[i]] = x[i] + w[i] (so w[i] is the difference from i to its parent: parent minus i). Then we can compute x relative to root. When we compress path, we adjust.
Alternatively, define diff[i] as x[i] - x[parent[i]]. I'd prefer that: let diff[i] = x[i] - x[parent[i]]. Then if we have root r, diff[r] = 0 (or self). Then to get x[i] relative to root: x[i] = x[parent[i]] + diff[i]; recursively we can compute x[i] = x[r] + sum of diffs along path.
But then when we do find with path compression, we need to update diff.
Common in AtCoder's union-find with weight: They often maintain par and weight arrays, where weight[i] is the distance from i to its parent in the direction: x[par[i]] = x[i] + weight[i]. I'll adopt that to be consistent with typical code. Let's define:
- parent[i] = the parent of i.
- weight[i] = the value such that: x[parent[i]] - x[i] = weight[i]. i.e., x[parent[i]] = x[i] + weight[i].
Then when we find root, we accumulate the total difference from i to root.
Implementation:
int find(int x) {
if (parent[x] == x) return x;
int r = find(parent[x]);
weight[x] += weight[parent[x]]; // because we need x to root: initially weight[x] gives difference from x to its old parent. After recursion, parent[x] now points to root, and weight[parent[x]] gives difference from old parent to root. So to get difference from x to root: weight[x] (x to old parent) + weight[old parent] to root. So we add.
parent[x] = r;
return r;
}
After find(x), we have weight[x] = x_root - x? Actually careful: weight[x] defined as x[parent[x]] - x[x]. If we want the difference from root to x, we need to accumulate. Let's compute:
Let p = parent[x], w = weight[x] => x[p] = x[x] + w.
After recursion, we have parent[p] updated to root, and weight[p] = x[root] - x[p]. Then we want weight[x] to become x[root] - x[x]. So new weight[x] = (x[root] - x[p]) + (x[p] - x[x]) = weight[p] + w. So we add weight[p] to weight[x]. So the update is weight[x] += weight[parent[x]] (after parent[x] is updated). That matches.
Thus after find(x), we have: x[root] = x[x] + weight[x].
Now, to handle a constraint: given L, R, D: x[R] - x[L] = D.
We need to check consistency. Using the union-find, we can represent the relationship between L and R.
Let rL = find(L), rR = find(R).
If rL == rR: then we have computed weight[L] and weight[R] such that:
x[rL] = x[L] + weight[L]
x[rR] = x[R] + weight[R] but since same root, x[rL] = x[rR]. So from these equations, we can express x[R] - x[L] = weight[L] - weight[R] (because x[L] = x[root] - weight[L]? Wait let's derive properly.
We have: weight[L] after find: x[root] = x[L] + weight[L] => x[L] = x[root] - weight[L].
Similarly, x[R] = x[root] - weight[R].
Thus x[R] - x[L] = (x[root] - weight[R]) - (x[root] - weight[L]) = weight[L] - weight[R].
We want this to equal D. So check if weight[L] - weight[R] == D. If not, contradiction.
If rL != rR: we need to merge the sets. We need to attach one root under the other and set its weight such that the condition holds.
Suppose we set parent[rR] = rL. We need to determine what weight[rR] should be such that the constraint x[R] - x[L] = D is satisfied with the new relationship.
We have current relations: from rR to R: after find(R), we have x[rR] = x[R] + weight[R] -> x[R] = x[rR] - weight[R]? Wait careful: Our definition: weight[i] is such that x[parent[i]] = x[i] + weight[i]. So after find, for node R, we have parent[R] maybe root but after find, we have weight[R] such that x[root_R] = x[R] + weight[R]. So x[R] = x[root_R] - weight[R]. Similarly, x[L] = x[root_L] - weight[L].
We want to set parent[rR] = rL, and assign weight[rR] (the weight from rR to its new parent rL) such that the condition holds.
Let’s denote w = weight[rR] (to be set) meaning: x[rL] = x[rR] + w.
We want x[R] - x[L] = D.
Substitute x[R] and x[L]:
x[R] = x[rR] - weight[R]
x[L] = x[rL] - weight[L]
So x[R] - x[L] = (x[rR] - weight[R]) - (x[rL] - weight[L]) = (x[rR] - x[rL]) - weight[R] + weight[L].
We want this equal D.
But we will set x[rL] = x[rR] + w => x[rR] - x[rL] = -w.
Thus x[R] - x[L] = -w - weight[R] + weight[L] = D.
Therefore, w = weight[L] - weight[R] - D.
Thus we set parent[rR] = rL and weight[rR] = weight[L] - weight[R] - D.
But we need to verify sign. Let's do alternative derivation with simpler relation:
We can think in terms of x[root] being a base. If we attach rR under rL, we want that after merging, the relationship between L and R is satisfied.
We have: x[L] = X - weight[L] where X = x[rL] (since root = rL)
and x[R] = Y - weight[R] where Y = x[rR].
When we set rR's parent to rL, we need to define weight[rR] such that Y = x[rR] relates to X: we want that x[rL] = x[rR] + weight[rR] -> X = Y + w, i.e., w = X - Y.
Now the condition: x[R] - x[L] = D => (Y - weight[R]) - (X - weight[L]) = D => Y - weight[R] - X + weight[L] = D => (Y - X) + weight[L] - weight[R] = D => -(X - Y) + weight[L] - weight[R] = D => -w + weight[L] - weight[R] = D => w = weight[L] - weight[R] - D. Same result.
So weight[rR] = weight[L] - weight[R] - D.
Check: If we later query, it will work.
Alternatively, we could attach the other way: attach rL under rR, then compute appropriate weight. Usually we union by size or rank. So we should maintain size and attach smaller to larger.
Let's define size array.
When we attach rR to rL, we set parent[rR] = rL, weight[rR] = weight[L] - weight[R] - D.
But we also need to ensure that we use the appropriate order: If we attach rL to rR, the formula would be different. So we should compute based on which root becomes parent.
We'll do:
int rx = find(L), ry = find(R);
if (rx != ry) {
// attach ry under rx maybe
// But we need to compute weight[ry] = ?
}
But we need to decide direction. Let's define we always attach ry under rx (i.e., set parent[ry] = rx). Then we need to set weight[ry] such that constraint holds.
We have:
- For node L: after find, weight[L] gives x[rx] = x[L] + weight[L].
- For node R: after find, weight[R] gives x[ry] = x[R] + weight[R].
We'll set parent[ry] = rx, and assign weight[ry] = ? such that after union the relation holds.
We want: x[R] - x[L] = D.
Using x[R] = x[ry] - weight[R] (since x[ry] = x[R] + weight[R] => x[R] = x[ry] - weight[R]).
And x[L] = x[rx] - weight[L].
We have: after union, x[ry] is related to x[rx] by: x[rx] = x[ry] + weight[ry] (since weight[ry] defined as x[parent[ry]] - x[ry]? Actually weight[ry] would be defined as difference from ry to its parent (rx). So x[rx] = x[ry] + weight[ry]. So weight[ry] = x[rx] - x[ry].
Now compute:
x[R] - x[L] = (x[ry] - weight[R]) - (x[rx] - weight[L]) = (x[ry] - x[rx]) - weight[R] + weight[L] = -weight[ry] - weight[R] + weight[L].
Set = D => -weight[ry] - weight[R] + weight[L] = D => weight[ry] = weight[L] - weight[R] - D.
Thus formula is as before.
That's correct.
Now, if we instead attach rx under ry, we would need a different formula: weight[rx] = weight[R] - weight[L] + D? Let's derive for completeness: if we set parent[rx] = ry, then weight[rx] = ? such that condition holds. Then x[ry] = x[rx] + weight[rx]. And we want x[R] - x[L] = D. Starting from x[R] = x[ry] - weight[R]; x[L] = x[rx] - weight[L]. Then x[R] - x[L] = (x[ry] - weight[R]) - (x[rx] - weight[L]) = (x[ry] - x[rx]) - weight[R] + weight[L]. But x[ry] - x[rx] = -weight[rx] (since x[ry] = x[rx] + weight[rx] => x[ry] - x[rx] = weight[rx], actually careful: if parent[rx] = ry, then weight[rx] is defined as x[parent[rx]] - x[rx] = x[ry] - x[rx]. So x[ry] - x[rx] = weight[rx]. So x[R] - x[L] = weight[rx] - weight[R] + weight[L]. Set = D => weight[rx] = D + weight[R] - weight[L].
Thus both are possible. In our union by size, we can attach smaller to larger, and compute weight accordingly.
Now, need to initialize: parent[i]=i, weight[i]=0, size[i]=1.
Implementation details:
- Use 0-index or 1-index? Nodes 1..N. Use arrays of length N+1.
Find with path compression:
function find(x):
if parent[x] != x:
p = parent[x];
root = find(p);
weight[x] += weight[p]; // because weight[p] now relative to root after recursion
parent[x] = root;
return parent[x];
But careful: In recursion, after find(p) returns root, weight[p] is updated to weight from p to root. Then we need to update weight[x] to weight from x to root: originally weight[x] gave x[parent[x]] - x[x] (with parent[x] maybe p). After p has weight[p] giving x[root] - x[p], so total from x to root: (x[p] - x[x]) + (x[root] - x[p]) = weight[x] + weight[p]. So we add weight[p] to weight[x].
But if we do weight[x] += weight[parent[x]]; after parent[x] is updated? In code:
int find(int x) {
if (parent[x] == x) return x;
int r = find(parent[x]);
weight[x] += weight[parent[x]];
parent[x] = r;
return r;
}
This works because after find(parent[x]) returns, parent[parent[x]] might have been updated to root, and weight[parent[x]] has been updated to represent distance from old parent to root. So weight[x] gets added.
Now, the union operation:
void unite(int L, int R, int D) {
int rL = find(L);
int rR = find(R);
if (rL == rR) {
// check
if (weight[L] - weight[R] != D) ok = false;
return;
}
// attach smaller to larger
if (size[rL] < size[rR]) {
// attach rL under rR. Need to compute weight[rL]
// We have constraint x[R] - x[L] = D.
// If we set parent[rL] = rR, then weight[rL] = ? as derived: weight[rL] = D + weight[R] - weight[L].
// because x[R] - x[L] = D => weight[rL] = D + weight[R] - weight[L].
// Let's verify with our definitions: after find, we have:
// For L: x[L] = x[rL] - weight[L] (since x[rL] = x[L] + weight[L])
// For R: x[R] = x[rR] - weight[R].
// If we set parent[rL] = rR, we need weight[rL] such that: x[rR] = x[rL] + weight[rL] (since parent of rL is rR, so weight[rL] = x[parent] - x[self] = x[rR] - x[rL]).
// Then we want x[R] - x[L] = D.
// Compute: x[R] - x[L] = (x[rR] - weight[R]) - (x[rL] - weight[L]) = (x[rR] - x[rL]) - weight[R] + weight[L] = weight[rL] - weight[R] + weight[L].
// Set = D => weight[rL] = D + weight[R] - weight[L].
// So correct.
parent[rL] = rR;
weight[rL] = D + weight[R] - weight[L];
size[rR] += size[rL];
} else {
// attach rR under rL
// weight[rR] = weight[L] - weight[R] - D (as derived earlier)
parent[rR] = rL;
weight[rR] = weight[L] - weight[R] - D;
size[rL] += size[rR];
}
}
Double-check signs: In else branch, we derived weight[rR] = weight[L] - weight[R] - D. That matches earlier.
But careful: In else, we are attaching rR under rL. Then after union, we need to ensure that find operations later correctly compute distances. Yes.
Now, after processing all constraints, if no contradiction found, output "Yes", else "No".
Edge cases: M=0 -> always Yes.
Let's test with given examples.
Example 1:
N=3, edges: (1,2,1), (2,3,1), (1,3,2)
Initialize: parent[1]=1,weight[1]=0, similarly 2,3.
Process (1,2,1):
find(1)=1,w[1]=0; find(2)=2,w[2]=0; sizes: both 1, say else branch (size 1 >=1) attach 2 under 1: parent[2]=1, weight[2]= weight[1] - weight[2] - D = 0 - 0 - 1 = -1. But check: weight[2] =? According to definition, weight[2] is x[parent[2]] - x[2] = x[1] - x[2]. That equals? We have constraint x2 - x1 = 1 => x2 = x1 + 1 => x1 - x2 = -1. So weight[2] = -1, correct. So after union: size[1]=2.
Process (2,3,1):
find(2): first find parent[2]=1, parent[1]=1 => returns 1; weight[2] updates: currently weight[2] = -1, parent[2]=1. Call find(1) returns 1, weight[1]=0. Then weight[2] += weight[parent[2]] = weight[1] = 0 => weight[2] = -1+0=-1; parent[2]=1. So find(2) returns 1 with weight[2] = -1 meaning x[1] = x[2] + weight[2]? Actually after find, weight[2] should represent distance from 2 to root (1): x[1] = x[2] + weight[2] -> weight[2] = x[1] - x[2] = -1, correct.
find(3)=3, weight[3]=0.
Now rL = 1, rR = 3. sizes: size[1]=2, size[3]=1, so else? Actually size[1]=2 >= size[3]=1, so we go to else branch (attach rR=3 under rL=1). weight[3] will be set: weight[3] = weight[2]? Wait we need weight[L] and weight[R] from the nodes L=2, R=3. But careful: In union, we have L=2, R=3. We have computed wL = weight[2] = -1 (after find), wR = weight[3] = 0. D=1.
In else branch (attach rR under rL), we set weight[rR] = weight[L] - weight[R] - D = (-1) - 0 - 1 = -2. So parent[3]=1, weight[3]= -2. Let's verify: after union, we should have x3 - x2 = 1. Using our definitions: x[1] unknown. We have from earlier: weight[2] = -1 => x1 = x2 -1? Actually weight[2] = -1 means x[parent[2]] - x[2] = x1 - x2 = -1 => x1 = x2 -1 => x2 = x1 +1. weight[3] = -2 => x1 - x3 = -2 => x1 = x3 -2 => x3 = x1 +2. So x3 - x2 = (x1+2)-(x1+1)=1, correct.
Now process (1,3,2):
find(1): root 1, weight[1]=0.
find(3): parent[3]=1, weight[3] = -2 (current). find(3): parent[3]!=1? parent[3]=1, so find(1) returns 1, weight[3] += weight[parent[3]] = weight[1] =0 => weight[3] = -2, parent[3]=1. So weight[3] = -2. So weight[1] =0, weight[3] = -2. Check condition: weight[1] - weight[3] should equal D? Actually we derived check: weight[L] - weight[R] == D? For L=1,R=3, D=2. weight[1]=0, weight[3]= -2 => 0 - (-2) = 2, which equals D. So consistent. Output Yes.
Example 2:
(1,2,1) (2,3,1) (1,3,5)
First two same as above. After first two, we have x1 (unknown), x2 = x1+1, x3 = x1+2. Then third: x3 - x1 = 2, but D=5 => inconsistency. Let's simulate: after first two, as above, weight[1]=0, weight[3] = -2? Wait after (2,3,1), weight[3] = -2? Actually weight[3] = -2? Let's compute again: after (1,2,1): weight[2] = -1. After (2,3,1): we computed weight[3] = -2. So for (1,3,2) we got check weight[1] - weight[3] = 0 - (-2)=2. For D=5, it would be false. So No.
Example 3:
4 3
2 1 1
2 3 5
3 4 2
Interpret: (2,1,1): x1 - x2 = 1? Actually x1 - x2 = 1? Constraint: Person R is to the right of Person L: x_R - x_L = D. Here L=2,R=1? So R=1, L=2 => x1 - x2 = D=1 => x1 = x2 +1. So x1 is right of x2 by 1.
(2,3,5): x3 - x2 =5 => x3 = x2+5.
(3,4,2): x4 - x3 =2 => x4 = x3+2 = x2+7.
Check consistency: no contradictions. Our code should handle.
Let's test with union-find:
Initialize.
Edge (2,1,1): L=2,R=1,D=1. find(2)=2, find(1)=1. Attach? sizes equal. Suppose we attach rR (root of 1) under rL (root of 2) or else branch? Let's decide by size: both size 1, we can go either. Our code: if size[rL] < size[rR] then attach rL under rR else attach rR under rL. So with sizes equal, else branch attaches rR under rL. So attach root 1 under root 2. Compute weight[1] = weight[L] - weight[R] - D? Wait careful: we are in else branch because size[rL] (size[2]=1) is not less than size[1]=1, so else executes (attach rR under rL). rL=2, rR=1, L=2,R=1. weight[L] = weight[2]=0, weight[R]=weight[1]=0. So weight[rR] (weight[1]) = weight[L] - weight[R] - D = 0 - 0 - 1 = -1. So parent[1]=2, weight[1] = -1. That means? After union, we have x[2] = x[1] + weight[1]? Since weight[1] defined as x[parent[1]] - x[1] = x[2] - x[1] = -1? Actually weight[1] = -1 means x[2] - x[1] = -1 => x[1] - x[2] = 1, which matches constraint x1 - x2 =1. Good.
Now (2,3,5): L=2,R=3,D=5. find(2): parent[2]=2 => root 2, weight[2]=0. find(3): root 3, weight[3]=0. Now sizes: size[2]=2 (since 2's component includes 1,2), size[3]=1. So else? size[rL]=2 >= size[rR]=1 => else branch attaches rR (3) under rL (2). Compute weight[3] = weight[L] - weight[R] - D = weight[2] - weight[3] -5 = 0 - 0 -5 = -5. So parent[3]=2, weight[3] = -5. Check: constraint x3 - x2 =5 => x2 - x3 = -5, so weight[3] = x[2] - x[3] = -5, correct.
Now (3,4,2): L=3,R=4,D=2. find(3): parent[3]=2, weight[3] = -5. Path compression: find(2) returns 2, weight[3] += weight[parent[3]]? weight[2]=0 => weight[3] stays -5, parent[3]=2. So root rL=2, weight[3] = -5. find(4): root 4, weight[4]=0. Now sizes: size[2]=3, size[4]=1 => else? size[2]>=1 so else branch attaches rR=4 under rL=2. Compute weight[4] = weight[L] - weight[R] - D = weight[3] - weight[4] - 2 = (-5) - 0 - 2 = -7. So parent[4]=2, weight[4]=-7. Constraint: x4 - x3 =2 => x3 = x4 -2? Actually we have x4 - x3 = 2 => x3 - x4 = -2. Our derived: weight[4] = x[2] - x[4]? Since parent[4]=2, weight[4] = x[2] - x[4]. With weight[3] = x[2] - x[3] = -5 => x[3] = x[2] +5? Wait x[2] - x[3] = -5 => x[3] = x[2] +5. And weight[4] = -7 => x[2] - x[4] = -7 => x[4] = x[2] +7. So x4 - x3 = (x2+7)-(x2+5)=2, correct.
No contradictions, output Yes.
Example 4:
10 3
8 7 100
7 9 100
9 8 100
Edges: (8,7,100): x7 - x8 =100? Actually R=7, L=8 => x7 - x8 =100 => x7 = x8+100.
(7,9,100): x9 - x7 =100 => x9 = x7+100 = x8+200.
(9,8,100): x8 - x9 =100 => x8 - (x8+200) = -200 = 100? Contradiction. So output No.
Check union-find:
Process (8,7,100): L=8,R=7,D=100. find(8)=8, find(7)=7. sizes equal -> else attach rR=7 under rL=8. weight[7] = weight[8] - weight[7] - D = 0-0-100 = -100. So parent[7]=8, weight[7]=-100 (meaning x8 - x7 = -100 => x7 = x8+100). Good.
(7,9,100): L=7,R=9,D=100. find(7): parent[7]=8, weight[7]=-100. find(8): root 8, weight[8]=0. So after find(7): recursively find(8) returns 8, then weight[7] += weight[parent[7]]? weight[parent[7]] = weight[8] = 0 => weight[7] stays -100, parent[7]=8. So rL=8, weight[7] = -100. find(9)=9 weight 0. sizes: size[8]=2 (nodes 8,7), size[9]=1. else attach rR=9 under rL=8. weight[9] = weight[L] - weight[R] - D = weight[7] - weight[9] - 100 = -100 -0 -100 = -200. So parent[9]=8, weight[9] = -200. That yields: weight[9] = x8 - x9 = -200 => x9 = x8 +200.
(9,8,100): L=9,R=8,D=100. find(9): parent[9]=8, weight[9] = -200. find(8): root 8, weight[8]=0. After find: weight[9] += weight[parent[9]]? parent[9] is 8, weight[8]=0 => weight[9] = -200; root rL=8. find(8)=8, rR=8. So both have same root. Check: weight[L] - weight[R] = weight[9] - weight[8] = -200 - 0 = -200. But D=100, not equal, so contradiction -> No.
Works.
Example 5: N=100 M=0 -> Yes.
Thus algorithm seems correct.
Now, about domain bounds: As argued, we don't need to check because we can shift. However, we must consider that the coordinates must be integers. Our solution uses integer differences, so integers are fine.
But wait: The bound is 0 <= x_i <= 10^9. Suppose we have a component with all nodes having fixed relative positions. The absolute coordinates are determined up to an additive constant. We can choose the constant such that all x_i are within [0,10^9]. Since the range of values (max-min) is less than 10^9, we can set the constant = -min, resulting in coordinates from 0 to range <= 999,990,000 < 1e9, so they are within bounds. But what if range equals exactly 1e9? Could that happen? Let's compute maximum possible range: maximum absolute difference between any two nodes in a component is at most the sum of D_i along a simple path. Each D_i <= 10000, and there can be at most N-1 edges in a simple path. N-1 = 99,999? Actually N up to 100,000, so N-1 = 99,999. 99,999 * 10,000 = 999,990,000. So indeed less than 1,000,000,000. So range <= 999,990,000 < 1e9. So we can always shift to fit within [0, 10^9]. But we must also ensure that coordinates are integer; they are.
Thus we can ignore bounds.
Implementation details: Input up to 200k lines, so use fast IO.
We'll implement UnionFind class with arrays: parent, weight, size (or rank). N up to 100k, so arrays of ints fine. weight may need to hold values up to sum of D's, which can be up to ~1e9, fits in 32-bit signed? 100k * 10000 = 1e9, fits in int (2^31-1 = 2.147e9). But could be negative also, but magnitude similar. So use int. However, in union find, weight can accumulate sums; but the maximum absolute value might be larger due to adding and subtracting, but still bounded by total sum of D's along any path, which is at most 1e9. So int is safe.
But there is possibility of overflow if we do weight[L] - weight[R] - D etc. These values are within same bounds, so safe.
Implementation steps:
- Read N, M.
- Create UnionFind uf(N+1) (or N, indexing 1..N)
- For i in 1..M: read L,R,D.
- Call uf.unite(L,R,D) which also checks consistency. Keep a boolean flag ok = true.
- If any conflict, set ok false.
- After processing all, if ok print "Yes", else "No".
Now, need to ensure that find updates weight correctly. In find, we should use recursion or iterative. Recursion depth could be up to N (100k) which might cause stack overflow in some environments if not deep recursion? But typical recursion depth in Python may be limited; but this solution is in C++. Since we're writing in C++, recursion is okay as depth up to 100k could cause stack overflow if not using iterative. But we can implement find iteratively with path compression, or use recursion carefully with compiler optimizations? Usually, union-find recursion depth can be large, but path compression makes tree almost flat, but worst-case before compression could be deep if we union sequentially without balancing. However, union by size/rank keeps tree depth O(log N). Recursion depth of ~log N is fine. But if we don't use union by size, depth could be O(N). But we are using union by size, so depth is at most log N, ~17 for 100k, safe. Even if it's a bit more, recursion depth of a few hundred is fine. So recursion is safe.
Alternatively, iterative is also fine.
Let's implement find as:
int find(int x) {
if (parent[x] == x) return x;
int root = find(parent[x]);
weight[x] += weight[parent[x]];
parent[x] = root;
return root;
}
But careful: weight[parent[x]] after recursion might have changed? In recursion, we call find(parent[x]) which updates parent[parent[x]] and weight[parent[x]]. So after it returns, weight[parent[x]] is the weight from the old parent to root. So then we add that to weight[x] and set parent[x]=root. That's standard.
We need to make sure we compute weight[parent[x]] after parent[x] might have changed. In code, we could store original parent:
int p = parent[x];
int r = find(p);
weight[x] += weight[p]; // since weight[p] is now weight from p to root? Actually after find(p), weight[p] has been updated to be the weight from p to root. But we need to add weight from p to root, which is weight[p] after find. However, if we do weight[x] += weight[parent[x]] after parent[x] is set to root? Let's see typical code:
int find(int x) {
if (par[x] == x) return x;
int r = find(par[x]);
dist[x] += dist[par[x]];
par[x] = r;
return r;
}
But this uses dist[par[x]] after recursion but before updating par[x]? Actually after find(par[x]), par[par[x]] has been updated to root. But the variable par[x] still points to the old parent (which is now with updated dist). So dist[par[x]] is the updated distance from old parent to root. So dist[x] += dist[par[x]] is correct. Then set par[x] = r.
Yes.
We must be careful not to use an intermediate variable that changes. In code:
int find(int x) {
if (parent[x] == x) return x;
int p = find(parent[x]);
weight[x] += weight[parent[x]];
parent[x] = p;
return p;
}
But this uses weight[parent[x]] after parent[x] might have been changed by the recursive call? Actually parent[x] is not changed until after the recursion, because the recursive call modifies parent[parent[x]] but not parent[x] itself. So parent[x] still holds the old parent index. And after the recursive call, weight[parent[x]] has been updated to be the distance from that old parent to the root. So then we add to weight[x] and set parent[x] to root. This works.
Alternatively:
int find(int x) {
if (parent[x] != x) {
int root = find(parent[x]);
weight[x] += weight[parent[x]];
parent[x] = root;
}
return parent[x];
}
That's fine.
Now union:
void unite(int u, int v, int d) {
int ru = find(u);
int rv = find(v);
if (ru == rv) {
// check consistency
if (weight[u] - weight[v] != d) ok = false;
return;
}
// attach smaller to larger
if (size[ru] < size[rv]) {
// attach ru under rv
// Need to set weight[ru] such that condition holds.
// For nodes u and v: we have ru (root of u) will become child of rv.
// We need to set weight[ru] so that after union, x[v] - x[u] = d? Actually constraint: x[v] - x[u] = d (since L=u,R=v? In our call unite(L,R,D) we have L,R. But careful: the constraint is x_R - x_L = D. So we are passing L,R,D. So we have u = L, v = R. So we need to ensure that after union, x[v] - x[u] = D.
// With ru as u's root, rv as v's root.
// If we attach ru under rv, then weight[ru] = ? We derived earlier: if we set parent[ru] = rv, then weight[ru] = D + weight[v] - weight[u]?
// Let's derive again for this case.
// We have: after find, we have:
// x[u] = x[ru] - weight[u]? Actually we have: weight[i] defined such that x[parent[i]] = x[i] + weight[i]. So for u, after find, parent[u] = ru (maybe ru is root, but if ru is root, then parent[u] = ru? Actually after find, u's parent may be ru, but if ru is root, parent[u] may be ru. The relation is: for any node i, after find, we have that x[parent[i]] = x[i] + weight[i], and parent[i] is the root. So x[root] = x[i] + weight[i] => x[i] = x[root] - weight[i].
// So we have:
// x[u] = x[ru] - weight[u]
// x[v] = x[rv] - weight[v]
// If we set parent[ru] = rv, then we define weight[ru] such that: x[rv] = x[ru] + weight[ru] (since weight[ru] = x[parent[ru]] - x[ru] = x[rv] - x[ru]).
// So weight[ru] = x[rv] - x[ru].
// We want x[v] - x[u] = D.
// Substitute: (x[rv] - weight[v]) - (x[ru] - weight[u]) = (x[rv] - x[ru]) - weight[v] + weight[u] = weight[ru] - weight[v] + weight[u] = D.
// => weight[ru] = D + weight[v] - weight[u].
// So when attaching ru under rv, weight[ru] = D + weight[v] - weight[u].
parent[ru] = rv;
weight[ru] = D + weight[v] - weight[u];
size[rv] += size[ru];
} else {
// attach rv under ru
parent[rv] = ru;
weight[rv] = weight[u] - weight[v] - D;
size[ru] += size[rv];
}
}
But we need to be careful: In the else branch we attached rv under ru, and we used weight[rv] = weight[u] - weight[v] - D, which matches earlier derivation for attaching rR under rL where L=u, R=v. In that derivation we had rL = ru, rR = rv, and we set weight[rv] = weight[u] - weight[v] - D. That's correct.
Now for the case where size[ru] < size[rv], we attach ru under rv. Then we need to set weight[ru] = D + weight[v] - weight[u]. So that's symmetrical.
Now check with previous examples using this new union method.
Let's test example 1 with this union method:
Initialize sizes.
(1,2,1): ru=1, rv=2, size[1]=1, size[2]=1 -> else? else branch (since size[ru] >= size[rv])? Actually size[ru] = size[1] =1, size[rv] = size[2] =1, not less, so we go to else: attach rv under ru. So parent[2]=1, weight[2] = weight[1] - weight[2] - D = 0 - 0 -1 = -1. That's same as before.
(2,3,1): find(2) -> root=1, weight[2] = -1 (since weight[2] updates? We'll compute). After find(2): parent[2]=1, weight[2] = -1. find(1) returns 1, weight[1]=0, so weight[2] remains -1. So ru for u=2 is 1, weight[u]=-1? Actually after find(2), we have root ru = 1 and weight[2] = -1. find(3) -> root=3, weight[3]=0, rv=3. Now sizes: size[1]=2, size[3]=1. Since size[ru] (2) >= size[rv] (1), else branch attaches rv under ru. So parent[3]=1, weight[3] = weight[u] - weight[v] - D = weight[2] - weight[3] - 1 = -1 - 0 -1 = -2. That's same as before.
(1,3,2): find(1)=1 weight[1]=0; find(3): parent[3]=1, weight[3] = -2; after find, root rv=1, weight[3] stays -