Submission #729790

# Submission time Handle Problem Language Result Execution time Memory
729790 2023-04-24T14:48:57 Z WonderfulWhale Graph (BOI20_graph) C++17
100 / 100
218 ms 38944 KB
#include<bits/stdc++.h>
using namespace std;

#define int int64_t
#define pb push_back
#define st first
#define nd second
#define pii pair<int, int>
#define double long double
#define pdd pair<double, double>
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

const int MAXN = 100009;
const double eps = 1e-9;

bool f(double a, double b) {
	return abs(a-b)<eps; 
}

vector<pair<int, double>> G[MAXN];
bool vis[MAXN];
pdd val[MAXN];
double ans[MAXN];

vector<int> v;
void dfs(int x) {
	v.pb(x);
	vis[x] = true;
	for(auto [y, d]:G[x]) {
		if(vis[y]) continue;
		val[y] = {-val[x].st, d-val[x].nd};
		dfs(y);
	}
}

int32_t main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cout << fixed;
	cout.precision(10);

	int n, m;
	cin >> n >> m;
	for(int i=0;i<m;i++) {
		int a, b, c;
		cin >> a >> b >> c;
		G[a].pb({b, c});
		G[b].pb({a, c});
	}
	for(int i=1;i<=n;i++) {
		if(vis[i]) continue;
		v.clear();
		val[i] = {1, 0};
		dfs(i);
		pair<bool, double> forced = {false, 0};
		for(int x:v) {
			for(auto [y, d]:G[x]) {
				if(f(val[x].st+val[y].st, 0)) {
					if(!f(val[x].nd+val[y].nd, d)) {
						cout << "NO\n";
						return 0;
					}
				} else {
					double nx = (d-val[x].nd-val[y].nd)/(val[x].st+val[y].st);
					if(forced.st) {
						if(!f(forced.nd, nx)) {
							cout << "NO\n";
							return 0;
						}
					} else {
						forced = {true, nx};
					}
	 			}
			}
		}
		if(forced.st) {
			for(int x:v) {
				ans[x] = forced.nd*val[x].st+val[x].nd;
			}
			continue;
		}
		pdd sum = {0, 0};
		vector<pair<double, pdd>> V;
		for(int x:v) {
			int root = -val[x].nd/val[x].st;
			sum.st -= 1;
			if(val[x].st>0) {
				sum.nd -= val[x].nd;
				V.pb({root, {2, val[x].nd*2}});
			} else {
				sum.nd += val[x].nd;
				V.pb({root, {2, -val[x].nd*2}});
			}
		}
		sort(all(V));
		if(sum.st>0) {
			cout << "NO\n";
			return 0;
		}
		pdd res = {sum.st*V[0].st+sum.nd, V[0].st};
		for(auto x:V) {
			sum.st += x.nd.st;
			sum.nd += x.nd.nd;
			res = min(res, {sum.st*x.st+sum.nd, x.st});
		}
		if(sum.st<0) {
			cout << "NO\n";
			return 0;
		}
		for(int x:v) {
			ans[x] = res.nd*val[x].st+val[x].nd;
		}
	}
	cout << "YES\n";
	for(int i=1;i<=n;i++) {
		cout << ans[i] << " ";
	}
	cout << "\n";
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 2 ms 2644 KB answer = YES
4 Correct 2 ms 2684 KB answer = NO
5 Correct 2 ms 2680 KB answer = YES
6 Correct 2 ms 2644 KB answer = YES
7 Correct 2 ms 2644 KB answer = YES
8 Correct 2 ms 2644 KB answer = YES
9 Correct 3 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2680 KB answer = YES
12 Correct 2 ms 2680 KB answer = NO
13 Correct 2 ms 2648 KB answer = YES
14 Correct 2 ms 2684 KB answer = YES
15 Correct 2 ms 2644 KB answer = YES
16 Correct 2 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 2 ms 2676 KB answer = YES
19 Correct 3 ms 2644 KB answer = YES
20 Correct 3 ms 2644 KB answer = YES
21 Correct 2 ms 2680 KB answer = YES
22 Correct 2 ms 2644 KB answer = NO
23 Correct 2 ms 2644 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 2 ms 2644 KB answer = YES
4 Correct 2 ms 2684 KB answer = NO
5 Correct 2 ms 2680 KB answer = YES
6 Correct 2 ms 2644 KB answer = YES
7 Correct 2 ms 2644 KB answer = YES
8 Correct 2 ms 2644 KB answer = YES
9 Correct 3 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2680 KB answer = YES
12 Correct 2 ms 2680 KB answer = NO
13 Correct 2 ms 2648 KB answer = YES
14 Correct 2 ms 2684 KB answer = YES
15 Correct 2 ms 2644 KB answer = YES
16 Correct 2 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 2 ms 2676 KB answer = YES
19 Correct 3 ms 2644 KB answer = YES
20 Correct 3 ms 2644 KB answer = YES
21 Correct 2 ms 2680 KB answer = YES
22 Correct 2 ms 2644 KB answer = NO
23 Correct 2 ms 2644 KB answer = NO
24 Correct 2 ms 2644 KB answer = YES
25 Correct 2 ms 2676 KB answer = YES
26 Correct 2 ms 2680 KB answer = YES
27 Correct 3 ms 2620 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 3 ms 2676 KB answer = YES
30 Correct 2 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 2 ms 2644 KB answer = YES
33 Correct 2 ms 2680 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 3 ms 2644 KB answer = YES
36 Correct 2 ms 2680 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 2 ms 2644 KB answer = YES
4 Correct 2 ms 2684 KB answer = NO
5 Correct 2 ms 2680 KB answer = YES
6 Correct 2 ms 2644 KB answer = YES
7 Correct 2 ms 2644 KB answer = YES
8 Correct 2 ms 2644 KB answer = YES
9 Correct 3 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2680 KB answer = YES
12 Correct 2 ms 2680 KB answer = NO
13 Correct 2 ms 2648 KB answer = YES
14 Correct 2 ms 2684 KB answer = YES
15 Correct 2 ms 2644 KB answer = YES
16 Correct 2 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 2 ms 2676 KB answer = YES
19 Correct 3 ms 2644 KB answer = YES
20 Correct 3 ms 2644 KB answer = YES
21 Correct 2 ms 2680 KB answer = YES
22 Correct 2 ms 2644 KB answer = NO
23 Correct 2 ms 2644 KB answer = NO
24 Correct 2 ms 2644 KB answer = YES
25 Correct 2 ms 2676 KB answer = YES
26 Correct 2 ms 2680 KB answer = YES
27 Correct 3 ms 2620 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 3 ms 2676 KB answer = YES
30 Correct 2 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 2 ms 2644 KB answer = YES
33 Correct 2 ms 2680 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 3 ms 2644 KB answer = YES
36 Correct 2 ms 2680 KB answer = YES
37 Correct 2 ms 2644 KB answer = YES
38 Correct 2 ms 2644 KB answer = YES
39 Correct 2 ms 2812 KB answer = YES
40 Correct 4 ms 2772 KB answer = YES
41 Correct 2 ms 2772 KB answer = NO
42 Correct 4 ms 2900 KB answer = YES
43 Correct 3 ms 2900 KB answer = YES
44 Correct 4 ms 2812 KB answer = YES
45 Correct 4 ms 2776 KB answer = YES
46 Correct 3 ms 2772 KB answer = YES
47 Correct 3 ms 2772 KB answer = YES
48 Correct 3 ms 2772 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 2 ms 2644 KB answer = YES
4 Correct 2 ms 2684 KB answer = NO
5 Correct 2 ms 2680 KB answer = YES
6 Correct 2 ms 2644 KB answer = YES
7 Correct 2 ms 2644 KB answer = YES
8 Correct 2 ms 2644 KB answer = YES
9 Correct 3 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2680 KB answer = YES
12 Correct 2 ms 2680 KB answer = NO
13 Correct 2 ms 2648 KB answer = YES
14 Correct 2 ms 2684 KB answer = YES
15 Correct 2 ms 2644 KB answer = YES
16 Correct 2 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 2 ms 2676 KB answer = YES
19 Correct 3 ms 2644 KB answer = YES
20 Correct 3 ms 2644 KB answer = YES
21 Correct 2 ms 2680 KB answer = YES
22 Correct 2 ms 2644 KB answer = NO
23 Correct 2 ms 2644 KB answer = NO
24 Correct 2 ms 2644 KB answer = YES
25 Correct 2 ms 2676 KB answer = YES
26 Correct 2 ms 2680 KB answer = YES
27 Correct 3 ms 2620 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 3 ms 2676 KB answer = YES
30 Correct 2 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 2 ms 2644 KB answer = YES
33 Correct 2 ms 2680 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 3 ms 2644 KB answer = YES
36 Correct 2 ms 2680 KB answer = YES
37 Correct 2 ms 2644 KB answer = YES
38 Correct 2 ms 2644 KB answer = YES
39 Correct 2 ms 2812 KB answer = YES
40 Correct 4 ms 2772 KB answer = YES
41 Correct 2 ms 2772 KB answer = NO
42 Correct 4 ms 2900 KB answer = YES
43 Correct 3 ms 2900 KB answer = YES
44 Correct 4 ms 2812 KB answer = YES
45 Correct 4 ms 2776 KB answer = YES
46 Correct 3 ms 2772 KB answer = YES
47 Correct 3 ms 2772 KB answer = YES
48 Correct 3 ms 2772 KB answer = YES
49 Correct 20 ms 4928 KB answer = YES
50 Correct 18 ms 4692 KB answer = YES
51 Correct 13 ms 5232 KB answer = YES
52 Correct 5 ms 4436 KB answer = NO
53 Correct 3 ms 2944 KB answer = YES
54 Correct 7 ms 3292 KB answer = YES
55 Correct 9 ms 3936 KB answer = YES
56 Correct 21 ms 4944 KB answer = YES
57 Correct 12 ms 4400 KB answer = YES
58 Correct 16 ms 4508 KB answer = YES
59 Correct 10 ms 4224 KB answer = YES
60 Correct 13 ms 4688 KB answer = YES
61 Correct 6 ms 3588 KB answer = YES
62 Correct 69 ms 23888 KB answer = NO
63 Correct 93 ms 24060 KB answer = YES
64 Correct 84 ms 23884 KB answer = NO
65 Correct 88 ms 24020 KB answer = YES
66 Correct 3 ms 3028 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 2 ms 2644 KB answer = YES
4 Correct 2 ms 2684 KB answer = NO
5 Correct 2 ms 2680 KB answer = YES
6 Correct 2 ms 2644 KB answer = YES
7 Correct 2 ms 2644 KB answer = YES
8 Correct 2 ms 2644 KB answer = YES
9 Correct 3 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2680 KB answer = YES
12 Correct 2 ms 2680 KB answer = NO
13 Correct 2 ms 2648 KB answer = YES
14 Correct 2 ms 2684 KB answer = YES
15 Correct 2 ms 2644 KB answer = YES
16 Correct 2 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 2 ms 2676 KB answer = YES
19 Correct 3 ms 2644 KB answer = YES
20 Correct 3 ms 2644 KB answer = YES
21 Correct 2 ms 2680 KB answer = YES
22 Correct 2 ms 2644 KB answer = NO
23 Correct 2 ms 2644 KB answer = NO
24 Correct 2 ms 2644 KB answer = YES
25 Correct 2 ms 2676 KB answer = YES
26 Correct 2 ms 2680 KB answer = YES
27 Correct 3 ms 2620 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 3 ms 2676 KB answer = YES
30 Correct 2 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 2 ms 2644 KB answer = YES
33 Correct 2 ms 2680 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 3 ms 2644 KB answer = YES
36 Correct 2 ms 2680 KB answer = YES
37 Correct 2 ms 2644 KB answer = YES
38 Correct 2 ms 2644 KB answer = YES
39 Correct 2 ms 2812 KB answer = YES
40 Correct 4 ms 2772 KB answer = YES
41 Correct 2 ms 2772 KB answer = NO
42 Correct 4 ms 2900 KB answer = YES
43 Correct 3 ms 2900 KB answer = YES
44 Correct 4 ms 2812 KB answer = YES
45 Correct 4 ms 2776 KB answer = YES
46 Correct 3 ms 2772 KB answer = YES
47 Correct 3 ms 2772 KB answer = YES
48 Correct 3 ms 2772 KB answer = YES
49 Correct 20 ms 4928 KB answer = YES
50 Correct 18 ms 4692 KB answer = YES
51 Correct 13 ms 5232 KB answer = YES
52 Correct 5 ms 4436 KB answer = NO
53 Correct 3 ms 2944 KB answer = YES
54 Correct 7 ms 3292 KB answer = YES
55 Correct 9 ms 3936 KB answer = YES
56 Correct 21 ms 4944 KB answer = YES
57 Correct 12 ms 4400 KB answer = YES
58 Correct 16 ms 4508 KB answer = YES
59 Correct 10 ms 4224 KB answer = YES
60 Correct 13 ms 4688 KB answer = YES
61 Correct 6 ms 3588 KB answer = YES
62 Correct 69 ms 23888 KB answer = NO
63 Correct 93 ms 24060 KB answer = YES
64 Correct 84 ms 23884 KB answer = NO
65 Correct 88 ms 24020 KB answer = YES
66 Correct 3 ms 3028 KB answer = YES
67 Correct 131 ms 29064 KB answer = YES
68 Correct 132 ms 29128 KB answer = YES
69 Correct 92 ms 25112 KB answer = YES
70 Correct 142 ms 38944 KB answer = YES
71 Correct 94 ms 25172 KB answer = YES
72 Correct 179 ms 23696 KB answer = YES
73 Correct 152 ms 19692 KB answer = YES
74 Correct 77 ms 16576 KB answer = YES
75 Correct 35 ms 14588 KB answer = NO
76 Correct 21 ms 5264 KB answer = YES
77 Correct 43 ms 7728 KB answer = YES
78 Correct 66 ms 12152 KB answer = YES
79 Correct 143 ms 21664 KB answer = YES
80 Correct 99 ms 18864 KB answer = YES
81 Correct 61 ms 19188 KB answer = NO
82 Correct 162 ms 24684 KB answer = YES
83 Correct 148 ms 25916 KB answer = YES
84 Correct 205 ms 30152 KB answer = YES
85 Correct 123 ms 29092 KB answer = YES
86 Correct 103 ms 25192 KB answer = YES
87 Correct 61 ms 19400 KB answer = NO
88 Correct 152 ms 22092 KB answer = YES
89 Correct 99 ms 17836 KB answer = YES
90 Correct 105 ms 17892 KB answer = YES
91 Correct 161 ms 18564 KB answer = YES
92 Correct 55 ms 11280 KB answer = YES
93 Correct 56 ms 11268 KB answer = YES
94 Correct 79 ms 22928 KB answer = NO
95 Correct 55 ms 17276 KB answer = NO
96 Correct 218 ms 35004 KB answer = YES
97 Correct 40 ms 22160 KB answer = NO