#pragma GCC optimize("Ofast,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;
#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()
cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;
template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;
#define SIK { cout<< "NO\n"; exit(0); }
struct num {
ll x, y;
num () : x(0), y(0) {}
num (cl &x, cl &y) : x(x), y(y) {}
friend bool operator == (const num &a, const num &b) { return (a.x == b.x && a.y == b.y); }
friend num operator + (const num &a, const num &b) { return num(a.x + b.x, a.y + b.y); }
friend num operator - (const num &a, const num &b) { return num(a.x - b.x, a.y - b.y); }
friend num operator - (cl &a, const num &b) { return num(-b.x, a - b.y); }
friend double operator & (const num &a, const num &b) {
num t = a - b;
return t.y / double(-t.x);
}
};
cl N = 1e5 + 7;
ll n, m;
double val [N], X, cur, ans;
bool mark [N], seen [N];
vector <pll> adj [N];
vector <ll> vec;
num a [N];
void dfs (ll v) {
seen[v] = 1;
vec.push_back(-(a[v].y * a[v].x));
for (auto [u, w] : adj[v]) {
if (X != inf) return;
if (seen[u]) {
num _ = w - a[v];
if (_ == a[u]) continue;
if (_.x == a[u].x) SIK;
X = _ & a[u];
return;
} else {
a[u] = w - a[v];
dfs(u);
}
}
}
void dfs2 (ll v) {
mark[v] = 1;
cur += val[v];
for (auto [u, w] : adj[v]) {
if (mark[u]) {
if (abs((val[v] + val[u]) - w) > 1e-7) SIK;
} else {
val[u] = w - val[v];
dfs2(u);
}
}
}
int main ()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cout<< fixed << setprecision(7);
cin>> n >> m;
for (ll i = 1, v, u, w; i <= m; i++) {
cin>> v >> u >> w;
adj[v].push_back({u, w});
adj[u].push_back({v, w});
}
for (ll i = 1; i <= n; i++) if (!mark[i]) {
X = inf; a[i].x = 1; cur = 0; vec.clear();
dfs(i);
if (X == inf) {
sort(All(vec));
val[i] = vec[SZ(vec) / 2];
dfs2(i);
ans += cur;
}
else {
val[i] = X;
dfs2(i);
ans += cur;
}
}
cout<< "YES\n";
output(1, n, val, 1);
cerr<< "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
/*
4 4
1 2 1
2 3 2
1 3 2
3 4 1
2 1
1 2 1
3 2
1 2 2
2 3 2
3 4
1 2 2
2 2 1
2 1 1
1 2 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3436 KB |
answer = YES |
2 |
Correct |
3 ms |
3436 KB |
answer = YES |
3 |
Correct |
3 ms |
3436 KB |
answer = YES |
4 |
Correct |
3 ms |
3564 KB |
answer = NO |
5 |
Correct |
3 ms |
3436 KB |
answer = YES |
6 |
Correct |
3 ms |
3436 KB |
answer = YES |
7 |
Correct |
3 ms |
3436 KB |
answer = YES |
8 |
Correct |
3 ms |
3436 KB |
answer = YES |
9 |
Correct |
3 ms |
3436 KB |
answer = NO |
10 |
Correct |
3 ms |
3436 KB |
answer = YES |
11 |
Correct |
3 ms |
3436 KB |
answer = YES |
12 |
Correct |
4 ms |
3456 KB |
answer = NO |
13 |
Correct |
3 ms |
3436 KB |
answer = YES |
14 |
Correct |
3 ms |
3436 KB |
answer = YES |
15 |
Correct |
4 ms |
3436 KB |
answer = YES |
16 |
Correct |
3 ms |
3564 KB |
answer = YES |
17 |
Correct |
3 ms |
3436 KB |
answer = YES |
18 |
Correct |
3 ms |
3436 KB |
answer = YES |
19 |
Correct |
3 ms |
3456 KB |
answer = YES |
20 |
Correct |
4 ms |
3436 KB |
answer = YES |
21 |
Correct |
3 ms |
3436 KB |
answer = YES |
22 |
Correct |
3 ms |
3436 KB |
answer = NO |
23 |
Correct |
4 ms |
3436 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3436 KB |
answer = YES |
2 |
Correct |
3 ms |
3436 KB |
answer = YES |
3 |
Correct |
3 ms |
3436 KB |
answer = YES |
4 |
Correct |
3 ms |
3564 KB |
answer = NO |
5 |
Correct |
3 ms |
3436 KB |
answer = YES |
6 |
Correct |
3 ms |
3436 KB |
answer = YES |
7 |
Correct |
3 ms |
3436 KB |
answer = YES |
8 |
Correct |
3 ms |
3436 KB |
answer = YES |
9 |
Correct |
3 ms |
3436 KB |
answer = NO |
10 |
Correct |
3 ms |
3436 KB |
answer = YES |
11 |
Correct |
3 ms |
3436 KB |
answer = YES |
12 |
Correct |
4 ms |
3456 KB |
answer = NO |
13 |
Correct |
3 ms |
3436 KB |
answer = YES |
14 |
Correct |
3 ms |
3436 KB |
answer = YES |
15 |
Correct |
4 ms |
3436 KB |
answer = YES |
16 |
Correct |
3 ms |
3564 KB |
answer = YES |
17 |
Correct |
3 ms |
3436 KB |
answer = YES |
18 |
Correct |
3 ms |
3436 KB |
answer = YES |
19 |
Correct |
3 ms |
3456 KB |
answer = YES |
20 |
Correct |
4 ms |
3436 KB |
answer = YES |
21 |
Correct |
3 ms |
3436 KB |
answer = YES |
22 |
Correct |
3 ms |
3436 KB |
answer = NO |
23 |
Correct |
4 ms |
3436 KB |
answer = NO |
24 |
Correct |
3 ms |
3436 KB |
answer = YES |
25 |
Correct |
4 ms |
3436 KB |
answer = YES |
26 |
Correct |
3 ms |
3436 KB |
answer = YES |
27 |
Correct |
3 ms |
3436 KB |
answer = YES |
28 |
Correct |
3 ms |
3436 KB |
answer = YES |
29 |
Correct |
3 ms |
3436 KB |
answer = YES |
30 |
Correct |
4 ms |
3436 KB |
answer = NO |
31 |
Correct |
3 ms |
3456 KB |
answer = YES |
32 |
Correct |
3 ms |
3436 KB |
answer = YES |
33 |
Correct |
3 ms |
3436 KB |
answer = YES |
34 |
Correct |
4 ms |
3436 KB |
answer = YES |
35 |
Correct |
3 ms |
3436 KB |
answer = YES |
36 |
Correct |
3 ms |
3436 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3436 KB |
answer = YES |
2 |
Correct |
3 ms |
3436 KB |
answer = YES |
3 |
Correct |
3 ms |
3436 KB |
answer = YES |
4 |
Correct |
3 ms |
3564 KB |
answer = NO |
5 |
Correct |
3 ms |
3436 KB |
answer = YES |
6 |
Correct |
3 ms |
3436 KB |
answer = YES |
7 |
Correct |
3 ms |
3436 KB |
answer = YES |
8 |
Correct |
3 ms |
3436 KB |
answer = YES |
9 |
Correct |
3 ms |
3436 KB |
answer = NO |
10 |
Correct |
3 ms |
3436 KB |
answer = YES |
11 |
Correct |
3 ms |
3436 KB |
answer = YES |
12 |
Correct |
4 ms |
3456 KB |
answer = NO |
13 |
Correct |
3 ms |
3436 KB |
answer = YES |
14 |
Correct |
3 ms |
3436 KB |
answer = YES |
15 |
Correct |
4 ms |
3436 KB |
answer = YES |
16 |
Correct |
3 ms |
3564 KB |
answer = YES |
17 |
Correct |
3 ms |
3436 KB |
answer = YES |
18 |
Correct |
3 ms |
3436 KB |
answer = YES |
19 |
Correct |
3 ms |
3456 KB |
answer = YES |
20 |
Correct |
4 ms |
3436 KB |
answer = YES |
21 |
Correct |
3 ms |
3436 KB |
answer = YES |
22 |
Correct |
3 ms |
3436 KB |
answer = NO |
23 |
Correct |
4 ms |
3436 KB |
answer = NO |
24 |
Correct |
3 ms |
3436 KB |
answer = YES |
25 |
Correct |
4 ms |
3436 KB |
answer = YES |
26 |
Correct |
3 ms |
3436 KB |
answer = YES |
27 |
Correct |
3 ms |
3436 KB |
answer = YES |
28 |
Correct |
3 ms |
3436 KB |
answer = YES |
29 |
Correct |
3 ms |
3436 KB |
answer = YES |
30 |
Correct |
4 ms |
3436 KB |
answer = NO |
31 |
Correct |
3 ms |
3456 KB |
answer = YES |
32 |
Correct |
3 ms |
3436 KB |
answer = YES |
33 |
Correct |
3 ms |
3436 KB |
answer = YES |
34 |
Correct |
4 ms |
3436 KB |
answer = YES |
35 |
Correct |
3 ms |
3436 KB |
answer = YES |
36 |
Correct |
3 ms |
3436 KB |
answer = YES |
37 |
Correct |
3 ms |
3436 KB |
answer = YES |
38 |
Correct |
3 ms |
3436 KB |
answer = YES |
39 |
Correct |
4 ms |
3564 KB |
answer = YES |
40 |
Correct |
4 ms |
3564 KB |
answer = YES |
41 |
Correct |
3 ms |
3564 KB |
answer = NO |
42 |
Correct |
4 ms |
3564 KB |
answer = YES |
43 |
Correct |
4 ms |
3564 KB |
answer = YES |
44 |
Correct |
4 ms |
3564 KB |
answer = YES |
45 |
Correct |
4 ms |
3564 KB |
answer = YES |
46 |
Correct |
4 ms |
3564 KB |
answer = YES |
47 |
Correct |
4 ms |
3564 KB |
answer = YES |
48 |
Correct |
4 ms |
3564 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3436 KB |
answer = YES |
2 |
Correct |
3 ms |
3436 KB |
answer = YES |
3 |
Correct |
3 ms |
3436 KB |
answer = YES |
4 |
Correct |
3 ms |
3564 KB |
answer = NO |
5 |
Correct |
3 ms |
3436 KB |
answer = YES |
6 |
Correct |
3 ms |
3436 KB |
answer = YES |
7 |
Correct |
3 ms |
3436 KB |
answer = YES |
8 |
Correct |
3 ms |
3436 KB |
answer = YES |
9 |
Correct |
3 ms |
3436 KB |
answer = NO |
10 |
Correct |
3 ms |
3436 KB |
answer = YES |
11 |
Correct |
3 ms |
3436 KB |
answer = YES |
12 |
Correct |
4 ms |
3456 KB |
answer = NO |
13 |
Correct |
3 ms |
3436 KB |
answer = YES |
14 |
Correct |
3 ms |
3436 KB |
answer = YES |
15 |
Correct |
4 ms |
3436 KB |
answer = YES |
16 |
Correct |
3 ms |
3564 KB |
answer = YES |
17 |
Correct |
3 ms |
3436 KB |
answer = YES |
18 |
Correct |
3 ms |
3436 KB |
answer = YES |
19 |
Correct |
3 ms |
3456 KB |
answer = YES |
20 |
Correct |
4 ms |
3436 KB |
answer = YES |
21 |
Correct |
3 ms |
3436 KB |
answer = YES |
22 |
Correct |
3 ms |
3436 KB |
answer = NO |
23 |
Correct |
4 ms |
3436 KB |
answer = NO |
24 |
Correct |
3 ms |
3436 KB |
answer = YES |
25 |
Correct |
4 ms |
3436 KB |
answer = YES |
26 |
Correct |
3 ms |
3436 KB |
answer = YES |
27 |
Correct |
3 ms |
3436 KB |
answer = YES |
28 |
Correct |
3 ms |
3436 KB |
answer = YES |
29 |
Correct |
3 ms |
3436 KB |
answer = YES |
30 |
Correct |
4 ms |
3436 KB |
answer = NO |
31 |
Correct |
3 ms |
3456 KB |
answer = YES |
32 |
Correct |
3 ms |
3436 KB |
answer = YES |
33 |
Correct |
3 ms |
3436 KB |
answer = YES |
34 |
Correct |
4 ms |
3436 KB |
answer = YES |
35 |
Correct |
3 ms |
3436 KB |
answer = YES |
36 |
Correct |
3 ms |
3436 KB |
answer = YES |
37 |
Correct |
3 ms |
3436 KB |
answer = YES |
38 |
Correct |
3 ms |
3436 KB |
answer = YES |
39 |
Correct |
4 ms |
3564 KB |
answer = YES |
40 |
Correct |
4 ms |
3564 KB |
answer = YES |
41 |
Correct |
3 ms |
3564 KB |
answer = NO |
42 |
Correct |
4 ms |
3564 KB |
answer = YES |
43 |
Correct |
4 ms |
3564 KB |
answer = YES |
44 |
Correct |
4 ms |
3564 KB |
answer = YES |
45 |
Correct |
4 ms |
3564 KB |
answer = YES |
46 |
Correct |
4 ms |
3564 KB |
answer = YES |
47 |
Correct |
4 ms |
3564 KB |
answer = YES |
48 |
Correct |
4 ms |
3564 KB |
answer = YES |
49 |
Correct |
12 ms |
4332 KB |
answer = YES |
50 |
Correct |
13 ms |
4716 KB |
answer = YES |
51 |
Correct |
13 ms |
4716 KB |
answer = YES |
52 |
Correct |
7 ms |
3948 KB |
answer = NO |
53 |
Correct |
4 ms |
3564 KB |
answer = YES |
54 |
Correct |
6 ms |
3692 KB |
answer = YES |
55 |
Correct |
8 ms |
3948 KB |
answer = YES |
56 |
Correct |
12 ms |
4332 KB |
answer = YES |
57 |
Correct |
12 ms |
4204 KB |
answer = YES |
58 |
Correct |
11 ms |
4204 KB |
answer = YES |
59 |
Correct |
11 ms |
4204 KB |
answer = YES |
60 |
Correct |
12 ms |
4204 KB |
answer = YES |
61 |
Correct |
8 ms |
3948 KB |
answer = YES |
62 |
Correct |
64 ms |
10604 KB |
answer = NO |
63 |
Correct |
71 ms |
11184 KB |
answer = YES |
64 |
Correct |
67 ms |
11116 KB |
answer = NO |
65 |
Correct |
77 ms |
11244 KB |
answer = YES |
66 |
Correct |
5 ms |
3692 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3436 KB |
answer = YES |
2 |
Correct |
3 ms |
3436 KB |
answer = YES |
3 |
Correct |
3 ms |
3436 KB |
answer = YES |
4 |
Correct |
3 ms |
3564 KB |
answer = NO |
5 |
Correct |
3 ms |
3436 KB |
answer = YES |
6 |
Correct |
3 ms |
3436 KB |
answer = YES |
7 |
Correct |
3 ms |
3436 KB |
answer = YES |
8 |
Correct |
3 ms |
3436 KB |
answer = YES |
9 |
Correct |
3 ms |
3436 KB |
answer = NO |
10 |
Correct |
3 ms |
3436 KB |
answer = YES |
11 |
Correct |
3 ms |
3436 KB |
answer = YES |
12 |
Correct |
4 ms |
3456 KB |
answer = NO |
13 |
Correct |
3 ms |
3436 KB |
answer = YES |
14 |
Correct |
3 ms |
3436 KB |
answer = YES |
15 |
Correct |
4 ms |
3436 KB |
answer = YES |
16 |
Correct |
3 ms |
3564 KB |
answer = YES |
17 |
Correct |
3 ms |
3436 KB |
answer = YES |
18 |
Correct |
3 ms |
3436 KB |
answer = YES |
19 |
Correct |
3 ms |
3456 KB |
answer = YES |
20 |
Correct |
4 ms |
3436 KB |
answer = YES |
21 |
Correct |
3 ms |
3436 KB |
answer = YES |
22 |
Correct |
3 ms |
3436 KB |
answer = NO |
23 |
Correct |
4 ms |
3436 KB |
answer = NO |
24 |
Correct |
3 ms |
3436 KB |
answer = YES |
25 |
Correct |
4 ms |
3436 KB |
answer = YES |
26 |
Correct |
3 ms |
3436 KB |
answer = YES |
27 |
Correct |
3 ms |
3436 KB |
answer = YES |
28 |
Correct |
3 ms |
3436 KB |
answer = YES |
29 |
Correct |
3 ms |
3436 KB |
answer = YES |
30 |
Correct |
4 ms |
3436 KB |
answer = NO |
31 |
Correct |
3 ms |
3456 KB |
answer = YES |
32 |
Correct |
3 ms |
3436 KB |
answer = YES |
33 |
Correct |
3 ms |
3436 KB |
answer = YES |
34 |
Correct |
4 ms |
3436 KB |
answer = YES |
35 |
Correct |
3 ms |
3436 KB |
answer = YES |
36 |
Correct |
3 ms |
3436 KB |
answer = YES |
37 |
Correct |
3 ms |
3436 KB |
answer = YES |
38 |
Correct |
3 ms |
3436 KB |
answer = YES |
39 |
Correct |
4 ms |
3564 KB |
answer = YES |
40 |
Correct |
4 ms |
3564 KB |
answer = YES |
41 |
Correct |
3 ms |
3564 KB |
answer = NO |
42 |
Correct |
4 ms |
3564 KB |
answer = YES |
43 |
Correct |
4 ms |
3564 KB |
answer = YES |
44 |
Correct |
4 ms |
3564 KB |
answer = YES |
45 |
Correct |
4 ms |
3564 KB |
answer = YES |
46 |
Correct |
4 ms |
3564 KB |
answer = YES |
47 |
Correct |
4 ms |
3564 KB |
answer = YES |
48 |
Correct |
4 ms |
3564 KB |
answer = YES |
49 |
Correct |
12 ms |
4332 KB |
answer = YES |
50 |
Correct |
13 ms |
4716 KB |
answer = YES |
51 |
Correct |
13 ms |
4716 KB |
answer = YES |
52 |
Correct |
7 ms |
3948 KB |
answer = NO |
53 |
Correct |
4 ms |
3564 KB |
answer = YES |
54 |
Correct |
6 ms |
3692 KB |
answer = YES |
55 |
Correct |
8 ms |
3948 KB |
answer = YES |
56 |
Correct |
12 ms |
4332 KB |
answer = YES |
57 |
Correct |
12 ms |
4204 KB |
answer = YES |
58 |
Correct |
11 ms |
4204 KB |
answer = YES |
59 |
Correct |
11 ms |
4204 KB |
answer = YES |
60 |
Correct |
12 ms |
4204 KB |
answer = YES |
61 |
Correct |
8 ms |
3948 KB |
answer = YES |
62 |
Correct |
64 ms |
10604 KB |
answer = NO |
63 |
Correct |
71 ms |
11184 KB |
answer = YES |
64 |
Correct |
67 ms |
11116 KB |
answer = NO |
65 |
Correct |
77 ms |
11244 KB |
answer = YES |
66 |
Correct |
5 ms |
3692 KB |
answer = YES |
67 |
Correct |
105 ms |
18404 KB |
answer = YES |
68 |
Correct |
97 ms |
18280 KB |
answer = YES |
69 |
Correct |
90 ms |
18276 KB |
answer = YES |
70 |
Correct |
134 ms |
22756 KB |
answer = YES |
71 |
Correct |
94 ms |
18276 KB |
answer = YES |
72 |
Correct |
122 ms |
11108 KB |
answer = YES |
73 |
Correct |
111 ms |
10856 KB |
answer = YES |
74 |
Correct |
77 ms |
12012 KB |
answer = YES |
75 |
Correct |
30 ms |
8684 KB |
answer = NO |
76 |
Correct |
15 ms |
4460 KB |
answer = YES |
77 |
Correct |
28 ms |
5484 KB |
answer = YES |
78 |
Correct |
46 ms |
6768 KB |
answer = YES |
79 |
Correct |
107 ms |
10092 KB |
answer = YES |
80 |
Correct |
78 ms |
12136 KB |
answer = YES |
81 |
Correct |
54 ms |
13288 KB |
answer = NO |
82 |
Correct |
124 ms |
17512 KB |
answer = YES |
83 |
Correct |
153 ms |
18532 KB |
answer = YES |
84 |
Correct |
151 ms |
18404 KB |
answer = YES |
85 |
Correct |
105 ms |
18404 KB |
answer = YES |
86 |
Correct |
102 ms |
18532 KB |
answer = YES |
87 |
Correct |
70 ms |
11372 KB |
answer = NO |
88 |
Correct |
145 ms |
13672 KB |
answer = YES |
89 |
Correct |
101 ms |
9964 KB |
answer = YES |
90 |
Correct |
100 ms |
9984 KB |
answer = YES |
91 |
Correct |
107 ms |
9964 KB |
answer = YES |
92 |
Correct |
53 ms |
7404 KB |
answer = YES |
93 |
Correct |
51 ms |
7276 KB |
answer = YES |
94 |
Correct |
68 ms |
16424 KB |
answer = NO |
95 |
Correct |
54 ms |
8940 KB |
answer = NO |
96 |
Correct |
175 ms |
19052 KB |
answer = YES |
97 |
Correct |
42 ms |
16356 KB |
answer = NO |