Submission #305577

# Submission time Handle Problem Language Result Execution time Memory
305577 2020-09-23T15:08:34 Z luciocf Graph (BOI20_graph) C++14
100 / 100
578 ms 18676 KB
#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 <= 600; 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";
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB answer = YES
2 Correct 2 ms 2688 KB answer = YES
3 Correct 2 ms 2688 KB answer = YES
4 Correct 3 ms 2688 KB answer = NO
5 Correct 2 ms 2688 KB answer = YES
6 Correct 3 ms 2688 KB answer = YES
7 Correct 2 ms 2688 KB answer = YES
8 Correct 2 ms 2688 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2688 KB answer = YES
11 Correct 3 ms 2688 KB answer = YES
12 Correct 2 ms 2688 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2688 KB answer = YES
16 Correct 3 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 2 ms 2688 KB answer = YES
19 Correct 3 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 2 ms 2688 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB answer = YES
2 Correct 2 ms 2688 KB answer = YES
3 Correct 2 ms 2688 KB answer = YES
4 Correct 3 ms 2688 KB answer = NO
5 Correct 2 ms 2688 KB answer = YES
6 Correct 3 ms 2688 KB answer = YES
7 Correct 2 ms 2688 KB answer = YES
8 Correct 2 ms 2688 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2688 KB answer = YES
11 Correct 3 ms 2688 KB answer = YES
12 Correct 2 ms 2688 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2688 KB answer = YES
16 Correct 3 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 2 ms 2688 KB answer = YES
19 Correct 3 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 2 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 3 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 2 ms 2688 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB answer = YES
2 Correct 2 ms 2688 KB answer = YES
3 Correct 2 ms 2688 KB answer = YES
4 Correct 3 ms 2688 KB answer = NO
5 Correct 2 ms 2688 KB answer = YES
6 Correct 3 ms 2688 KB answer = YES
7 Correct 2 ms 2688 KB answer = YES
8 Correct 2 ms 2688 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2688 KB answer = YES
11 Correct 3 ms 2688 KB answer = YES
12 Correct 2 ms 2688 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2688 KB answer = YES
16 Correct 3 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 2 ms 2688 KB answer = YES
19 Correct 3 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 2 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 3 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 2 ms 2688 KB answer = YES
37 Correct 3 ms 2688 KB answer = YES
38 Correct 2 ms 2688 KB answer = YES
39 Correct 5 ms 2688 KB answer = YES
40 Correct 3 ms 2816 KB answer = YES
41 Correct 5 ms 2816 KB answer = NO
42 Correct 6 ms 2816 KB answer = YES
43 Correct 7 ms 2816 KB answer = YES
44 Correct 6 ms 2816 KB answer = YES
45 Correct 4 ms 2816 KB answer = YES
46 Correct 5 ms 2688 KB answer = YES
47 Correct 3 ms 2816 KB answer = YES
48 Correct 5 ms 2816 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB answer = YES
2 Correct 2 ms 2688 KB answer = YES
3 Correct 2 ms 2688 KB answer = YES
4 Correct 3 ms 2688 KB answer = NO
5 Correct 2 ms 2688 KB answer = YES
6 Correct 3 ms 2688 KB answer = YES
7 Correct 2 ms 2688 KB answer = YES
8 Correct 2 ms 2688 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2688 KB answer = YES
11 Correct 3 ms 2688 KB answer = YES
12 Correct 2 ms 2688 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2688 KB answer = YES
16 Correct 3 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 2 ms 2688 KB answer = YES
19 Correct 3 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 2 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 3 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 2 ms 2688 KB answer = YES
37 Correct 3 ms 2688 KB answer = YES
38 Correct 2 ms 2688 KB answer = YES
39 Correct 5 ms 2688 KB answer = YES
40 Correct 3 ms 2816 KB answer = YES
41 Correct 5 ms 2816 KB answer = NO
42 Correct 6 ms 2816 KB answer = YES
43 Correct 7 ms 2816 KB answer = YES
44 Correct 6 ms 2816 KB answer = YES
45 Correct 4 ms 2816 KB answer = YES
46 Correct 5 ms 2688 KB answer = YES
47 Correct 3 ms 2816 KB answer = YES
48 Correct 5 ms 2816 KB answer = YES
49 Correct 37 ms 3580 KB answer = YES
50 Correct 14 ms 3712 KB answer = YES
51 Correct 39 ms 3712 KB answer = YES
52 Correct 31 ms 3576 KB answer = NO
53 Correct 6 ms 2816 KB answer = YES
54 Correct 11 ms 2944 KB answer = YES
55 Correct 19 ms 3200 KB answer = YES
56 Correct 37 ms 3576 KB answer = YES
57 Correct 44 ms 3448 KB answer = YES
58 Correct 34 ms 3456 KB answer = YES
59 Correct 12 ms 3456 KB answer = YES
60 Correct 38 ms 3456 KB answer = YES
61 Correct 8 ms 3072 KB answer = YES
62 Correct 73 ms 7928 KB answer = NO
63 Correct 84 ms 8056 KB answer = YES
64 Correct 76 ms 7928 KB answer = NO
65 Correct 83 ms 8056 KB answer = YES
66 Correct 10 ms 2816 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB answer = YES
2 Correct 2 ms 2688 KB answer = YES
3 Correct 2 ms 2688 KB answer = YES
4 Correct 3 ms 2688 KB answer = NO
5 Correct 2 ms 2688 KB answer = YES
6 Correct 3 ms 2688 KB answer = YES
7 Correct 2 ms 2688 KB answer = YES
8 Correct 2 ms 2688 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2688 KB answer = YES
11 Correct 3 ms 2688 KB answer = YES
12 Correct 2 ms 2688 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2688 KB answer = YES
16 Correct 3 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 2 ms 2688 KB answer = YES
19 Correct 3 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 2 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 3 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 2 ms 2688 KB answer = YES
37 Correct 3 ms 2688 KB answer = YES
38 Correct 2 ms 2688 KB answer = YES
39 Correct 5 ms 2688 KB answer = YES
40 Correct 3 ms 2816 KB answer = YES
41 Correct 5 ms 2816 KB answer = NO
42 Correct 6 ms 2816 KB answer = YES
43 Correct 7 ms 2816 KB answer = YES
44 Correct 6 ms 2816 KB answer = YES
45 Correct 4 ms 2816 KB answer = YES
46 Correct 5 ms 2688 KB answer = YES
47 Correct 3 ms 2816 KB answer = YES
48 Correct 5 ms 2816 KB answer = YES
49 Correct 37 ms 3580 KB answer = YES
50 Correct 14 ms 3712 KB answer = YES
51 Correct 39 ms 3712 KB answer = YES
52 Correct 31 ms 3576 KB answer = NO
53 Correct 6 ms 2816 KB answer = YES
54 Correct 11 ms 2944 KB answer = YES
55 Correct 19 ms 3200 KB answer = YES
56 Correct 37 ms 3576 KB answer = YES
57 Correct 44 ms 3448 KB answer = YES
58 Correct 34 ms 3456 KB answer = YES
59 Correct 12 ms 3456 KB answer = YES
60 Correct 38 ms 3456 KB answer = YES
61 Correct 8 ms 3072 KB answer = YES
62 Correct 73 ms 7928 KB answer = NO
63 Correct 84 ms 8056 KB answer = YES
64 Correct 76 ms 7928 KB answer = NO
65 Correct 83 ms 8056 KB answer = YES
66 Correct 10 ms 2816 KB answer = YES
67 Correct 344 ms 14960 KB answer = YES
68 Correct 333 ms 14832 KB answer = YES
69 Correct 101 ms 14908 KB answer = YES
70 Correct 146 ms 18032 KB answer = YES
71 Correct 103 ms 14832 KB answer = YES
72 Correct 533 ms 10864 KB answer = YES
73 Correct 158 ms 10732 KB answer = YES
74 Correct 88 ms 10136 KB answer = YES
75 Correct 267 ms 9124 KB answer = NO
76 Correct 44 ms 3708 KB answer = YES
77 Correct 95 ms 4728 KB answer = YES
78 Correct 188 ms 6264 KB answer = YES
79 Correct 468 ms 9644 KB answer = YES
80 Correct 314 ms 10100 KB answer = YES
81 Correct 74 ms 11248 KB answer = NO
82 Correct 147 ms 14576 KB answer = YES
83 Correct 223 ms 14952 KB answer = YES
84 Correct 578 ms 14956 KB answer = YES
85 Correct 349 ms 16368 KB answer = YES
86 Correct 122 ms 16532 KB answer = YES
87 Correct 207 ms 11256 KB answer = NO
88 Correct 244 ms 13428 KB answer = YES
89 Correct 131 ms 11036 KB answer = YES
90 Correct 129 ms 10948 KB answer = YES
91 Correct 137 ms 10872 KB answer = YES
92 Correct 66 ms 7416 KB answer = YES
93 Correct 67 ms 7416 KB answer = YES
94 Correct 509 ms 14832 KB answer = NO
95 Correct 64 ms 9592 KB answer = NO
96 Correct 308 ms 18676 KB answer = YES
97 Correct 285 ms 14832 KB answer = NO