#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<int, 3>
struct dsu{
vector<int> sz, p;
int n;
dsu(int ns){
n = ns;
sz.resize(n + 1);
p.resize(n + 1);
for (int i = 1; i <= n; i++){
p[i] = i;
sz[i] = 1;
}
}
int get(int v){
if (p[v] != v){
p[v] = get(p[v]);
}
return p[v];
}
bool unite(int x, int y){
x = get(x); y = get(y);
if (x == y) return 0;
if (sz[x] > sz[y]) swap(x, y);
p[x] = y;
sz[y] += sz[x];
return 1;
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m; cin>>n>>m;
vector<arr3> ed;
while (m--){
int x, y, w; cin>>x>>y>>w;
ed.pb({x, y, w});
}
vector<pii> g[n + 1];
dsu T(n);
for (auto [x, y, w]: ed){
if (T.unite(x, y)){
g[x].pb({y, w});
g[y].pb({x, w});
}
}
for (int i = 2; i <= n; i++){
if (T.get(i) != T.get(1)){
cout<<"NO"<<"\n";
return 0;
}
}
vector<int> a(n + 1), b(n + 1);
function<void(int, int)> dfs = [&](int v, int pr){
for (auto [i, w]: g[v]){
if (i == pr) continue;
b[i] = w - b[v];
a[i] = -a[v];
dfs(i, v);
}
};
a[1] = 1; b[1] = 0;
dfs(1, 0);
double X = 0; bool ind = 1;
for (auto [x, y, w]: ed){
int p = a[x] + a[y], q = b[x] + b[y];
if (!p){
if (q != w){
cout<<"NO"<<"\n";
return 0;
}
continue;
}
if (ind){
X = 1.0 * (w - q) / p;
ind = 0;
continue;
}
if (X != 1.0 * (w - q) / p){
cout<<"NO"<<"\n";
return 0;
}
}
if (ind){
vector<int> all;
for (int i = 1; i <= n; i++){
all.pb((a[i] == -1) ? b[i] : -b[i]);
}
sort(all.begin(), all.end());
X = all[all.size() / 2];
}
cout<<"YES"<<"\n";
for (int i = 1; i <= n; i++){
cout<<a[i] * X + b[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... |