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>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int32_t, int32_t> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 100010
#define INF (ll)1e9+10
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
int N,M,a,b,c;
bool vis[maxn];
vpi adj[maxn];
ld value;
int status = 0; // status is 0 if not set, 1 if fixed and -1 if no solution
pi val[maxn];
vi v;
ld ans[maxn];
void dfs(int x){
if (status == -1) return;
vis[x] = 1;
v.pb(x);
aFOR(i,adj[x]){
if (vis[i.f]){
pi cur = val[x];
cur.f += val[i.f].f;
cur.s += val[i.f].s;
if (cur.f == 0){
if (cur.s != i.s){
status = -1; return;
}
}else{
ld newvalue = (ld(i.s) - ld(cur.s)) / ld(cur.f);
if (status == 1 && value != newvalue){
status = -1; return;
}
value = newvalue;
status = 1;
}
}else{
val[i.f] = pi(-val[x].f, i.s - val[x].s);
dfs(i.f);
}
}
}
int32_t main(){
fast;
cin >> N >> M;
FOR(i,1,M){
cin >> a >> b >> c;
adj[a].pb(pi(b, c)); adj[b].pb(pi(a,c));
}
FOR(i,1,N) if (!vis[i]){
val[i] = pi(1,0);
status = 0;
v.clear();
dfs(i);
if (status == -1){
cout << "NO"; return 0;
}else if (status == 0){
aFOR(j,v) aFOR(k,adj[j]) assert(val[j].f + val[k.f].f == 0);
vi v2;
aFOR(j,v){
if (val[j].f == -1) v2.pb(val[j].s);
else v2.pb(-val[j].s);
}
sort(all(v2));
value = v2[v2.size()/2];
}
aFOR(j,v) ans[j] = (ld)val[j].f * value + (ld)val[j].s;
//aFOR(j,v) aFOR(k,adj[j]) assert(ans[j] + ans[k.f] == k.s);
}
cout << "YES\n";
FOR(i,1,N) cout << ans[i] << ' ';
}
# | 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... |