Submission #806363

# Submission time Handle Problem Language Result Execution time Memory
806363 2023-08-04T06:16:28 Z pawned Graph (BOI20_graph) C++17
100 / 100
195 ms 30292 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;

int N, M;

vector<pair<ii, ll>> edges;

vector<pair<int, ll>> adj[100005];

ll parity[100005];		// +x or -x
ll base[100005];	// [k] +- x

bool vis[100005];

vi comp;

void dfs(int n) {
	vis[n] = true;
	comp.pb(n);
	for (pair<int, ll> i : adj[n]) {
		if (!vis[i.fi]) {
			parity[i.fi] = -parity[n];
			base[i.fi] = i.se - base[n];
			dfs(i.fi);
		}
	}
}

bool solve() {
	cin>>N>>M;
	for (int i = 0; i < M; i++) {
		int u, v;
		cin>>u>>v;
		u--; v--;
		ll w;
		cin>>w;
		w *= 2;	// multiply by 2 to avoid .5 decimals
		edges.pb({{u, v}, w});
		adj[u].pb({v, w});
		adj[v].pb({u, w});
	}
	ll total = 0;
	ll ans[N] = {0};
	for (int i = 0; i < N; i++) {
		if (vis[i])
			continue;
		comp.clear();
		parity[i] = 1;
		base[i] = 0;
		dfs(i);
/*
		cout<<"comp: ";
		for (int j : comp) {
			cout<<"{"<<j<<", "<<parity[j]<<", "<<base[j]<<"}; ";
		}
		cout<<endl;
*/
		ll xval = 1e9;
		for (int j : comp) {
//			cout<<"at "<<j<<endl;
			for (pair<int, ll> k : adj[j]) {
				ll pari1 = parity[j];
				ll base1 = base[j];
				ll pari2 = parity[k.fi];
				ll base2 = base[k.fi];
				if (pari1 != pari2) {
					if (base1 + base2 != k.se)
						return false;
				}
				else {
//					cout<<"tight: "<<j<<" "<<k.fi<<endl;
					ll parisum = pari1 + pari2;	// either -2x or 2x
					ll basesum = base1 + base2;
					// goal is to sum to k.se
					ll res = (k.se - basesum) / parisum;
					// no need to div by 2 again since parisum is already double
//					cout<<"needed res: "<<res<<endl;
					if (xval == 1e9)
						xval = res;
					else {
						if (xval != res)
							return false;
					}
				}
			}
		}
//		cout<<"xval: "<<xval<<endl;
		if (xval != 1e9) {
			ll sum = 0;
			for (int j : comp) {
				ans[j] = xval * parity[j] + base[j];
				sum += abs(xval * parity[j] + base[j]);
			}
			total += sum;
		}
		else {
			// any median will do
			ll K = comp.size();
			ll all[K];
			for (int j = 0; j < K; j++) {
				all[j] = parity[comp[j]] * base[comp[j]];
			}
/*
			for (int j : all)
				cout<<"yay "<<j<<endl;
*/
			sort(all, all + K);
			ll median = -all[K / 2];	// make this zero
//			cout<<"median is "<<median<<endl;
			ll sum = 0;
			for (int j : comp) {
				sum += abs(parity[j] * median + base[j]);
				ans[j] = parity[j] * median + base[j];
			}
			total += sum;
		}
		// made a tree, now check all edges are satisfied
	}
//	cout<<"total: "<<total<<endl;
	cout<<"YES"<<endl;
	for (int i = 0; i < N; i++) {
		printf("%.10f ", ans[i] / 2.0);
	}
	cout<<endl;
	return true;
}

int main() {
	if (!solve())
		cout<<"NO"<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB answer = YES
2 Correct 1 ms 2644 KB answer = YES
3 Correct 1 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 1 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2644 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 1 ms 2644 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 1 ms 2644 KB answer = YES
14 Correct 2 ms 2644 KB answer = YES
15 Correct 1 ms 2644 KB answer = YES
16 Correct 1 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 1 ms 2644 KB answer = YES
19 Correct 1 ms 2644 KB answer = YES
20 Correct 1 ms 2644 KB answer = YES
21 Correct 2 ms 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB answer = YES
2 Correct 1 ms 2644 KB answer = YES
3 Correct 1 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 1 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2644 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 1 ms 2644 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 1 ms 2644 KB answer = YES
14 Correct 2 ms 2644 KB answer = YES
15 Correct 1 ms 2644 KB answer = YES
16 Correct 1 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 1 ms 2644 KB answer = YES
19 Correct 1 ms 2644 KB answer = YES
20 Correct 1 ms 2644 KB answer = YES
21 Correct 2 ms 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
24 Correct 1 ms 2644 KB answer = YES
25 Correct 1 ms 2560 KB answer = YES
26 Correct 1 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 1 ms 2644 KB answer = YES
29 Correct 2 ms 2644 KB answer = YES
30 Correct 1 ms 2644 KB answer = NO
31 Correct 1 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 2 ms 2644 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 1 ms 2644 KB answer = YES
36 Correct 1 ms 2644 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB answer = YES
2 Correct 1 ms 2644 KB answer = YES
3 Correct 1 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 1 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2644 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 1 ms 2644 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 1 ms 2644 KB answer = YES
14 Correct 2 ms 2644 KB answer = YES
15 Correct 1 ms 2644 KB answer = YES
16 Correct 1 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 1 ms 2644 KB answer = YES
19 Correct 1 ms 2644 KB answer = YES
20 Correct 1 ms 2644 KB answer = YES
21 Correct 2 ms 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
24 Correct 1 ms 2644 KB answer = YES
25 Correct 1 ms 2560 KB answer = YES
26 Correct 1 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 1 ms 2644 KB answer = YES
29 Correct 2 ms 2644 KB answer = YES
30 Correct 1 ms 2644 KB answer = NO
31 Correct 1 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 2 ms 2644 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 1 ms 2644 KB answer = YES
36 Correct 1 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 2644 KB answer = YES
40 Correct 2 ms 2788 KB answer = YES
41 Correct 2 ms 2772 KB answer = NO
42 Correct 2 ms 2664 KB answer = YES
43 Correct 2 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 2644 KB answer = YES
47 Correct 3 ms 2772 KB answer = YES
48 Correct 2 ms 2792 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB answer = YES
2 Correct 1 ms 2644 KB answer = YES
3 Correct 1 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 1 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2644 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 1 ms 2644 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 1 ms 2644 KB answer = YES
14 Correct 2 ms 2644 KB answer = YES
15 Correct 1 ms 2644 KB answer = YES
16 Correct 1 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 1 ms 2644 KB answer = YES
19 Correct 1 ms 2644 KB answer = YES
20 Correct 1 ms 2644 KB answer = YES
21 Correct 2 ms 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
24 Correct 1 ms 2644 KB answer = YES
25 Correct 1 ms 2560 KB answer = YES
26 Correct 1 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 1 ms 2644 KB answer = YES
29 Correct 2 ms 2644 KB answer = YES
30 Correct 1 ms 2644 KB answer = NO
31 Correct 1 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 2 ms 2644 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 1 ms 2644 KB answer = YES
36 Correct 1 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 2644 KB answer = YES
40 Correct 2 ms 2788 KB answer = YES
41 Correct 2 ms 2772 KB answer = NO
42 Correct 2 ms 2664 KB answer = YES
43 Correct 2 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 2644 KB answer = YES
47 Correct 3 ms 2772 KB answer = YES
48 Correct 2 ms 2792 KB answer = YES
49 Correct 10 ms 3920 KB answer = YES
50 Correct 10 ms 4208 KB answer = YES
51 Correct 11 ms 4236 KB answer = YES
52 Correct 7 ms 4068 KB answer = NO
53 Correct 3 ms 2772 KB answer = YES
54 Correct 3 ms 2900 KB answer = YES
55 Correct 5 ms 3284 KB answer = YES
56 Correct 10 ms 3920 KB answer = YES
57 Correct 9 ms 3792 KB answer = YES
58 Correct 9 ms 3760 KB answer = YES
59 Correct 9 ms 3760 KB answer = YES
60 Correct 10 ms 3932 KB answer = YES
61 Correct 6 ms 3252 KB answer = YES
62 Correct 104 ms 17896 KB answer = NO
63 Correct 138 ms 18056 KB answer = YES
64 Correct 103 ms 17928 KB answer = NO
65 Correct 111 ms 18024 KB answer = YES
66 Correct 3 ms 2900 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB answer = YES
2 Correct 1 ms 2644 KB answer = YES
3 Correct 1 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 1 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2644 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 1 ms 2644 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 1 ms 2644 KB answer = YES
14 Correct 2 ms 2644 KB answer = YES
15 Correct 1 ms 2644 KB answer = YES
16 Correct 1 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 1 ms 2644 KB answer = YES
19 Correct 1 ms 2644 KB answer = YES
20 Correct 1 ms 2644 KB answer = YES
21 Correct 2 ms 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
24 Correct 1 ms 2644 KB answer = YES
25 Correct 1 ms 2560 KB answer = YES
26 Correct 1 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 1 ms 2644 KB answer = YES
29 Correct 2 ms 2644 KB answer = YES
30 Correct 1 ms 2644 KB answer = NO
31 Correct 1 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 2 ms 2644 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 1 ms 2644 KB answer = YES
36 Correct 1 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 2644 KB answer = YES
40 Correct 2 ms 2788 KB answer = YES
41 Correct 2 ms 2772 KB answer = NO
42 Correct 2 ms 2664 KB answer = YES
43 Correct 2 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 2644 KB answer = YES
47 Correct 3 ms 2772 KB answer = YES
48 Correct 2 ms 2792 KB answer = YES
49 Correct 10 ms 3920 KB answer = YES
50 Correct 10 ms 4208 KB answer = YES
51 Correct 11 ms 4236 KB answer = YES
52 Correct 7 ms 4068 KB answer = NO
53 Correct 3 ms 2772 KB answer = YES
54 Correct 3 ms 2900 KB answer = YES
55 Correct 5 ms 3284 KB answer = YES
56 Correct 10 ms 3920 KB answer = YES
57 Correct 9 ms 3792 KB answer = YES
58 Correct 9 ms 3760 KB answer = YES
59 Correct 9 ms 3760 KB answer = YES
60 Correct 10 ms 3932 KB answer = YES
61 Correct 6 ms 3252 KB answer = YES
62 Correct 104 ms 17896 KB answer = NO
63 Correct 138 ms 18056 KB answer = YES
64 Correct 103 ms 17928 KB answer = NO
65 Correct 111 ms 18024 KB answer = YES
66 Correct 3 ms 2900 KB answer = YES
67 Correct 96 ms 21216 KB answer = YES
68 Correct 86 ms 21104 KB answer = YES
69 Correct 90 ms 21160 KB answer = YES
70 Correct 152 ms 30292 KB answer = YES
71 Correct 99 ms 21104 KB answer = YES
72 Correct 96 ms 16156 KB answer = YES
73 Correct 96 ms 15288 KB answer = YES
74 Correct 65 ms 13756 KB answer = YES
75 Correct 43 ms 12844 KB answer = NO
76 Correct 15 ms 4180 KB answer = YES
77 Correct 22 ms 5836 KB answer = YES
78 Correct 38 ms 8372 KB answer = YES
79 Correct 92 ms 14188 KB answer = YES
80 Correct 66 ms 13844 KB answer = YES
81 Correct 67 ms 16824 KB answer = NO
82 Correct 103 ms 20748 KB answer = YES
83 Correct 111 ms 21836 KB answer = YES
84 Correct 109 ms 21808 KB answer = YES
85 Correct 96 ms 21224 KB answer = YES
86 Correct 102 ms 21176 KB answer = YES
87 Correct 71 ms 15272 KB answer = NO
88 Correct 116 ms 17600 KB answer = YES
89 Correct 118 ms 14032 KB answer = YES
90 Correct 90 ms 13912 KB answer = YES
91 Correct 91 ms 14520 KB answer = YES
92 Correct 44 ms 9032 KB answer = YES
93 Correct 47 ms 8984 KB answer = YES
94 Correct 85 ms 20416 KB answer = NO
95 Correct 73 ms 13180 KB answer = NO
96 Correct 195 ms 27232 KB answer = YES
97 Correct 62 ms 19768 KB answer = NO