Sure, let's tackle this problem step by step.
Problem Overview
You are given a directed graph with N vertices and M edges. Each edge has a weight, and you need to assign an integer value x_i to each vertex such that for every directed edge from vertex u_j to v_j with weight w_j, the following condition holds:
[
x_{v_j} - x_{u_j} = w_j
]
Additionally, each x_i must be within the range ([-10^{18}, 10^{18}]). It's guaranteed that at least one such assignment exists.
Solution Approach
This problem can be modeled as a system of linear equations where each equation corresponds to an edge in the graph:
[
x_{v_j} = x_{u_j} + w_j
]
Given that the graph can be large (N, M up to (2 \times 10^5)), we need an efficient approach. The key insights are:
- Graph Representation: The problem can be seen as assigning potentials to nodes such that the difference between connected nodes matches the edge weights.
- Consistency and Connectivity: Since a solution is guaranteed to exist, the system of equations is consistent. We can process each connected component independently.
- Assignment Strategy:
- Choose an arbitrary starting node in each connected component and assign it a value (e.g.,
0).
- Propagate the assignments to other nodes using BFS or DFS, adhering to the edge constraints.
Here's a step-by-step strategy:
- Build the Adjacency List: Represent the graph using adjacency lists where each node points to its outgoing edges along with their weights.
- Initialize Assignments: Use an array to store the
x_i values and a boolean array to track which nodes have been assigned.
- Process Each Component:
- For each unassigned node, assign it
0 and perform BFS to assign values to all reachable nodes.
- During BFS, for each edge
(u, v, w), set x_v = x_u + w. If x_v was previously assigned, ensure consistency.
- Output the Assignments: After processing all components, output the
x_i values.
This approach ensures that all nodes are assigned values consistently while adhering to the edge constraints.
C++ Implementation
Below is the C++ implementation of the above approach:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
// Adjacency list: for each node, list of pairs (v, w)
vector<vector<pair<int, ll>>> adj(N+1);
for(int i=0; i<M; ++i){
int u, v;
ll w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
}
// Initialize x and assigned arrays
vector<ll> x(N+1, 0);
vector<bool> assigned(N+1, false);
// BFS queue
queue<int> q;
for(int i=1; i<=N; ++i){
if(!assigned[i]){
x[i] = 0;
assigned[i] = true;
q.push(i);
while(!q.empty()){
int u = q.front();
q.pop();
for(auto &[v, w] : adj[u]){
if(!assigned[v]){
x[v] = x[u] + w;
assigned[v] = true;
q.push(v);
}
else{
// Since a solution exists, we assume consistency
// Uncomment below lines if you want to check consistency
/*
if(x[v] != x[u] + w){
// Inconsistent, but problem guarantees consistency
// Handle if needed
}
*/
}
}
}
}
}
// Output the x[1] to x[N]
for(int i=1; i<=N; ++i){
cout << x[i] << (i == N ? '\n' : ' ');
}
}
Explanation of the Code
-
Input Reading:
- The number of vertices
N and edges M are read first.
- Each edge is read and stored in an adjacency list
adj, where adj[u] contains pairs (v, w) representing an edge from u to v with weight w.
-
Initialization:
x: An array to store the assigned values for each vertex. Initialized to 0.
assigned: A boolean array to keep track of which vertices have been assigned values.
-
Processing Each Component:
- Iterate through each vertex. If it's unassigned, assign it
0 and enqueue it for BFS.
- During BFS, for each outgoing edge
(u, v, w), assign x_v = x_u + w if v is unassigned. If v is already assigned, we skip since the consistency is guaranteed.
-
Output:
- After processing all vertices, print the
x_i values separated by spaces.
Example
Let's consider a simple example to illustrate the solution.
Input
3 3
1 2 10
2 3 20
1 3 30
Explanation
- We can assign
x_1 = 0.
- From edge
1 -> 2 with weight 10, x_2 = x_1 + 10 = 10.
- From edge
2 -> 3 with weight 20, x_3 = x_2 + 20 = 30.
- From edge
1 -> 3 with weight 30, x_3 = x_1 + 30 = 30 (consistent).
Output
0 10 30
This output satisfies all the edge constraints.
Conclusion
The provided BFS-based approach efficiently assigns values to each vertex while satisfying all edge constraints. The implementation ensures that all connected components are processed, and the guarantee of a solution simplifies consistency checks.