#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
int N, M;
vvpii adj;
vi vis;
vpii poly;
vi newNodes;
vector<int> ans;
bool possible = true;
bool xdefined = false;
int x = 0;
void dfs(int v)
{
vis[v] = true;
newNodes.push_back(v);
int u, c;
for (pii e : adj[v])
{
tie(u, c) = e;
if (vis[u])
{
// check whether cohrerent possible
pii res = {poly[v].first + poly[u].first, poly[v].second + poly[u].second};
if (res.first == c && res.second == 0)
{
// perfect, nothing to do
}
else if (xdefined)
{
if (res.first + res.second * x - c != 0)
possible = false;
}
else
{
// not x defined
if (res.second == 0)
possible = false;
else
{
xdefined = true;
assert((c - res.first) % res.second == 0);
x = (c - res.first) / res.second;
}
}
}
else
{ // new Node
poly[u] = {c - poly[v].first, -poly[v].second};
dfs(u);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
adj.resize(N);
for (int i = 0, a, b, c; i < M; ++i)
{
cin >> a >> b >> c;
--a, --b, c *= 2;
adj[a].push_back({b, c}), adj[b].push_back({a, c});
}
vis.assign(N, false);
ans.resize(N);
poly.resize(N);
for (int v = 0; v < N; ++v)
if (!vis[v])
{
xdefined = false;
x = 0;
poly[v] = {0, 1};
newNodes.clear();
dfs(v);
if (!xdefined)
{
int l = INT_MAX, r = INT_MIN;
for (int w : newNodes)
{
if (poly[w].second != 0)
{
int xcand = -poly[w].first / poly[w].second;
l = min(l, xcand), r = max(r, xcand);
}
}
x = 0;
ll minAns = 1LL << 60;
while (l < r)
{
int m = (l + r) / 2;
ll e1 = 0, e2 = 0;
for (int u : newNodes)
{
e1 += (ll)abs(poly[u].first + poly[u].second * m);
e2 += (ll)abs(poly[u].first + poly[u].second * (m + 1));
}
if (e1 < minAns)
x = m, minAns = e1;
if (e2 < minAns)
x = m + 1, minAns = e2;
if (e1 < e2)
r = m;
else if (e1 > e2)
l = m + 1;
else
l = m, r = m;
}
}
for (int u : newNodes)
ans[u] = poly[u].first + poly[u].second * x;
}
if (possible)
{
cout << "YES\n";
for (int v = 0; v < N; ++v)
cout << ans[v] / (double)2 << " ";
cout << "\n";
}
else
{
cout << "NO\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
answer = YES |
2 |
Correct |
0 ms |
212 KB |
answer = YES |
3 |
Correct |
0 ms |
212 KB |
answer = YES |
4 |
Correct |
0 ms |
212 KB |
answer = NO |
5 |
Correct |
1 ms |
212 KB |
answer = YES |
6 |
Correct |
1 ms |
212 KB |
answer = YES |
7 |
Correct |
1 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
0 ms |
212 KB |
answer = NO |
10 |
Correct |
0 ms |
212 KB |
answer = YES |
11 |
Correct |
0 ms |
212 KB |
answer = YES |
12 |
Correct |
0 ms |
212 KB |
answer = NO |
13 |
Correct |
0 ms |
212 KB |
answer = YES |
14 |
Correct |
0 ms |
212 KB |
answer = YES |
15 |
Correct |
1 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
1 ms |
212 KB |
answer = YES |
18 |
Correct |
0 ms |
212 KB |
answer = YES |
19 |
Correct |
0 ms |
212 KB |
answer = YES |
20 |
Correct |
0 ms |
212 KB |
answer = YES |
21 |
Correct |
1 ms |
212 KB |
answer = YES |
22 |
Correct |
0 ms |
212 KB |
answer = NO |
23 |
Correct |
0 ms |
212 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
answer = YES |
2 |
Correct |
0 ms |
212 KB |
answer = YES |
3 |
Correct |
0 ms |
212 KB |
answer = YES |
4 |
Correct |
0 ms |
212 KB |
answer = NO |
5 |
Correct |
1 ms |
212 KB |
answer = YES |
6 |
Correct |
1 ms |
212 KB |
answer = YES |
7 |
Correct |
1 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
0 ms |
212 KB |
answer = NO |
10 |
Correct |
0 ms |
212 KB |
answer = YES |
11 |
Correct |
0 ms |
212 KB |
answer = YES |
12 |
Correct |
0 ms |
212 KB |
answer = NO |
13 |
Correct |
0 ms |
212 KB |
answer = YES |
14 |
Correct |
0 ms |
212 KB |
answer = YES |
15 |
Correct |
1 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
1 ms |
212 KB |
answer = YES |
18 |
Correct |
0 ms |
212 KB |
answer = YES |
19 |
Correct |
0 ms |
212 KB |
answer = YES |
20 |
Correct |
0 ms |
212 KB |
answer = YES |
21 |
Correct |
1 ms |
212 KB |
answer = YES |
22 |
Correct |
0 ms |
212 KB |
answer = NO |
23 |
Correct |
0 ms |
212 KB |
answer = NO |
24 |
Correct |
1 ms |
212 KB |
answer = YES |
25 |
Correct |
0 ms |
212 KB |
answer = YES |
26 |
Correct |
1 ms |
212 KB |
answer = YES |
27 |
Correct |
0 ms |
212 KB |
answer = YES |
28 |
Correct |
0 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
212 KB |
answer = YES |
30 |
Correct |
0 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
0 ms |
212 KB |
answer = YES |
33 |
Correct |
0 ms |
212 KB |
answer = YES |
34 |
Correct |
1 ms |
212 KB |
answer = YES |
35 |
Correct |
1 ms |
212 KB |
answer = YES |
36 |
Correct |
1 ms |
212 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
answer = YES |
2 |
Correct |
0 ms |
212 KB |
answer = YES |
3 |
Correct |
0 ms |
212 KB |
answer = YES |
4 |
Correct |
0 ms |
212 KB |
answer = NO |
5 |
Correct |
1 ms |
212 KB |
answer = YES |
6 |
Correct |
1 ms |
212 KB |
answer = YES |
7 |
Correct |
1 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
0 ms |
212 KB |
answer = NO |
10 |
Correct |
0 ms |
212 KB |
answer = YES |
11 |
Correct |
0 ms |
212 KB |
answer = YES |
12 |
Correct |
0 ms |
212 KB |
answer = NO |
13 |
Correct |
0 ms |
212 KB |
answer = YES |
14 |
Correct |
0 ms |
212 KB |
answer = YES |
15 |
Correct |
1 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
1 ms |
212 KB |
answer = YES |
18 |
Correct |
0 ms |
212 KB |
answer = YES |
19 |
Correct |
0 ms |
212 KB |
answer = YES |
20 |
Correct |
0 ms |
212 KB |
answer = YES |
21 |
Correct |
1 ms |
212 KB |
answer = YES |
22 |
Correct |
0 ms |
212 KB |
answer = NO |
23 |
Correct |
0 ms |
212 KB |
answer = NO |
24 |
Correct |
1 ms |
212 KB |
answer = YES |
25 |
Correct |
0 ms |
212 KB |
answer = YES |
26 |
Correct |
1 ms |
212 KB |
answer = YES |
27 |
Correct |
0 ms |
212 KB |
answer = YES |
28 |
Correct |
0 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
212 KB |
answer = YES |
30 |
Correct |
0 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
0 ms |
212 KB |
answer = YES |
33 |
Correct |
0 ms |
212 KB |
answer = YES |
34 |
Correct |
1 ms |
212 KB |
answer = YES |
35 |
Correct |
1 ms |
212 KB |
answer = YES |
36 |
Correct |
1 ms |
212 KB |
answer = YES |
37 |
Execution timed out |
1079 ms |
212 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
answer = YES |
2 |
Correct |
0 ms |
212 KB |
answer = YES |
3 |
Correct |
0 ms |
212 KB |
answer = YES |
4 |
Correct |
0 ms |
212 KB |
answer = NO |
5 |
Correct |
1 ms |
212 KB |
answer = YES |
6 |
Correct |
1 ms |
212 KB |
answer = YES |
7 |
Correct |
1 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
0 ms |
212 KB |
answer = NO |
10 |
Correct |
0 ms |
212 KB |
answer = YES |
11 |
Correct |
0 ms |
212 KB |
answer = YES |
12 |
Correct |
0 ms |
212 KB |
answer = NO |
13 |
Correct |
0 ms |
212 KB |
answer = YES |
14 |
Correct |
0 ms |
212 KB |
answer = YES |
15 |
Correct |
1 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
1 ms |
212 KB |
answer = YES |
18 |
Correct |
0 ms |
212 KB |
answer = YES |
19 |
Correct |
0 ms |
212 KB |
answer = YES |
20 |
Correct |
0 ms |
212 KB |
answer = YES |
21 |
Correct |
1 ms |
212 KB |
answer = YES |
22 |
Correct |
0 ms |
212 KB |
answer = NO |
23 |
Correct |
0 ms |
212 KB |
answer = NO |
24 |
Correct |
1 ms |
212 KB |
answer = YES |
25 |
Correct |
0 ms |
212 KB |
answer = YES |
26 |
Correct |
1 ms |
212 KB |
answer = YES |
27 |
Correct |
0 ms |
212 KB |
answer = YES |
28 |
Correct |
0 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
212 KB |
answer = YES |
30 |
Correct |
0 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
0 ms |
212 KB |
answer = YES |
33 |
Correct |
0 ms |
212 KB |
answer = YES |
34 |
Correct |
1 ms |
212 KB |
answer = YES |
35 |
Correct |
1 ms |
212 KB |
answer = YES |
36 |
Correct |
1 ms |
212 KB |
answer = YES |
37 |
Execution timed out |
1079 ms |
212 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
answer = YES |
2 |
Correct |
0 ms |
212 KB |
answer = YES |
3 |
Correct |
0 ms |
212 KB |
answer = YES |
4 |
Correct |
0 ms |
212 KB |
answer = NO |
5 |
Correct |
1 ms |
212 KB |
answer = YES |
6 |
Correct |
1 ms |
212 KB |
answer = YES |
7 |
Correct |
1 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
0 ms |
212 KB |
answer = NO |
10 |
Correct |
0 ms |
212 KB |
answer = YES |
11 |
Correct |
0 ms |
212 KB |
answer = YES |
12 |
Correct |
0 ms |
212 KB |
answer = NO |
13 |
Correct |
0 ms |
212 KB |
answer = YES |
14 |
Correct |
0 ms |
212 KB |
answer = YES |
15 |
Correct |
1 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
1 ms |
212 KB |
answer = YES |
18 |
Correct |
0 ms |
212 KB |
answer = YES |
19 |
Correct |
0 ms |
212 KB |
answer = YES |
20 |
Correct |
0 ms |
212 KB |
answer = YES |
21 |
Correct |
1 ms |
212 KB |
answer = YES |
22 |
Correct |
0 ms |
212 KB |
answer = NO |
23 |
Correct |
0 ms |
212 KB |
answer = NO |
24 |
Correct |
1 ms |
212 KB |
answer = YES |
25 |
Correct |
0 ms |
212 KB |
answer = YES |
26 |
Correct |
1 ms |
212 KB |
answer = YES |
27 |
Correct |
0 ms |
212 KB |
answer = YES |
28 |
Correct |
0 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
212 KB |
answer = YES |
30 |
Correct |
0 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
0 ms |
212 KB |
answer = YES |
33 |
Correct |
0 ms |
212 KB |
answer = YES |
34 |
Correct |
1 ms |
212 KB |
answer = YES |
35 |
Correct |
1 ms |
212 KB |
answer = YES |
36 |
Correct |
1 ms |
212 KB |
answer = YES |
37 |
Execution timed out |
1079 ms |
212 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |