답안 #305576

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
305576 2020-09-23T15:07:30 Z luciocf Graph (BOI20_graph) C++17
58 / 100
700 ms 17904 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 <= 800; 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 2 ms 2688 KB answer = YES
2 Correct 3 ms 2688 KB answer = YES
3 Correct 2 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 2 ms 2688 KB answer = YES
11 Correct 2 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 2 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 3 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 2 ms 2688 KB answer = NO
23 Correct 2 ms 2688 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB answer = YES
2 Correct 3 ms 2688 KB answer = YES
3 Correct 2 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 2 ms 2688 KB answer = YES
11 Correct 2 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 2 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 3 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 2 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 2720 KB answer = YES
26 Correct 3 ms 2688 KB answer = YES
27 Correct 3 ms 2688 KB answer = YES
28 Correct 2 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 2 ms 2688 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB answer = YES
2 Correct 3 ms 2688 KB answer = YES
3 Correct 2 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 2 ms 2688 KB answer = YES
11 Correct 2 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 2 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 3 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 2 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 2720 KB answer = YES
26 Correct 3 ms 2688 KB answer = YES
27 Correct 3 ms 2688 KB answer = YES
28 Correct 2 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 2 ms 2688 KB answer = YES
37 Correct 4 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 8 ms 2816 KB answer = YES
44 Correct 7 ms 2816 KB answer = YES
45 Correct 3 ms 2816 KB answer = YES
46 Correct 5 ms 2816 KB answer = YES
47 Correct 3 ms 2816 KB answer = YES
48 Correct 3 ms 2816 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB answer = YES
2 Correct 3 ms 2688 KB answer = YES
3 Correct 2 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 2 ms 2688 KB answer = YES
11 Correct 2 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 2 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 3 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 2 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 2720 KB answer = YES
26 Correct 3 ms 2688 KB answer = YES
27 Correct 3 ms 2688 KB answer = YES
28 Correct 2 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 2 ms 2688 KB answer = YES
37 Correct 4 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 8 ms 2816 KB answer = YES
44 Correct 7 ms 2816 KB answer = YES
45 Correct 3 ms 2816 KB answer = YES
46 Correct 5 ms 2816 KB answer = YES
47 Correct 3 ms 2816 KB answer = YES
48 Correct 3 ms 2816 KB answer = YES
49 Correct 48 ms 3576 KB answer = YES
50 Correct 14 ms 3712 KB answer = YES
51 Correct 46 ms 3704 KB answer = YES
52 Correct 39 ms 3584 KB answer = NO
53 Correct 6 ms 2816 KB answer = YES
54 Correct 13 ms 2944 KB answer = YES
55 Correct 25 ms 3200 KB answer = YES
56 Correct 47 ms 3576 KB answer = YES
57 Correct 53 ms 3576 KB answer = YES
58 Correct 41 ms 3448 KB answer = YES
59 Correct 12 ms 3456 KB answer = YES
60 Correct 46 ms 3580 KB answer = YES
61 Correct 8 ms 3200 KB answer = YES
62 Correct 77 ms 7996 KB answer = NO
63 Correct 81 ms 8056 KB answer = YES
64 Correct 76 ms 7928 KB answer = NO
65 Correct 93 ms 8184 KB answer = YES
66 Correct 11 ms 2816 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB answer = YES
2 Correct 3 ms 2688 KB answer = YES
3 Correct 2 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 2 ms 2688 KB answer = YES
11 Correct 2 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 2 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 3 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 2 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 2720 KB answer = YES
26 Correct 3 ms 2688 KB answer = YES
27 Correct 3 ms 2688 KB answer = YES
28 Correct 2 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 2 ms 2688 KB answer = YES
37 Correct 4 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 8 ms 2816 KB answer = YES
44 Correct 7 ms 2816 KB answer = YES
45 Correct 3 ms 2816 KB answer = YES
46 Correct 5 ms 2816 KB answer = YES
47 Correct 3 ms 2816 KB answer = YES
48 Correct 3 ms 2816 KB answer = YES
49 Correct 48 ms 3576 KB answer = YES
50 Correct 14 ms 3712 KB answer = YES
51 Correct 46 ms 3704 KB answer = YES
52 Correct 39 ms 3584 KB answer = NO
53 Correct 6 ms 2816 KB answer = YES
54 Correct 13 ms 2944 KB answer = YES
55 Correct 25 ms 3200 KB answer = YES
56 Correct 47 ms 3576 KB answer = YES
57 Correct 53 ms 3576 KB answer = YES
58 Correct 41 ms 3448 KB answer = YES
59 Correct 12 ms 3456 KB answer = YES
60 Correct 46 ms 3580 KB answer = YES
61 Correct 8 ms 3200 KB answer = YES
62 Correct 77 ms 7996 KB answer = NO
63 Correct 81 ms 8056 KB answer = YES
64 Correct 76 ms 7928 KB answer = NO
65 Correct 93 ms 8184 KB answer = YES
66 Correct 11 ms 2816 KB answer = YES
67 Correct 419 ms 14960 KB answer = YES
68 Correct 414 ms 14828 KB answer = YES
69 Correct 107 ms 14832 KB answer = YES
70 Correct 150 ms 17904 KB answer = YES
71 Correct 102 ms 14792 KB answer = YES
72 Correct 666 ms 10744 KB answer = YES
73 Correct 184 ms 12144 KB answer = YES
74 Correct 90 ms 10996 KB answer = YES
75 Correct 340 ms 9972 KB answer = NO
76 Correct 56 ms 3832 KB answer = YES
77 Correct 111 ms 5112 KB answer = YES
78 Correct 230 ms 6788 KB answer = YES
79 Correct 539 ms 10736 KB answer = YES
80 Correct 407 ms 10996 KB answer = YES
81 Correct 88 ms 12656 KB answer = NO
82 Correct 147 ms 15856 KB answer = YES
83 Correct 207 ms 16372 KB answer = YES
84 Execution timed out 716 ms 16240 KB Time limit exceeded
85 Halted 0 ms 0 KB -