#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
using pi = pair<int, int>;
int main() {
int n, M; cin >> n >> M;
vector<vector<pi>> g(n);
for (int i = 0; i < M; i++)
{
int x, y, z; cin >> x >> y >> z; x--; y--;
g[x].push_back({ y, z });
g[y].push_back({ x, z });
}
vector<pi> v(n, { -1 , 0 });
vector<double> r(n, 0.1);
bool b = 1;
for (int i = 0; i < n; i++)
{
if (!v[i].second)
{
double a = 0.1;
v[i] = { 0, 1 };
vector<pi> c;
vector<int> aa(1, i);
queue<int> q; q.push(i);
while (!q.empty() && a == 0.1)
{
int x = q.front(); q.pop();
for (pi p : g[x])
{
int y = p.first;
if (!v[y].second)
{
v[y] = { -v[x].first + p.second,-v[x].second };
c.push_back(v[y]);
aa.push_back(y);
q.push(y);
}
else
{
if (v[y].second != v[x].second)
{
if (v[x].first + v[y].first != p.second)
{
cout << "NO" << endl;
return 0;
}
}
else
{
a = 0.5 * (v[y].first + v[x].first - p.second) * (-v[x].second);
break;
}
}
}
}
if (a != 0.1)
{
while (!q.empty())
{
q.pop();
}
q.push(i);
r[i] = a;
while (!q.empty())
{
int x = q.front(); q.pop();
for (pi p : g[x])
{
int y = p.first;
double rr = 1.0*p.second - r[x];
if (r[y] != 0.1 && r[y] != rr)
{
cout << "NO" << endl;
return 0;
}
if (r[y] == 0.1)
{
r[y] = rr;
q.push(y);
v[y] = { -2, -2 };
}
}
}
}
else
{
/*
int x = 0;
for (pi p : c)
{
x += abs(p.first);
}
int mn = x, i = 0;
for (int m = -2*n; m <= 2*n; m++)
{
x = 0;
for (pi p : c)
{
x += abs(p.first + p.second * m);
}
if (x < mn)
{
mn = x;
i = m;
}
}
*/
int mx = 2 * n, mn = -2 * n;
while (mx - mn > 0)
{
int m = (mx + mn) / 2;
int x = 0;
for (pi p : c)
{
x += abs(p.first + p.second * (m + 1));
}
int y = 0;
for (pi p : c)
{
y += abs(p.first + p.second * m);
}
if (mx - mn == 1)
{
if (x >= y)
{
mx = mn;
}
else
{
mn = mx;
}
}
else
{
if (x >= y)
{
mx = m;
}
else
{
mn = m + 1;
}
}
}
//*/
for (int x : aa)
{
pi p = v[x];
r[x] = p.first + p.second * mn;
}
}
}
}
cout << "YES" << endl;
for (int i = 0; i < n; i++)
{
cout << r[i] << " ";
}
cout << endl;
return 0;
}
Compilation message
Graph.cpp: In function 'int main()':
Graph.cpp:21:7: warning: unused variable 'b' [-Wunused-variable]
21 | bool b = 1;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
answer = YES |
2 |
Correct |
1 ms |
336 KB |
answer = YES |
3 |
Correct |
1 ms |
336 KB |
answer = YES |
4 |
Correct |
1 ms |
336 KB |
answer = NO |
5 |
Incorrect |
1 ms |
336 KB |
participant answer is larger than the answer of jury |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
answer = YES |
2 |
Correct |
1 ms |
336 KB |
answer = YES |
3 |
Correct |
1 ms |
336 KB |
answer = YES |
4 |
Correct |
1 ms |
336 KB |
answer = NO |
5 |
Incorrect |
1 ms |
336 KB |
participant answer is larger than the answer of jury |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
answer = YES |
2 |
Correct |
1 ms |
336 KB |
answer = YES |
3 |
Correct |
1 ms |
336 KB |
answer = YES |
4 |
Correct |
1 ms |
336 KB |
answer = NO |
5 |
Incorrect |
1 ms |
336 KB |
participant answer is larger than the answer of jury |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
answer = YES |
2 |
Correct |
1 ms |
336 KB |
answer = YES |
3 |
Correct |
1 ms |
336 KB |
answer = YES |
4 |
Correct |
1 ms |
336 KB |
answer = NO |
5 |
Incorrect |
1 ms |
336 KB |
participant answer is larger than the answer of jury |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
answer = YES |
2 |
Correct |
1 ms |
336 KB |
answer = YES |
3 |
Correct |
1 ms |
336 KB |
answer = YES |
4 |
Correct |
1 ms |
336 KB |
answer = NO |
5 |
Incorrect |
1 ms |
336 KB |
participant answer is larger than the answer of jury |
6 |
Halted |
0 ms |
0 KB |
- |