#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
using namespace std;
struct DSU {
struct N {
int p, sz = 1, c = 0, s = 0;
N() {}
};
vector<N> d;
DSU(int n) {
d = vector<N>(n);
for(int i = 0; i < n; i++) d[i].p = i;
}
int find(int i) {
if(d[i].p == i) return i;
int temp = find(d[i].p);
d[i].c -= d[d[i].p].c;
d[i].s != d[d[i].p].s;
d[i].p = temp;
return d[i].p;
}
bool join(int i, int j, int k) {
int x = find(i), y = find(j);
if(x == y) return true;
if(d[x].sz < d[y].sz) {
swap(x, y);
swap(i, j);
}
d[x].sz += d[y].sz;
d[y].p = x;
d[y].c += (d[j].s ? -1 : 1) * (k - d[i].c - d[j].c);
d[y].s = !d[i].s != d[j].s;
return false;
}
};
struct E {
int a, b, c;
E(int ai, int bi, int ci):a(ai),b(bi),c(ci){}
E(){}
};
signed main() {
cout << fixed << setprecision(8);
int n, m, a, b, c;
cin >> n >> m;
DSU d(n);
vector<E> edges;
for(int i = 0; i < m; i++) {
cin >> a >> b >> c;
if(d.join(--a, --b, c)) edges.push_back(E(a, b, c));
}
vector<double> res(n, INT_MAX);
bool ok = true;
for(auto [a, b, c] : edges) {
if(d.d[a].s == d.d[b].s) res[d.find(a)] = (d.d[a].s ? -1.f : 1.f) * (c - d.d[a].c - d.d[b].c) / 2.f;
else if(c - d.d[a].c - d.d[b].c) {
ok = false;
break;
}
}
if(!ok) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
map<int,pii> mp;
for(int i = 0; i < n; i++) {
a = d.find(i);
if(res[a] == INT_MAX) {
if(d.d[i].s) mp[a].ss++;
else mp[a].ff++;
}
}
for(auto i : mp) {
if(i.ss.ff < i.ss.ss) res[i.ff] = 1;
else res[i.ff] = 0;
}
for(int i = 0; i < n; i++) {
a = d.find(i);
res[i] = (d.d[i].s ? -1.f : 1.f) * res[a] + d.d[i].c;
cout << res[i] << " ";
}
cout << "\n";
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |