This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define PB push_back
#define sz(x) ((int)x.size())
#define i2 array<int,2>
using namespace std;
typedef long double ld;
const int oo = 2e9;
const int N = 100100;
const int cnst = 90;
const int con = 50;
queue<int> q;
bool was;
vector<i2> g[N];
int n, m, col[N], mrk[N], tt;
int cost = 0, nm[N];
void BAD(){
cout << "NO";
exit(0);
}
mt19937 rnd(chrono::system_clock().now().time_since_epoch().count());
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n >> m;
for (int i = 0; i < m; i++){
int x, y, z; cin >> x >> y >> z;
x--; y--;
g[x].PB({y, z * 2});
g[y].PB({x, z * 2});
}
for (int i = 0; i < n; i++)
nm[i] = i;
fill(col, col + n, -oo);
// shuffle(nm, nm + n, rnd);
for (int ite = 0; ite < n; ite++){
// int i = nm[ite];
int i = ite;
if (col[i] != -oo) continue;
int best = oo, mem = -oo;
for (int vl = -cnst; vl <= con; vl++){
was = 0;
tt++;
cost = 0;
while (sz(q)) q.pop();
q.push(i);
col[i] = vl;
cost = abs(vl);
mrk[i] = tt;
while (!was && sz(q) && cost < best){
int v = q.front(); q.pop();
for (i2 u : g[v]){
if (mrk[u[0]] == tt){
if (col[u[0]] != u[1] - col[v]){
was = 1;
break;
}
} else {
q.push(u[0]);
mrk[u[0]] = tt;
col[u[0]] = u[1] - col[v];
cost += abs(col[u[0]]);
}
}
}
if (!was && best > cost){
best = cost;
mem = vl;
}
}
if (best == oo) BAD();
tt++;
while (sz(q)) q.pop();
int vl = mem;
q.push(i);
col[i] = vl;
mrk[i] = tt;
while (sz(q)){
int v = q.front(); q.pop();
for (i2 u : g[v]){
if (mrk[u[0]] < tt) {
q.push(u[0]);
mrk[u[0]] = tt;
col[u[0]] = u[1] - col[v];
}
}
}
}
cout << "YES\n";
for (int i = 0; i < n; i++)
cout << fixed << setprecision(10) << ld(col[i]) / 2.0 << " ";
return 0;
}
# | 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... |