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;
/*
- Los valores de los distintos componentes conectados son independientes entre sí.
- Cuando se hace un DFS de un CC(componente conectado), el vértice inicial tiene un valor de x
- Para cada eje (a, b, c)
Si b no está explorado, valor[b] = c - valor[a]
Si b está explorado, pero la expresion c = valor[a] + valor[b] no da
ninguna información adicional, no se hace nada
Si b está explorado, y la expresión da información adicional, se usa para calcular x.
para evitar WA verificar el resultado
*/
vector<int> eje[100001];
vector<int> color[100001];
vector<double> coefx(100001, 0.0);
vector<double> coefc(100001, 0.0);
vector<int> componentes(100001, 0);
vector<double> xval(1);
vector<double> findx;
int comp_cnt = 0;
void dfs(int u){
findx.push_back(coefc[u] * coefx[u] * -1.0);
for(int i = 0; i < eje[u].size(); i++){
int v = eje[u][i];
int c = color[u][i];
if(!componentes[v]){
componentes[v] = comp_cnt;
coefx[v] = -1.0 * coefx[u];
coefc[v] = double(c) - coefc[u];
dfs(v);
}else{
if(coefx[u] + coefx[v] == 0.0) continue;
xval.back() = (double(c) - (coefc[u] + coefc[v]))/(coefx[u] + coefx[v]);
}
}
}
int main(){
int n, m;
cin>>n>>m;
int a, b, c;
for(int i = 1; i <= m; i++){
cin>>a>>b>>c;
eje[a].push_back(b);
color[a].push_back(c);
eje[b].push_back(a);
color[b].push_back(c);
}
for(int i = 1; i <= n; i++){
if(componentes[i]) continue;
comp_cnt++;
componentes[i] = comp_cnt;
coefx[i] = 1.0;
xval.push_back(-9999.9);
findx.clear();
dfs(i);
if(xval.back() == -9999.9){
sort(findx.begin(), findx.end());
xval.back() = findx[findx.size()/2];
}
}
double val[n+1];
for(int i = 1; i <= n; i++){
val[i] = coefx[i] * xval[componentes[i]] + coefc[i];
}
for(int i = 1; i <= n; i++){
for(int j = 0; j < eje[i].size(); j++){
if(val[i] + val[eje[i][j]] != color[i][j]){
cout<<"NO\n";
return 0;
}
}
}
cout<<"YES\n";
for(int i = 1; i <= n; i++){
cout << val[i] << ' ';
}
cout << '\n';
}
Compilation message (stderr)
Graph.cpp: In function 'void dfs(int)':
Graph.cpp:28:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int i = 0; i < eje[u].size(); i++){
| ~~^~~~~~~~~~~~~~~
Graph.cpp: In function 'int main()':
Graph.cpp:78:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int j = 0; j < eje[i].size(); j++){
| ~~^~~~~~~~~~~~~~~
# | 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... |