이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 mn = -n, mx = 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;
}
컴파일 시 표준 에러 (stderr) 메시지
Graph.cpp: In function 'int main()':
Graph.cpp:21:7: warning: unused variable 'b' [-Wunused-variable]
21 | bool b = 1;
| ^
# | 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... |