You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.6 KiB
66 lines
1.6 KiB
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
typedef long long LL;
|
|
const int N = 100010, M = 300010;
|
|
int n, m;
|
|
|
|
int cnt[N];
|
|
LL dist[N];
|
|
bool st[N];
|
|
|
|
int h[N], e[M], w[M], ne[M], idx;
|
|
void add(int a, int b, int c) {
|
|
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
|
|
}
|
|
|
|
bool spfa() {
|
|
queue<int> q;
|
|
for (int i = 1; i <= n; i++) {
|
|
dist[i] = 1;
|
|
st[i] = true;
|
|
q.push(i);
|
|
}
|
|
|
|
while (q.size()) {
|
|
int u = q.front();
|
|
q.pop();
|
|
|
|
st[u] = false;
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
int j = e[i];
|
|
if (dist[j] < dist[u] + w[i]) {
|
|
dist[j] = dist[u] + w[i];
|
|
cnt[j] = cnt[u] + 1;
|
|
if (cnt[j] >= n) return true;
|
|
if (!st[j]) {
|
|
q.push(j);
|
|
st[j] = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
int main() {
|
|
memset(h, -1, sizeof h);
|
|
cin >> n >> m;
|
|
while (m--) {
|
|
int x, a, b;
|
|
cin >> x >> a >> b;
|
|
if (x == 1) add(a, b, 0), add(b, a, 0); // A=B,A>=B,B>=A
|
|
if (x == 2) add(a, b, 1); // A<B,B-1>=A
|
|
if (x == 3) add(b, a, 0); // A>=B,B->A
|
|
if (x == 4) add(b, a, 1); // A>B,A>=B+1,B->A
|
|
if (x == 5) add(a, b, 0); // A<=B,B>=A,A->B
|
|
}
|
|
|
|
if (spfa())
|
|
puts("-1");
|
|
else {
|
|
LL res = 0;
|
|
for (int i = 1; i <= n; i++) res += dist[i];
|
|
printf("%lld\n", res);
|
|
}
|
|
return 0;
|
|
} |