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>
#define pb push_back
#define s second
#define f first
using namespace std;
const int N = 100005;
vector <pair<int,int> > v[N];
pair <int,double> d[N];
vector <int> pr;
int vis[N],q;
double val;
int p[N];
void dfs(int x){
pr.pb(x);
vis[x] = 1;
for (int i = 0; i < v[x].size(); i++){
int to = v[x][i].f,col = v[x][i].s,av = -d[x].f;
double bv = 1.0 - d[x].s;
if (col == 2) bv+=1.0;
if (!vis[to]) {
d[to] = {av,bv};
p[to] = x;
dfs(to);
}else if (av == d[to].f && bv != d[to].s) {
cout<<"NO";
exit(0);
}else if (av == d[to].f && bv == d[to].s){
continue;
}else{
q=1;
val = (d[to].s - bv)/(av - d[to].f);
}
}
}
signed main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n,m;
cin>>n>>m;
for (int i = 1; i <= m; i++){
int a,b,c;
cin>>a>>b>>c;
v[a].pb({b,c});
v[b].pb({a,c});
}
for (int i = 1; i <= n; i++){
if (vis[i]) continue;
pr.clear(); q=0;
d[i] = {1,0.0};
dfs(i);
if (!q){
vector <double> vec; vec.clear();
for (int x: pr)
vec.pb(-d[x].f*d[x].s);
sort(vec.begin(),vec.end());
val = vec[(vec.size())/2];
}
for (int x: pr)
d[x] = {0,val*d[x].f + d[x].s};
bool check = 1;
for (int x: pr)
for (int j = 0; j < v[x].size(); j++){
int to = v[x][j].f,col = v[x][j].s;
if (d[x].s + d[to].s != col) check=0;
}
if (!check) {
cout<<"NO";
exit(0);
}
}
cout<<"YES"<<endl;
for (int i =1; i <= n;i++)
cout<<d[i].s<<" ";
}
Compilation message (stderr)
Graph.cpp: In function 'void dfs(int)':
Graph.cpp:18:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for (int i = 0; i < v[x].size(); i++){
| ~~^~~~~~~~~~~~~
Graph.cpp: In function 'int main()':
Graph.cpp:67:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int j = 0; j < v[x].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... |