#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e4 + 5;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
#endif // LOCAL
int N, M;
cin >> N >> M;
vector<tuple<int, int, int>> edges;
//let pref[i] = number of zeros in prefix 1...i
for(int i = 0; i < M; ++i){
int l, r, k, v;
cin >> l >> r >> k >> v;
++r;
if(v == 0){
//pref[r] - pref[l] >= k <=> pref[l] - pref[r] <= -k
edges.emplace_back(l, r, -k);
} else{
//pref[r] - pref[l] <= k - 1
edges.emplace_back(r, l, k - 1);
}
}
for(int i = 0; i < N; ++i){
//pref[i + 1] - pref[i] <= 1
edges.emplace_back(i + 1, i, 1);
//pref[i + 1] - pref[i] >= 0 <=> pref[i] - pref[i + 1] <= 0
edges.emplace_back(i, i + 1, 0);
}
const int inf = 1e9;
vector<int> f(N + 1, inf);
f[0] = 0;
for(int rep = 0; rep < N; ++rep){
for(auto [u, v, w] : edges){
if(f[v] > f[u] + w){
f[v] = f[u] + w;
}
}
}
for(auto [u, v, w] : edges){
if(f[v] > f[u] + w){ //negative cycle
cout << -1 << '\n';
return 0;
}
}
for(int i = 0; i < N; ++i){
cout << (f[i + 1] - f[i] < 0 ? 0 : 1) << ' ';
}
return 0;
}
/*
Model :
We have a weighted graph G = (V, E)
Where |V| = N + 1, |E| = 2 * N + M
The edge (u, v, w) in the graph represent a constraint that value of a[u] - a[v] <= w
<=> a[u] - w <= a[v] (Look like a shortest path from a start to vertex v ?)
Here a[u] = pref[u] means the number of zeros in 1...u
so which means a[0] = pref[0] = 0
No solution when : exist a negative cycle in the graph
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |