#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <stack>
#include <cstdio>
#include <climits>
#include <tuple>
#include <ctime>
#include <cstring>
#include <numeric>
#include <functional>
#include <chrono>
#include <cassert>
#include <bitset>
#include <fstream>
//#include <bit>
//#include <ranges>
//#include <numbers>
#define itn int
#define sacnf scanf
#define sz(a) ((int)((a).size()))
// printf("%.10f\n", ans);
/*ofstream fout("timeline.out");
ifstream fin("timeline.in");*/
using ll = long long;
using namespace std;
const ll mod = 1e9 + 7;
const int limit = 160;
const int N = 2e5 + 1, inf = 1e9;
struct Edge {
int v, w;
};
int n, m, timer;
vector<bool> vis;
vector<int> comp, ans, layer;
vector<vector<Edge>> gp;
void dfs1(int u)
{
layer[u] = timer++;
vis[u] = 1;
comp.push_back(u);
for (Edge& e : gp[u]) if (!vis[e.v]) dfs1(e.v);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
vis = vector<bool>(n);
ans = layer = vector<int>(n);
gp = vector<vector<Edge>>(n);
for (int i = 0; i < m; i++)
{
int u, v, w;
cin >> u >> v >> w;
gp[--u].push_back({ --v, w * 2 });
gp[v].push_back({ u, w * 2 });
}
for (int i = 0; i < n; i++)
{
if (!vis[i])
{
timer = 0;
comp.clear();
dfs1(i);
int mn = inf, num = inf;
int cnt = sz(comp);
for (int val = -limit; val <= limit; val++)
{
//for (int& u : comp) vis[u] = 0;
vector<bool> used(cnt);
queue<int> q;
q.push(i);
used[layer[i]] = 1;
ans[i] = val;
int sum = abs(val);
while (!q.empty())
{
int u = q.front();
q.pop();
for (Edge& e : gp[u])
{
int v = e.v, w = e.w;
if (!used[layer[v]])
{
used[layer[v]] = 1;
ans[v] = w - ans[u];
q.push(v);
if (ans[v] < -limit || ans[v] > limit)
{
sum = inf;
break;
}
sum += abs(ans[v]);
}
else if (ans[u] + ans[v] != w)
{
sum = inf;
break;
}
}
if (sum == inf) break;
}
if (sum < mn) mn = sum, num = val;
}
if (num == inf) return cout << "NO", 0;
//for (int& u : comp) vis[u] = 0;
vector<bool> used(cnt);
queue<int> q;
q.push(i);
used[layer[i]] = 1;
ans[i] = num;
while (!q.empty())
{
int u = q.front();
q.pop();
for (Edge& e : gp[u])
{
int v = e.v, w = e.w;
if (!used[layer[v]])
{
used[layer[v]] = 1;
ans[v] = w - ans[u];
q.push(v);
}
}
}
}
}
cout << "YES\n";
for (int& i : ans)
{
if (i == -1) cout << "-0.5";
else
{
cout << i / 2;
if (abs(i) & 1) cout << ".5";
}
cout << " ";
}
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 |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
1 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
212 KB |
answer = NO |
10 |
Correct |
1 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 |
1 ms |
212 KB |
answer = YES |
15 |
Correct |
1 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
0 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 |
0 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
1 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 |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
1 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
212 KB |
answer = NO |
10 |
Correct |
1 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 |
1 ms |
212 KB |
answer = YES |
15 |
Correct |
1 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
0 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 |
0 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
1 ms |
212 KB |
answer = NO |
24 |
Correct |
1 ms |
212 KB |
answer = YES |
25 |
Correct |
1 ms |
212 KB |
answer = YES |
26 |
Correct |
1 ms |
212 KB |
answer = YES |
27 |
Correct |
1 ms |
212 KB |
answer = YES |
28 |
Correct |
1 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
340 KB |
answer = YES |
30 |
Correct |
1 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
1 ms |
212 KB |
answer = YES |
33 |
Correct |
2 ms |
212 KB |
answer = YES |
34 |
Correct |
2 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 |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
1 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
212 KB |
answer = NO |
10 |
Correct |
1 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 |
1 ms |
212 KB |
answer = YES |
15 |
Correct |
1 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
0 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 |
0 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
1 ms |
212 KB |
answer = NO |
24 |
Correct |
1 ms |
212 KB |
answer = YES |
25 |
Correct |
1 ms |
212 KB |
answer = YES |
26 |
Correct |
1 ms |
212 KB |
answer = YES |
27 |
Correct |
1 ms |
212 KB |
answer = YES |
28 |
Correct |
1 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
340 KB |
answer = YES |
30 |
Correct |
1 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
1 ms |
212 KB |
answer = YES |
33 |
Correct |
2 ms |
212 KB |
answer = YES |
34 |
Correct |
2 ms |
212 KB |
answer = YES |
35 |
Correct |
1 ms |
212 KB |
answer = YES |
36 |
Correct |
1 ms |
212 KB |
answer = YES |
37 |
Correct |
1 ms |
212 KB |
answer = YES |
38 |
Correct |
1 ms |
212 KB |
answer = YES |
39 |
Correct |
4 ms |
340 KB |
answer = YES |
40 |
Correct |
2 ms |
340 KB |
answer = YES |
41 |
Correct |
2 ms |
340 KB |
answer = NO |
42 |
Correct |
6 ms |
400 KB |
answer = YES |
43 |
Correct |
8 ms |
328 KB |
answer = YES |
44 |
Correct |
8 ms |
340 KB |
answer = YES |
45 |
Correct |
5 ms |
328 KB |
answer = YES |
46 |
Correct |
4 ms |
324 KB |
answer = YES |
47 |
Correct |
2 ms |
340 KB |
answer = YES |
48 |
Correct |
6 ms |
340 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 |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
1 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
212 KB |
answer = NO |
10 |
Correct |
1 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 |
1 ms |
212 KB |
answer = YES |
15 |
Correct |
1 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
0 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 |
0 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
1 ms |
212 KB |
answer = NO |
24 |
Correct |
1 ms |
212 KB |
answer = YES |
25 |
Correct |
1 ms |
212 KB |
answer = YES |
26 |
Correct |
1 ms |
212 KB |
answer = YES |
27 |
Correct |
1 ms |
212 KB |
answer = YES |
28 |
Correct |
1 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
340 KB |
answer = YES |
30 |
Correct |
1 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
1 ms |
212 KB |
answer = YES |
33 |
Correct |
2 ms |
212 KB |
answer = YES |
34 |
Correct |
2 ms |
212 KB |
answer = YES |
35 |
Correct |
1 ms |
212 KB |
answer = YES |
36 |
Correct |
1 ms |
212 KB |
answer = YES |
37 |
Correct |
1 ms |
212 KB |
answer = YES |
38 |
Correct |
1 ms |
212 KB |
answer = YES |
39 |
Correct |
4 ms |
340 KB |
answer = YES |
40 |
Correct |
2 ms |
340 KB |
answer = YES |
41 |
Correct |
2 ms |
340 KB |
answer = NO |
42 |
Correct |
6 ms |
400 KB |
answer = YES |
43 |
Correct |
8 ms |
328 KB |
answer = YES |
44 |
Correct |
8 ms |
340 KB |
answer = YES |
45 |
Correct |
5 ms |
328 KB |
answer = YES |
46 |
Correct |
4 ms |
324 KB |
answer = YES |
47 |
Correct |
2 ms |
340 KB |
answer = YES |
48 |
Correct |
6 ms |
340 KB |
answer = YES |
49 |
Correct |
100 ms |
1236 KB |
answer = YES |
50 |
Correct |
13 ms |
1544 KB |
answer = YES |
51 |
Correct |
72 ms |
1512 KB |
answer = YES |
52 |
Correct |
12 ms |
1420 KB |
answer = NO |
53 |
Correct |
8 ms |
384 KB |
answer = YES |
54 |
Correct |
19 ms |
536 KB |
answer = YES |
55 |
Correct |
43 ms |
760 KB |
answer = YES |
56 |
Correct |
94 ms |
1204 KB |
answer = YES |
57 |
Correct |
94 ms |
1128 KB |
answer = YES |
58 |
Correct |
78 ms |
1168 KB |
answer = YES |
59 |
Correct |
36 ms |
1076 KB |
answer = YES |
60 |
Correct |
90 ms |
1180 KB |
answer = YES |
61 |
Correct |
27 ms |
724 KB |
answer = YES |
62 |
Correct |
62 ms |
8036 KB |
answer = NO |
63 |
Correct |
74 ms |
7988 KB |
answer = YES |
64 |
Correct |
54 ms |
7948 KB |
answer = NO |
65 |
Correct |
86 ms |
8052 KB |
answer = YES |
66 |
Correct |
14 ms |
456 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 |
0 ms |
212 KB |
answer = YES |
6 |
Correct |
1 ms |
212 KB |
answer = YES |
7 |
Correct |
0 ms |
212 KB |
answer = YES |
8 |
Correct |
0 ms |
212 KB |
answer = YES |
9 |
Correct |
1 ms |
212 KB |
answer = NO |
10 |
Correct |
1 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 |
1 ms |
212 KB |
answer = YES |
15 |
Correct |
1 ms |
212 KB |
answer = YES |
16 |
Correct |
0 ms |
212 KB |
answer = YES |
17 |
Correct |
0 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 |
0 ms |
212 KB |
answer = YES |
22 |
Correct |
1 ms |
212 KB |
answer = NO |
23 |
Correct |
1 ms |
212 KB |
answer = NO |
24 |
Correct |
1 ms |
212 KB |
answer = YES |
25 |
Correct |
1 ms |
212 KB |
answer = YES |
26 |
Correct |
1 ms |
212 KB |
answer = YES |
27 |
Correct |
1 ms |
212 KB |
answer = YES |
28 |
Correct |
1 ms |
212 KB |
answer = YES |
29 |
Correct |
1 ms |
340 KB |
answer = YES |
30 |
Correct |
1 ms |
212 KB |
answer = NO |
31 |
Correct |
1 ms |
212 KB |
answer = YES |
32 |
Correct |
1 ms |
212 KB |
answer = YES |
33 |
Correct |
2 ms |
212 KB |
answer = YES |
34 |
Correct |
2 ms |
212 KB |
answer = YES |
35 |
Correct |
1 ms |
212 KB |
answer = YES |
36 |
Correct |
1 ms |
212 KB |
answer = YES |
37 |
Correct |
1 ms |
212 KB |
answer = YES |
38 |
Correct |
1 ms |
212 KB |
answer = YES |
39 |
Correct |
4 ms |
340 KB |
answer = YES |
40 |
Correct |
2 ms |
340 KB |
answer = YES |
41 |
Correct |
2 ms |
340 KB |
answer = NO |
42 |
Correct |
6 ms |
400 KB |
answer = YES |
43 |
Correct |
8 ms |
328 KB |
answer = YES |
44 |
Correct |
8 ms |
340 KB |
answer = YES |
45 |
Correct |
5 ms |
328 KB |
answer = YES |
46 |
Correct |
4 ms |
324 KB |
answer = YES |
47 |
Correct |
2 ms |
340 KB |
answer = YES |
48 |
Correct |
6 ms |
340 KB |
answer = YES |
49 |
Correct |
100 ms |
1236 KB |
answer = YES |
50 |
Correct |
13 ms |
1544 KB |
answer = YES |
51 |
Correct |
72 ms |
1512 KB |
answer = YES |
52 |
Correct |
12 ms |
1420 KB |
answer = NO |
53 |
Correct |
8 ms |
384 KB |
answer = YES |
54 |
Correct |
19 ms |
536 KB |
answer = YES |
55 |
Correct |
43 ms |
760 KB |
answer = YES |
56 |
Correct |
94 ms |
1204 KB |
answer = YES |
57 |
Correct |
94 ms |
1128 KB |
answer = YES |
58 |
Correct |
78 ms |
1168 KB |
answer = YES |
59 |
Correct |
36 ms |
1076 KB |
answer = YES |
60 |
Correct |
90 ms |
1180 KB |
answer = YES |
61 |
Correct |
27 ms |
724 KB |
answer = YES |
62 |
Correct |
62 ms |
8036 KB |
answer = NO |
63 |
Correct |
74 ms |
7988 KB |
answer = YES |
64 |
Correct |
54 ms |
7948 KB |
answer = NO |
65 |
Correct |
86 ms |
8052 KB |
answer = YES |
66 |
Correct |
14 ms |
456 KB |
answer = YES |
67 |
Correct |
178 ms |
14968 KB |
answer = YES |
68 |
Correct |
603 ms |
14844 KB |
answer = YES |
69 |
Correct |
597 ms |
14844 KB |
answer = YES |
70 |
Correct |
87 ms |
19348 KB |
answer = YES |
71 |
Correct |
562 ms |
15100 KB |
answer = YES |
72 |
Execution timed out |
1075 ms |
9076 KB |
Time limit exceeded |
73 |
Halted |
0 ms |
0 KB |
- |