# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
725066 | _martynas | Graph (BOI20_graph) | C++11 | 251 ms | 20932 KiB |
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;
#define F first
#define S second
#define PB push_back
#define all(a) (a).begin(), (a).end()
using pii = pair<int, int>;
using vi = vector<int>;
// a*x+b
struct LinearEq {
double a, b;
};
const int MXN = 1e5+5;
const double EPS = 1e-6;
int n, m;
vector<pii> adj[MXN];
bool visited[MXN];
LinearEq leq[MXN];
double X[MXN];
bool bad = false;
double x = -1.1e9;
vi comp;
void dfs(int u) {
comp.PB(u);
for(auto e : adj[u]) {
int v = e.F; double w = e.S;
if(visited[v]) {
if(abs(leq[v].a)<EPS) {
// v is known
double xx = (w-(leq[u].b+leq[v].b))/leq[u].a;
if(x > -1e9 && abs(x-xx) > EPS) bad = true;
x = xx;
continue;
}
if(abs(leq[u].a-leq[v].a)>EPS) {
if(abs(w-(leq[u].b+leq[v].b)) > EPS) {
bad = true;
}
}
else {
double xx = (w-(leq[u].b+leq[v].b))/(leq[u].a+leq[v].a);
if(x > -1e9 && abs(x-xx) > EPS) bad = true;
x = xx;
}
}
else {
leq[v].a = -leq[u].a;
leq[v].b = w-leq[u].b;
visited[v] = true;
dfs(v);
}
}
}
int main() {
cin >> n >> m;
for(int i = 0; i < m; i++) {
int a, b, c; cin >> a >> b >> c;
adj[a].PB({b, c});
adj[b].PB({a, c});
}
for(int i = 1; i <= n; i++) {
sort(all(adj[i]));
for(int j = 1; j < adj[i].size(); j++) {
if(adj[i][j].F == adj[i][j-1].F && adj[i][j].S != adj[i][j-1].S) {
cout << "NO\n";
return 0;
}
if(adj[i][j].F == i) {
visited[i] = true;
X[i] = 0.5*adj[i][j].S;
leq[i].b = X[i];
}
}
}
for(int i = 1; i <= n; i++) {
if(visited[i]) continue;
visited[i] = true;
leq[i] = {1, 0};
x = -1.1e9;
comp.clear();
dfs(i);
if(bad) {
cout << "NO\n";
return 0;
}
if(x < -1e9) {
// if there was at least one known value all of the component would be known
vector<double> vec;
for(int u : comp) {
vec.PB(-leq[u].a*leq[u].b);
}
sort(all(vec));
x = vec[vec.size()/2];
}
for(int u : comp) {
X[u] = leq[u].a*x+leq[u].b;
}
}
cout << "YES\n";
for(int i = 1; i <= n; i++) {
cout << X[i] << " \n"[i == n];
}
return 0;
}
Compilation message (stderr)
# | 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... |