Submission #729794

# Submission time Handle Problem Language Result Execution time Memory
729794 2023-04-24T14:51:28 Z WonderfulWhale Graph (BOI20_graph) C++17
100 / 100
251 ms 36208 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) {
			return 0;
			cout << "NO\n";
		}
		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) {
			return 0;
			cout << "NO\n";
		}
		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 2644 KB answer = NO
5 Correct 2 ms 2644 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 2 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 2 ms 2644 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 2644 KB answer = YES
19 Correct 2 ms 2644 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 1 ms 2644 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 2644 KB answer = NO
5 Correct 2 ms 2644 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 2 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 2 ms 2644 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 2644 KB answer = YES
19 Correct 2 ms 2644 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 1 ms 2644 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 2644 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 1 ms 2644 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 2 ms 2644 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 2644 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 2 ms 2644 KB answer = YES
36 Correct 2 ms 2644 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 2644 KB answer = NO
5 Correct 2 ms 2644 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 2 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 2 ms 2644 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 2644 KB answer = YES
19 Correct 2 ms 2644 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 1 ms 2644 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 2644 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 1 ms 2644 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 2 ms 2644 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 2644 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 2 ms 2644 KB answer = YES
36 Correct 2 ms 2644 KB answer = YES
37 Correct 2 ms 2644 KB answer = YES
38 Correct 2 ms 2644 KB answer = YES
39 Correct 2 ms 2772 KB answer = YES
40 Correct 3 ms 2772 KB answer = YES
41 Correct 2 ms 2772 KB answer = NO
42 Correct 3 ms 2900 KB answer = YES
43 Correct 3 ms 2772 KB answer = YES
44 Correct 2 ms 2772 KB answer = YES
45 Correct 2 ms 2772 KB answer = YES
46 Correct 2 ms 2772 KB answer = YES
47 Correct 3 ms 2772 KB answer = YES
48 Correct 3 ms 2768 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 2644 KB answer = NO
5 Correct 2 ms 2644 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 2 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 2 ms 2644 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 2644 KB answer = YES
19 Correct 2 ms 2644 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 1 ms 2644 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 2644 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 1 ms 2644 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 2 ms 2644 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 2644 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 2 ms 2644 KB answer = YES
36 Correct 2 ms 2644 KB answer = YES
37 Correct 2 ms 2644 KB answer = YES
38 Correct 2 ms 2644 KB answer = YES
39 Correct 2 ms 2772 KB answer = YES
40 Correct 3 ms 2772 KB answer = YES
41 Correct 2 ms 2772 KB answer = NO
42 Correct 3 ms 2900 KB answer = YES
43 Correct 3 ms 2772 KB answer = YES
44 Correct 2 ms 2772 KB answer = YES
45 Correct 2 ms 2772 KB answer = YES
46 Correct 2 ms 2772 KB answer = YES
47 Correct 3 ms 2772 KB answer = YES
48 Correct 3 ms 2768 KB answer = YES
49 Correct 15 ms 4816 KB answer = YES
50 Correct 11 ms 4580 KB answer = YES
51 Correct 14 ms 5180 KB answer = YES
52 Correct 5 ms 4308 KB answer = NO
53 Correct 3 ms 2900 KB answer = YES
54 Correct 4 ms 3284 KB answer = YES
55 Correct 7 ms 3908 KB answer = YES
56 Correct 12 ms 4816 KB answer = YES
57 Correct 12 ms 4272 KB answer = YES
58 Correct 12 ms 4380 KB answer = YES
59 Correct 13 ms 4064 KB answer = YES
60 Correct 16 ms 4480 KB answer = YES
61 Correct 6 ms 3428 KB answer = YES
62 Correct 63 ms 21564 KB answer = NO
63 Correct 81 ms 21708 KB answer = YES
64 Correct 78 ms 21584 KB answer = NO
65 Correct 79 ms 21800 KB answer = YES
66 Correct 4 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 2644 KB answer = NO
5 Correct 2 ms 2644 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 2 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 2 ms 2644 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 2644 KB answer = YES
19 Correct 2 ms 2644 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 1 ms 2644 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 2644 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 1 ms 2644 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 2 ms 2644 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 2644 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 2 ms 2644 KB answer = YES
36 Correct 2 ms 2644 KB answer = YES
37 Correct 2 ms 2644 KB answer = YES
38 Correct 2 ms 2644 KB answer = YES
39 Correct 2 ms 2772 KB answer = YES
40 Correct 3 ms 2772 KB answer = YES
41 Correct 2 ms 2772 KB answer = NO
42 Correct 3 ms 2900 KB answer = YES
43 Correct 3 ms 2772 KB answer = YES
44 Correct 2 ms 2772 KB answer = YES
45 Correct 2 ms 2772 KB answer = YES
46 Correct 2 ms 2772 KB answer = YES
47 Correct 3 ms 2772 KB answer = YES
48 Correct 3 ms 2768 KB answer = YES
49 Correct 15 ms 4816 KB answer = YES
50 Correct 11 ms 4580 KB answer = YES
51 Correct 14 ms 5180 KB answer = YES
52 Correct 5 ms 4308 KB answer = NO
53 Correct 3 ms 2900 KB answer = YES
54 Correct 4 ms 3284 KB answer = YES
55 Correct 7 ms 3908 KB answer = YES
56 Correct 12 ms 4816 KB answer = YES
57 Correct 12 ms 4272 KB answer = YES
58 Correct 12 ms 4380 KB answer = YES
59 Correct 13 ms 4064 KB answer = YES
60 Correct 16 ms 4480 KB answer = YES
61 Correct 6 ms 3428 KB answer = YES
62 Correct 63 ms 21564 KB answer = NO
63 Correct 81 ms 21708 KB answer = YES
64 Correct 78 ms 21584 KB answer = NO
65 Correct 79 ms 21800 KB answer = YES
66 Correct 4 ms 3028 KB answer = YES
67 Correct 136 ms 27784 KB answer = YES
68 Correct 138 ms 27740 KB answer = YES
69 Correct 86 ms 23668 KB answer = YES
70 Correct 155 ms 36208 KB answer = YES
71 Correct 90 ms 23748 KB answer = YES
72 Correct 161 ms 22332 KB answer = YES
73 Correct 138 ms 18336 KB answer = YES
74 Correct 74 ms 15684 KB answer = YES
75 Correct 40 ms 13656 KB answer = NO
76 Correct 17 ms 5164 KB answer = YES
77 Correct 34 ms 7476 KB answer = YES
78 Correct 58 ms 11572 KB answer = YES
79 Correct 135 ms 20480 KB answer = YES
80 Correct 98 ms 18008 KB answer = YES
81 Correct 48 ms 17872 KB answer = NO
82 Correct 121 ms 23360 KB answer = YES
83 Correct 159 ms 24640 KB answer = YES
84 Correct 184 ms 28528 KB answer = YES
85 Correct 125 ms 27712 KB answer = YES
86 Correct 97 ms 23804 KB answer = YES
87 Correct 103 ms 18080 KB answer = NO
88 Correct 176 ms 20724 KB answer = YES
89 Correct 107 ms 16508 KB answer = YES
90 Correct 132 ms 16508 KB answer = YES
91 Correct 112 ms 17276 KB answer = YES
92 Correct 82 ms 10508 KB answer = YES
93 Correct 71 ms 10596 KB answer = YES
94 Correct 86 ms 21728 KB answer = NO
95 Correct 57 ms 16120 KB answer = NO
96 Correct 251 ms 32528 KB answer = YES
97 Correct 41 ms 21060 KB answer = NO