#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e5+10;
int n, m;
long double a[maxn];
vector<pii> grafo[maxn];
int sign[maxn], cte[maxn];
vector<int> comp;
bool mark[maxn];
long double f(long double x)
{
long double ans = 0;
for (auto u: comp)
ans += abs(1.00*sign[u]*x + 1.00*cte[u]);
return ans;
}
long double search(long double l, long double r)
{
for (int i = 1; i <= 1000; i++)
{
long double mid1 = l + (r - l)/3.00;
long double mid2 = r - (r - l)/3.00;
long double f1 = f(mid1);
long double f2 = f(mid2);
if (f1 < f2) r = mid2;
else l = mid1;
}
return l;
}
void dfs(int u)
{
comp.push_back(u);
for (auto pp: grafo[u])
{
int v = pp.ff, w = pp.ss;
if (sign[v]) continue;
sign[v] = (sign[u] == 1 ? -1 : 1);
cte[v] = w-cte[u];
dfs(v);
}
}
void color(int u)
{
mark[u] = 1;
for (auto pp: grafo[u])
{
int v = pp.ff, w = pp.ss;
if (mark[v]) continue;
a[v] = 1.00*w-a[u];
color(v);
}
}
bool certo(int u, long double x)
{
for (auto x: comp)
mark[x] = 0;
a[u] = x;
color(u);
for (auto x: comp)
for (auto y: grafo[x])
if (fabs(a[x] + a[y.ff] - (long double)y.ss) > 1e-6)
return false;
return true;
}
int main(void)
{
ios::sync_with_stdio(false); cin.tie(0);
cout.precision(10);
cout << fixed;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int u, v, w;
cin >> u >> v >> w;
grafo[u].push_back({v, w});
grafo[v].push_back({u, w});
}
for (int i = 1; i <= n; i++)
{
if (sign[i] == 0)
{
sign[i] = 1;
comp.clear();
dfs(i);
long double x;
bool fixo = 0;
for (auto u: comp)
{
for (auto pp: grafo[u])
{
int v = pp.first, w = pp.second;
if (sign[v] == sign[u])
{
x = 1.00*sign[u]*(w-cte[u]-cte[v])/2.00;
fixo = 1;
break;
}
}
}
if (!fixo)
x = search(-1e9, 1e9);
if (!certo(i, x))
{
printf("NO\n");
return 0;
}
}
}
cout << "YES\n";
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
cout << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2688 KB |
answer = YES |
2 |
Correct |
2 ms |
2688 KB |
answer = YES |
3 |
Correct |
3 ms |
2688 KB |
answer = YES |
4 |
Correct |
2 ms |
2688 KB |
answer = NO |
5 |
Correct |
2 ms |
2688 KB |
answer = YES |
6 |
Correct |
2 ms |
2688 KB |
answer = YES |
7 |
Correct |
2 ms |
2688 KB |
answer = YES |
8 |
Correct |
3 ms |
2688 KB |
answer = YES |
9 |
Correct |
2 ms |
2688 KB |
answer = NO |
10 |
Correct |
3 ms |
2688 KB |
answer = YES |
11 |
Correct |
2 ms |
2688 KB |
answer = YES |
12 |
Correct |
2 ms |
2688 KB |
answer = NO |
13 |
Correct |
3 ms |
2688 KB |
answer = YES |
14 |
Correct |
3 ms |
2688 KB |
answer = YES |
15 |
Correct |
3 ms |
2688 KB |
answer = YES |
16 |
Correct |
2 ms |
2688 KB |
answer = YES |
17 |
Correct |
2 ms |
2688 KB |
answer = YES |
18 |
Correct |
2 ms |
2688 KB |
answer = YES |
19 |
Correct |
2 ms |
2688 KB |
answer = YES |
20 |
Correct |
2 ms |
2688 KB |
answer = YES |
21 |
Correct |
2 ms |
2688 KB |
answer = YES |
22 |
Correct |
3 ms |
2688 KB |
answer = NO |
23 |
Correct |
3 ms |
2688 KB |
answer = NO |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2688 KB |
answer = YES |
2 |
Correct |
2 ms |
2688 KB |
answer = YES |
3 |
Correct |
3 ms |
2688 KB |
answer = YES |
4 |
Correct |
2 ms |
2688 KB |
answer = NO |
5 |
Correct |
2 ms |
2688 KB |
answer = YES |
6 |
Correct |
2 ms |
2688 KB |
answer = YES |
7 |
Correct |
2 ms |
2688 KB |
answer = YES |
8 |
Correct |
3 ms |
2688 KB |
answer = YES |
9 |
Correct |
2 ms |
2688 KB |
answer = NO |
10 |
Correct |
3 ms |
2688 KB |
answer = YES |
11 |
Correct |
2 ms |
2688 KB |
answer = YES |
12 |
Correct |
2 ms |
2688 KB |
answer = NO |
13 |
Correct |
3 ms |
2688 KB |
answer = YES |
14 |
Correct |
3 ms |
2688 KB |
answer = YES |
15 |
Correct |
3 ms |
2688 KB |
answer = YES |
16 |
Correct |
2 ms |
2688 KB |
answer = YES |
17 |
Correct |
2 ms |
2688 KB |
answer = YES |
18 |
Correct |
2 ms |
2688 KB |
answer = YES |
19 |
Correct |
2 ms |
2688 KB |
answer = YES |
20 |
Correct |
2 ms |
2688 KB |
answer = YES |
21 |
Correct |
2 ms |
2688 KB |
answer = YES |
22 |
Correct |
3 ms |
2688 KB |
answer = NO |
23 |
Correct |
3 ms |
2688 KB |
answer = NO |
24 |
Correct |
3 ms |
2688 KB |
answer = YES |
25 |
Correct |
3 ms |
2688 KB |
answer = YES |
26 |
Correct |
3 ms |
2688 KB |
answer = YES |
27 |
Correct |
3 ms |
2688 KB |
answer = YES |
28 |
Correct |
3 ms |
2688 KB |
answer = YES |
29 |
Correct |
3 ms |
2688 KB |
answer = YES |
30 |
Correct |
3 ms |
2688 KB |
answer = NO |
31 |
Correct |
3 ms |
2688 KB |
answer = YES |
32 |
Correct |
2 ms |
2688 KB |
answer = YES |
33 |
Correct |
3 ms |
2688 KB |
answer = YES |
34 |
Correct |
3 ms |
2688 KB |
answer = YES |
35 |
Correct |
2 ms |
2688 KB |
answer = YES |
36 |
Correct |
3 ms |
2688 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2688 KB |
answer = YES |
2 |
Correct |
2 ms |
2688 KB |
answer = YES |
3 |
Correct |
3 ms |
2688 KB |
answer = YES |
4 |
Correct |
2 ms |
2688 KB |
answer = NO |
5 |
Correct |
2 ms |
2688 KB |
answer = YES |
6 |
Correct |
2 ms |
2688 KB |
answer = YES |
7 |
Correct |
2 ms |
2688 KB |
answer = YES |
8 |
Correct |
3 ms |
2688 KB |
answer = YES |
9 |
Correct |
2 ms |
2688 KB |
answer = NO |
10 |
Correct |
3 ms |
2688 KB |
answer = YES |
11 |
Correct |
2 ms |
2688 KB |
answer = YES |
12 |
Correct |
2 ms |
2688 KB |
answer = NO |
13 |
Correct |
3 ms |
2688 KB |
answer = YES |
14 |
Correct |
3 ms |
2688 KB |
answer = YES |
15 |
Correct |
3 ms |
2688 KB |
answer = YES |
16 |
Correct |
2 ms |
2688 KB |
answer = YES |
17 |
Correct |
2 ms |
2688 KB |
answer = YES |
18 |
Correct |
2 ms |
2688 KB |
answer = YES |
19 |
Correct |
2 ms |
2688 KB |
answer = YES |
20 |
Correct |
2 ms |
2688 KB |
answer = YES |
21 |
Correct |
2 ms |
2688 KB |
answer = YES |
22 |
Correct |
3 ms |
2688 KB |
answer = NO |
23 |
Correct |
3 ms |
2688 KB |
answer = NO |
24 |
Correct |
3 ms |
2688 KB |
answer = YES |
25 |
Correct |
3 ms |
2688 KB |
answer = YES |
26 |
Correct |
3 ms |
2688 KB |
answer = YES |
27 |
Correct |
3 ms |
2688 KB |
answer = YES |
28 |
Correct |
3 ms |
2688 KB |
answer = YES |
29 |
Correct |
3 ms |
2688 KB |
answer = YES |
30 |
Correct |
3 ms |
2688 KB |
answer = NO |
31 |
Correct |
3 ms |
2688 KB |
answer = YES |
32 |
Correct |
2 ms |
2688 KB |
answer = YES |
33 |
Correct |
3 ms |
2688 KB |
answer = YES |
34 |
Correct |
3 ms |
2688 KB |
answer = YES |
35 |
Correct |
2 ms |
2688 KB |
answer = YES |
36 |
Correct |
3 ms |
2688 KB |
answer = YES |
37 |
Correct |
4 ms |
2688 KB |
answer = YES |
38 |
Correct |
3 ms |
2688 KB |
answer = YES |
39 |
Correct |
6 ms |
2816 KB |
answer = YES |
40 |
Correct |
4 ms |
2756 KB |
answer = YES |
41 |
Correct |
7 ms |
2816 KB |
answer = NO |
42 |
Correct |
7 ms |
2816 KB |
answer = YES |
43 |
Correct |
8 ms |
2816 KB |
answer = YES |
44 |
Correct |
7 ms |
2816 KB |
answer = YES |
45 |
Correct |
4 ms |
2816 KB |
answer = YES |
46 |
Correct |
5 ms |
2816 KB |
answer = YES |
47 |
Correct |
4 ms |
2816 KB |
answer = YES |
48 |
Correct |
3 ms |
2816 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2688 KB |
answer = YES |
2 |
Correct |
2 ms |
2688 KB |
answer = YES |
3 |
Correct |
3 ms |
2688 KB |
answer = YES |
4 |
Correct |
2 ms |
2688 KB |
answer = NO |
5 |
Correct |
2 ms |
2688 KB |
answer = YES |
6 |
Correct |
2 ms |
2688 KB |
answer = YES |
7 |
Correct |
2 ms |
2688 KB |
answer = YES |
8 |
Correct |
3 ms |
2688 KB |
answer = YES |
9 |
Correct |
2 ms |
2688 KB |
answer = NO |
10 |
Correct |
3 ms |
2688 KB |
answer = YES |
11 |
Correct |
2 ms |
2688 KB |
answer = YES |
12 |
Correct |
2 ms |
2688 KB |
answer = NO |
13 |
Correct |
3 ms |
2688 KB |
answer = YES |
14 |
Correct |
3 ms |
2688 KB |
answer = YES |
15 |
Correct |
3 ms |
2688 KB |
answer = YES |
16 |
Correct |
2 ms |
2688 KB |
answer = YES |
17 |
Correct |
2 ms |
2688 KB |
answer = YES |
18 |
Correct |
2 ms |
2688 KB |
answer = YES |
19 |
Correct |
2 ms |
2688 KB |
answer = YES |
20 |
Correct |
2 ms |
2688 KB |
answer = YES |
21 |
Correct |
2 ms |
2688 KB |
answer = YES |
22 |
Correct |
3 ms |
2688 KB |
answer = NO |
23 |
Correct |
3 ms |
2688 KB |
answer = NO |
24 |
Correct |
3 ms |
2688 KB |
answer = YES |
25 |
Correct |
3 ms |
2688 KB |
answer = YES |
26 |
Correct |
3 ms |
2688 KB |
answer = YES |
27 |
Correct |
3 ms |
2688 KB |
answer = YES |
28 |
Correct |
3 ms |
2688 KB |
answer = YES |
29 |
Correct |
3 ms |
2688 KB |
answer = YES |
30 |
Correct |
3 ms |
2688 KB |
answer = NO |
31 |
Correct |
3 ms |
2688 KB |
answer = YES |
32 |
Correct |
2 ms |
2688 KB |
answer = YES |
33 |
Correct |
3 ms |
2688 KB |
answer = YES |
34 |
Correct |
3 ms |
2688 KB |
answer = YES |
35 |
Correct |
2 ms |
2688 KB |
answer = YES |
36 |
Correct |
3 ms |
2688 KB |
answer = YES |
37 |
Correct |
4 ms |
2688 KB |
answer = YES |
38 |
Correct |
3 ms |
2688 KB |
answer = YES |
39 |
Correct |
6 ms |
2816 KB |
answer = YES |
40 |
Correct |
4 ms |
2756 KB |
answer = YES |
41 |
Correct |
7 ms |
2816 KB |
answer = NO |
42 |
Correct |
7 ms |
2816 KB |
answer = YES |
43 |
Correct |
8 ms |
2816 KB |
answer = YES |
44 |
Correct |
7 ms |
2816 KB |
answer = YES |
45 |
Correct |
4 ms |
2816 KB |
answer = YES |
46 |
Correct |
5 ms |
2816 KB |
answer = YES |
47 |
Correct |
4 ms |
2816 KB |
answer = YES |
48 |
Correct |
3 ms |
2816 KB |
answer = YES |
49 |
Correct |
55 ms |
3704 KB |
answer = YES |
50 |
Correct |
14 ms |
3840 KB |
answer = YES |
51 |
Correct |
55 ms |
3836 KB |
answer = YES |
52 |
Correct |
48 ms |
3704 KB |
answer = NO |
53 |
Correct |
7 ms |
2816 KB |
answer = YES |
54 |
Correct |
14 ms |
2944 KB |
answer = YES |
55 |
Correct |
27 ms |
3200 KB |
answer = YES |
56 |
Correct |
53 ms |
3704 KB |
answer = YES |
57 |
Correct |
64 ms |
3576 KB |
answer = YES |
58 |
Correct |
47 ms |
3548 KB |
answer = YES |
59 |
Correct |
12 ms |
3616 KB |
answer = YES |
60 |
Correct |
56 ms |
3704 KB |
answer = YES |
61 |
Correct |
9 ms |
3200 KB |
answer = YES |
62 |
Correct |
76 ms |
10232 KB |
answer = NO |
63 |
Correct |
85 ms |
10360 KB |
answer = YES |
64 |
Correct |
80 ms |
10232 KB |
answer = NO |
65 |
Correct |
84 ms |
10364 KB |
answer = YES |
66 |
Correct |
13 ms |
2944 KB |
answer = YES |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2688 KB |
answer = YES |
2 |
Correct |
2 ms |
2688 KB |
answer = YES |
3 |
Correct |
3 ms |
2688 KB |
answer = YES |
4 |
Correct |
2 ms |
2688 KB |
answer = NO |
5 |
Correct |
2 ms |
2688 KB |
answer = YES |
6 |
Correct |
2 ms |
2688 KB |
answer = YES |
7 |
Correct |
2 ms |
2688 KB |
answer = YES |
8 |
Correct |
3 ms |
2688 KB |
answer = YES |
9 |
Correct |
2 ms |
2688 KB |
answer = NO |
10 |
Correct |
3 ms |
2688 KB |
answer = YES |
11 |
Correct |
2 ms |
2688 KB |
answer = YES |
12 |
Correct |
2 ms |
2688 KB |
answer = NO |
13 |
Correct |
3 ms |
2688 KB |
answer = YES |
14 |
Correct |
3 ms |
2688 KB |
answer = YES |
15 |
Correct |
3 ms |
2688 KB |
answer = YES |
16 |
Correct |
2 ms |
2688 KB |
answer = YES |
17 |
Correct |
2 ms |
2688 KB |
answer = YES |
18 |
Correct |
2 ms |
2688 KB |
answer = YES |
19 |
Correct |
2 ms |
2688 KB |
answer = YES |
20 |
Correct |
2 ms |
2688 KB |
answer = YES |
21 |
Correct |
2 ms |
2688 KB |
answer = YES |
22 |
Correct |
3 ms |
2688 KB |
answer = NO |
23 |
Correct |
3 ms |
2688 KB |
answer = NO |
24 |
Correct |
3 ms |
2688 KB |
answer = YES |
25 |
Correct |
3 ms |
2688 KB |
answer = YES |
26 |
Correct |
3 ms |
2688 KB |
answer = YES |
27 |
Correct |
3 ms |
2688 KB |
answer = YES |
28 |
Correct |
3 ms |
2688 KB |
answer = YES |
29 |
Correct |
3 ms |
2688 KB |
answer = YES |
30 |
Correct |
3 ms |
2688 KB |
answer = NO |
31 |
Correct |
3 ms |
2688 KB |
answer = YES |
32 |
Correct |
2 ms |
2688 KB |
answer = YES |
33 |
Correct |
3 ms |
2688 KB |
answer = YES |
34 |
Correct |
3 ms |
2688 KB |
answer = YES |
35 |
Correct |
2 ms |
2688 KB |
answer = YES |
36 |
Correct |
3 ms |
2688 KB |
answer = YES |
37 |
Correct |
4 ms |
2688 KB |
answer = YES |
38 |
Correct |
3 ms |
2688 KB |
answer = YES |
39 |
Correct |
6 ms |
2816 KB |
answer = YES |
40 |
Correct |
4 ms |
2756 KB |
answer = YES |
41 |
Correct |
7 ms |
2816 KB |
answer = NO |
42 |
Correct |
7 ms |
2816 KB |
answer = YES |
43 |
Correct |
8 ms |
2816 KB |
answer = YES |
44 |
Correct |
7 ms |
2816 KB |
answer = YES |
45 |
Correct |
4 ms |
2816 KB |
answer = YES |
46 |
Correct |
5 ms |
2816 KB |
answer = YES |
47 |
Correct |
4 ms |
2816 KB |
answer = YES |
48 |
Correct |
3 ms |
2816 KB |
answer = YES |
49 |
Correct |
55 ms |
3704 KB |
answer = YES |
50 |
Correct |
14 ms |
3840 KB |
answer = YES |
51 |
Correct |
55 ms |
3836 KB |
answer = YES |
52 |
Correct |
48 ms |
3704 KB |
answer = NO |
53 |
Correct |
7 ms |
2816 KB |
answer = YES |
54 |
Correct |
14 ms |
2944 KB |
answer = YES |
55 |
Correct |
27 ms |
3200 KB |
answer = YES |
56 |
Correct |
53 ms |
3704 KB |
answer = YES |
57 |
Correct |
64 ms |
3576 KB |
answer = YES |
58 |
Correct |
47 ms |
3548 KB |
answer = YES |
59 |
Correct |
12 ms |
3616 KB |
answer = YES |
60 |
Correct |
56 ms |
3704 KB |
answer = YES |
61 |
Correct |
9 ms |
3200 KB |
answer = YES |
62 |
Correct |
76 ms |
10232 KB |
answer = NO |
63 |
Correct |
85 ms |
10360 KB |
answer = YES |
64 |
Correct |
80 ms |
10232 KB |
answer = NO |
65 |
Correct |
84 ms |
10364 KB |
answer = YES |
66 |
Correct |
13 ms |
2944 KB |
answer = YES |
67 |
Correct |
503 ms |
16256 KB |
answer = YES |
68 |
Correct |
488 ms |
16368 KB |
answer = YES |
69 |
Correct |
105 ms |
16112 KB |
answer = YES |
70 |
Correct |
149 ms |
20592 KB |
answer = YES |
71 |
Correct |
106 ms |
16112 KB |
answer = YES |
72 |
Execution timed out |
775 ms |
12140 KB |
Time limit exceeded |
73 |
Halted |
0 ms |
0 KB |
- |