답안 #806343

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
806343 2023-08-04T06:08:14 Z pawned Graph (BOI20_graph) C++17
5 / 100
2 ms 2660 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[j] * base[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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 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 2 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 2 ms 2644 KB answer = YES
11 Correct 2 ms 2656 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 1 ms 2640 KB answer = YES
15 Correct 1 ms 2644 KB answer = YES
16 Correct 1 ms 2656 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 2 ms 2644 KB answer = YES
19 Correct 1 ms 2656 KB answer = YES
20 Correct 1 ms 2656 KB answer = YES
21 Correct 1 ms 2644 KB answer = YES
22 Correct 1 ms 2652 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 2 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 2 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 2 ms 2644 KB answer = YES
11 Correct 2 ms 2656 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 1 ms 2640 KB answer = YES
15 Correct 1 ms 2644 KB answer = YES
16 Correct 1 ms 2656 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 2 ms 2644 KB answer = YES
19 Correct 1 ms 2656 KB answer = YES
20 Correct 1 ms 2656 KB answer = YES
21 Correct 1 ms 2644 KB answer = YES
22 Correct 1 ms 2652 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
24 Correct 2 ms 2660 KB answer = YES
25 Correct 1 ms 2600 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2572 KB answer = YES
28 Correct 1 ms 2644 KB answer = YES
29 Correct 1 ms 2644 KB answer = YES
30 Correct 1 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 1 ms 2656 KB answer = YES
34 Incorrect 2 ms 2656 KB participant answer is larger than the answer of jury
35 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 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 2 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 2 ms 2644 KB answer = YES
11 Correct 2 ms 2656 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 1 ms 2640 KB answer = YES
15 Correct 1 ms 2644 KB answer = YES
16 Correct 1 ms 2656 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 2 ms 2644 KB answer = YES
19 Correct 1 ms 2656 KB answer = YES
20 Correct 1 ms 2656 KB answer = YES
21 Correct 1 ms 2644 KB answer = YES
22 Correct 1 ms 2652 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
24 Correct 2 ms 2660 KB answer = YES
25 Correct 1 ms 2600 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2572 KB answer = YES
28 Correct 1 ms 2644 KB answer = YES
29 Correct 1 ms 2644 KB answer = YES
30 Correct 1 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 1 ms 2656 KB answer = YES
34 Incorrect 2 ms 2656 KB participant answer is larger than the answer of jury
35 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 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 2 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 2 ms 2644 KB answer = YES
11 Correct 2 ms 2656 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 1 ms 2640 KB answer = YES
15 Correct 1 ms 2644 KB answer = YES
16 Correct 1 ms 2656 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 2 ms 2644 KB answer = YES
19 Correct 1 ms 2656 KB answer = YES
20 Correct 1 ms 2656 KB answer = YES
21 Correct 1 ms 2644 KB answer = YES
22 Correct 1 ms 2652 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
24 Correct 2 ms 2660 KB answer = YES
25 Correct 1 ms 2600 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2572 KB answer = YES
28 Correct 1 ms 2644 KB answer = YES
29 Correct 1 ms 2644 KB answer = YES
30 Correct 1 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 1 ms 2656 KB answer = YES
34 Incorrect 2 ms 2656 KB participant answer is larger than the answer of jury
35 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 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 2 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 2 ms 2644 KB answer = YES
11 Correct 2 ms 2656 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 1 ms 2640 KB answer = YES
15 Correct 1 ms 2644 KB answer = YES
16 Correct 1 ms 2656 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 2 ms 2644 KB answer = YES
19 Correct 1 ms 2656 KB answer = YES
20 Correct 1 ms 2656 KB answer = YES
21 Correct 1 ms 2644 KB answer = YES
22 Correct 1 ms 2652 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
24 Correct 2 ms 2660 KB answer = YES
25 Correct 1 ms 2600 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2572 KB answer = YES
28 Correct 1 ms 2644 KB answer = YES
29 Correct 1 ms 2644 KB answer = YES
30 Correct 1 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 1 ms 2656 KB answer = YES
34 Incorrect 2 ms 2656 KB participant answer is larger than the answer of jury
35 Halted 0 ms 0 KB -