답안 #1055556

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1055556 2024-08-12T21:47:43 Z Nonoze Graph (BOI20_graph) C++17
100 / 100
296 ms 49424 KB
/*
*	Author: Nonoze
*	Created: Wednesday 07/08/2024
*/
#include <bits/stdc++.h>
using namespace std;


#ifndef _IN_LOCAL
	#define dbg(...)
#endif

#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) return (void)(cout << x << endl)

#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)

#define int long long
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=25;



void solve();

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tt=1;
	// cin >> tt;
	while(tt--) solve();
	return 0;
}




int n, k, m, q;
vector<pair<int, int>> adj[100000];
vector<bool> visited(100000);
int weights[100000], obligated[100000];

vector<vector<int>> contains;
vector<int> roots;

void dfspre(int u) {
	visited[u]=1;
	contains.back().pb(u);
	for (auto [v, w] : adj[u]) if (!visited[v]) dfspre(v);
}

void precalc() {
	for (int i=0; i<n; i++) if (!visited[i]) {
		contains.pb({});
		dfspre(i);
		roots.pb(i);
	}
}

int toadd=-inf;
vector<int> colors(100000);

int dfs(int u, bool color=0) {
	if (obligated[u]!=-inf && weights[u]!=obligated[u]) return inf;
	colors[u]=color;
	int act=abs(weights[u]);
	for (auto [v, w]: adj[u]) {
		if (weights[v]!=-inf) {
			if (weights[v]+weights[u]==w || colors[v]!=colors[u]) continue;
			toadd=w-(weights[u]+weights[v]);
			if (color) toadd*=-1;
			toadd/=2;
			continue;
		}
		if (weights[v]!=-inf) {
			if (weights[v]+weights[u]!=w) return inf;
			continue;
		}
		weights[v]=w-weights[u];
		int t=dfs(v, color^1);
		if (t==inf) return inf;
		act+=t;
	}
	return act;
}

int calcw(int src, int empl, int w) {
	toadd=inf;
	for (auto u: contains[empl]) weights[u]=-inf;
	weights[src]=w;
	return dfs(src);
}

int calc(int src, int empl) {
	int l=-1000, r=1000, ans=0;
	while (l<=r) {
		int mid=(l+r)/2;
		int t1=calcw(src, empl, mid-1), t2=calcw(src, empl, mid+1), t=calcw(src, empl, mid);
		if (t<t1 && t<t2) return t;
		if (t1<t2) r=mid-1, ans=mid-1;
		else l=mid+1, ans=mid+1;
	}
	calcw(src, empl, ans);
	return ans;
}

void solve() {
	cin >> n >> m;
	map<int, int> ver[n];
	for (int i=0; i<n; i++) weights[i]=obligated[i]=-inf;
	for (int i=0; i<m; i++) {
		int u, v, w; cin >> u >> v >> w; u--, v--; if (v<u) swap(u, v);
		if (u==v) {
			if (obligated[u]!=-inf && obligated[u]!=w) quit("NO");
			obligated[u]=w;
			continue;
		}
		if (ver[u].count(v)) {
			if (ver[u][v]==w*2) continue;
			else quit("NO");
		}
		ver[u][v]=w*2;
		adj[u].pb({v, w*2});
		adj[v].pb({u, w*2});
	}
	precalc();
	int empl=-1;
	for (auto src: roots) {
		empl++;
		int obl=-1;
		for (auto u: contains[empl]) if (obligated[u]!=-inf) obl=u;
		if (obl!=-1) {
			src=obl;
			calcw(src, empl, obligated[src]);
			if (toadd!=inf) quit("NO");
			continue;
		}
		calcw(src, empl, 0);
		if (toadd==inf) {
			calcw(src, empl, 1);
			if (toadd==inf) calc(src, empl);
			else calcw(src, empl, 0);
			continue;
		}
		calcw(src, empl, toadd);
		if (toadd!=inf) quit("NO");
	}
	for (int u=0; u<n; u++) {
		for (auto [v, w]: adj[u]) {
			if (weights[v]+weights[u]!=w) quit("NO");
		}
	}
	cout << "YES" << endl;
	for (int i=0; i<n; i++) cout << (double)weights[i]/2 << ' ';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB answer = YES
2 Correct 1 ms 3420 KB answer = YES
3 Correct 1 ms 3420 KB answer = YES
4 Correct 1 ms 3444 KB answer = NO
5 Correct 1 ms 3420 KB answer = YES
6 Correct 1 ms 3420 KB answer = YES
7 Correct 1 ms 3420 KB answer = YES
8 Correct 1 ms 3420 KB answer = YES
9 Correct 1 ms 3420 KB answer = NO
10 Correct 1 ms 3420 KB answer = YES
11 Correct 2 ms 3448 KB answer = YES
12 Correct 1 ms 3420 KB answer = NO
13 Correct 2 ms 3420 KB answer = YES
14 Correct 2 ms 3420 KB answer = YES
15 Correct 2 ms 3420 KB answer = YES
16 Correct 2 ms 3420 KB answer = YES
17 Correct 1 ms 3420 KB answer = YES
18 Correct 1 ms 3420 KB answer = YES
19 Correct 1 ms 3420 KB answer = YES
20 Correct 1 ms 3420 KB answer = YES
21 Correct 1 ms 3508 KB answer = YES
22 Correct 1 ms 3420 KB answer = NO
23 Correct 1 ms 3420 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB answer = YES
2 Correct 1 ms 3420 KB answer = YES
3 Correct 1 ms 3420 KB answer = YES
4 Correct 1 ms 3444 KB answer = NO
5 Correct 1 ms 3420 KB answer = YES
6 Correct 1 ms 3420 KB answer = YES
7 Correct 1 ms 3420 KB answer = YES
8 Correct 1 ms 3420 KB answer = YES
9 Correct 1 ms 3420 KB answer = NO
10 Correct 1 ms 3420 KB answer = YES
11 Correct 2 ms 3448 KB answer = YES
12 Correct 1 ms 3420 KB answer = NO
13 Correct 2 ms 3420 KB answer = YES
14 Correct 2 ms 3420 KB answer = YES
15 Correct 2 ms 3420 KB answer = YES
16 Correct 2 ms 3420 KB answer = YES
17 Correct 1 ms 3420 KB answer = YES
18 Correct 1 ms 3420 KB answer = YES
19 Correct 1 ms 3420 KB answer = YES
20 Correct 1 ms 3420 KB answer = YES
21 Correct 1 ms 3508 KB answer = YES
22 Correct 1 ms 3420 KB answer = NO
23 Correct 1 ms 3420 KB answer = NO
24 Correct 1 ms 3416 KB answer = YES
25 Correct 2 ms 3472 KB answer = YES
26 Correct 1 ms 3420 KB answer = YES
27 Correct 2 ms 3420 KB answer = YES
28 Correct 1 ms 3420 KB answer = YES
29 Correct 1 ms 3420 KB answer = YES
30 Correct 1 ms 3420 KB answer = NO
31 Correct 1 ms 3420 KB answer = YES
32 Correct 1 ms 3420 KB answer = YES
33 Correct 2 ms 3416 KB answer = YES
34 Correct 2 ms 3416 KB answer = YES
35 Correct 2 ms 3420 KB answer = YES
36 Correct 2 ms 3420 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB answer = YES
2 Correct 1 ms 3420 KB answer = YES
3 Correct 1 ms 3420 KB answer = YES
4 Correct 1 ms 3444 KB answer = NO
5 Correct 1 ms 3420 KB answer = YES
6 Correct 1 ms 3420 KB answer = YES
7 Correct 1 ms 3420 KB answer = YES
8 Correct 1 ms 3420 KB answer = YES
9 Correct 1 ms 3420 KB answer = NO
10 Correct 1 ms 3420 KB answer = YES
11 Correct 2 ms 3448 KB answer = YES
12 Correct 1 ms 3420 KB answer = NO
13 Correct 2 ms 3420 KB answer = YES
14 Correct 2 ms 3420 KB answer = YES
15 Correct 2 ms 3420 KB answer = YES
16 Correct 2 ms 3420 KB answer = YES
17 Correct 1 ms 3420 KB answer = YES
18 Correct 1 ms 3420 KB answer = YES
19 Correct 1 ms 3420 KB answer = YES
20 Correct 1 ms 3420 KB answer = YES
21 Correct 1 ms 3508 KB answer = YES
22 Correct 1 ms 3420 KB answer = NO
23 Correct 1 ms 3420 KB answer = NO
24 Correct 1 ms 3416 KB answer = YES
25 Correct 2 ms 3472 KB answer = YES
26 Correct 1 ms 3420 KB answer = YES
27 Correct 2 ms 3420 KB answer = YES
28 Correct 1 ms 3420 KB answer = YES
29 Correct 1 ms 3420 KB answer = YES
30 Correct 1 ms 3420 KB answer = NO
31 Correct 1 ms 3420 KB answer = YES
32 Correct 1 ms 3420 KB answer = YES
33 Correct 2 ms 3416 KB answer = YES
34 Correct 2 ms 3416 KB answer = YES
35 Correct 2 ms 3420 KB answer = YES
36 Correct 2 ms 3420 KB answer = YES
37 Correct 1 ms 3420 KB answer = YES
38 Correct 2 ms 3672 KB answer = YES
39 Correct 2 ms 3672 KB answer = YES
40 Correct 2 ms 3676 KB answer = YES
41 Correct 2 ms 3676 KB answer = NO
42 Correct 2 ms 3676 KB answer = YES
43 Correct 2 ms 3676 KB answer = YES
44 Correct 2 ms 3676 KB answer = YES
45 Correct 2 ms 3676 KB answer = YES
46 Correct 1 ms 3676 KB answer = YES
47 Correct 2 ms 3676 KB answer = YES
48 Correct 2 ms 3676 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB answer = YES
2 Correct 1 ms 3420 KB answer = YES
3 Correct 1 ms 3420 KB answer = YES
4 Correct 1 ms 3444 KB answer = NO
5 Correct 1 ms 3420 KB answer = YES
6 Correct 1 ms 3420 KB answer = YES
7 Correct 1 ms 3420 KB answer = YES
8 Correct 1 ms 3420 KB answer = YES
9 Correct 1 ms 3420 KB answer = NO
10 Correct 1 ms 3420 KB answer = YES
11 Correct 2 ms 3448 KB answer = YES
12 Correct 1 ms 3420 KB answer = NO
13 Correct 2 ms 3420 KB answer = YES
14 Correct 2 ms 3420 KB answer = YES
15 Correct 2 ms 3420 KB answer = YES
16 Correct 2 ms 3420 KB answer = YES
17 Correct 1 ms 3420 KB answer = YES
18 Correct 1 ms 3420 KB answer = YES
19 Correct 1 ms 3420 KB answer = YES
20 Correct 1 ms 3420 KB answer = YES
21 Correct 1 ms 3508 KB answer = YES
22 Correct 1 ms 3420 KB answer = NO
23 Correct 1 ms 3420 KB answer = NO
24 Correct 1 ms 3416 KB answer = YES
25 Correct 2 ms 3472 KB answer = YES
26 Correct 1 ms 3420 KB answer = YES
27 Correct 2 ms 3420 KB answer = YES
28 Correct 1 ms 3420 KB answer = YES
29 Correct 1 ms 3420 KB answer = YES
30 Correct 1 ms 3420 KB answer = NO
31 Correct 1 ms 3420 KB answer = YES
32 Correct 1 ms 3420 KB answer = YES
33 Correct 2 ms 3416 KB answer = YES
34 Correct 2 ms 3416 KB answer = YES
35 Correct 2 ms 3420 KB answer = YES
36 Correct 2 ms 3420 KB answer = YES
37 Correct 1 ms 3420 KB answer = YES
38 Correct 2 ms 3672 KB answer = YES
39 Correct 2 ms 3672 KB answer = YES
40 Correct 2 ms 3676 KB answer = YES
41 Correct 2 ms 3676 KB answer = NO
42 Correct 2 ms 3676 KB answer = YES
43 Correct 2 ms 3676 KB answer = YES
44 Correct 2 ms 3676 KB answer = YES
45 Correct 2 ms 3676 KB answer = YES
46 Correct 1 ms 3676 KB answer = YES
47 Correct 2 ms 3676 KB answer = YES
48 Correct 2 ms 3676 KB answer = YES
49 Correct 8 ms 5724 KB answer = YES
50 Correct 7 ms 5976 KB answer = YES
51 Correct 13 ms 5980 KB answer = YES
52 Correct 12 ms 6108 KB answer = NO
53 Correct 2 ms 3676 KB answer = YES
54 Correct 4 ms 3932 KB answer = YES
55 Correct 7 ms 4660 KB answer = YES
56 Correct 16 ms 5468 KB answer = YES
57 Correct 11 ms 5540 KB answer = YES
58 Correct 10 ms 5468 KB answer = YES
59 Correct 7 ms 5280 KB answer = YES
60 Correct 13 ms 5468 KB answer = YES
61 Correct 4 ms 4444 KB answer = YES
62 Correct 5 ms 5832 KB answer = NO
63 Correct 87 ms 28184 KB answer = YES
64 Correct 83 ms 27984 KB answer = NO
65 Correct 91 ms 27988 KB answer = YES
66 Correct 3 ms 3928 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB answer = YES
2 Correct 1 ms 3420 KB answer = YES
3 Correct 1 ms 3420 KB answer = YES
4 Correct 1 ms 3444 KB answer = NO
5 Correct 1 ms 3420 KB answer = YES
6 Correct 1 ms 3420 KB answer = YES
7 Correct 1 ms 3420 KB answer = YES
8 Correct 1 ms 3420 KB answer = YES
9 Correct 1 ms 3420 KB answer = NO
10 Correct 1 ms 3420 KB answer = YES
11 Correct 2 ms 3448 KB answer = YES
12 Correct 1 ms 3420 KB answer = NO
13 Correct 2 ms 3420 KB answer = YES
14 Correct 2 ms 3420 KB answer = YES
15 Correct 2 ms 3420 KB answer = YES
16 Correct 2 ms 3420 KB answer = YES
17 Correct 1 ms 3420 KB answer = YES
18 Correct 1 ms 3420 KB answer = YES
19 Correct 1 ms 3420 KB answer = YES
20 Correct 1 ms 3420 KB answer = YES
21 Correct 1 ms 3508 KB answer = YES
22 Correct 1 ms 3420 KB answer = NO
23 Correct 1 ms 3420 KB answer = NO
24 Correct 1 ms 3416 KB answer = YES
25 Correct 2 ms 3472 KB answer = YES
26 Correct 1 ms 3420 KB answer = YES
27 Correct 2 ms 3420 KB answer = YES
28 Correct 1 ms 3420 KB answer = YES
29 Correct 1 ms 3420 KB answer = YES
30 Correct 1 ms 3420 KB answer = NO
31 Correct 1 ms 3420 KB answer = YES
32 Correct 1 ms 3420 KB answer = YES
33 Correct 2 ms 3416 KB answer = YES
34 Correct 2 ms 3416 KB answer = YES
35 Correct 2 ms 3420 KB answer = YES
36 Correct 2 ms 3420 KB answer = YES
37 Correct 1 ms 3420 KB answer = YES
38 Correct 2 ms 3672 KB answer = YES
39 Correct 2 ms 3672 KB answer = YES
40 Correct 2 ms 3676 KB answer = YES
41 Correct 2 ms 3676 KB answer = NO
42 Correct 2 ms 3676 KB answer = YES
43 Correct 2 ms 3676 KB answer = YES
44 Correct 2 ms 3676 KB answer = YES
45 Correct 2 ms 3676 KB answer = YES
46 Correct 1 ms 3676 KB answer = YES
47 Correct 2 ms 3676 KB answer = YES
48 Correct 2 ms 3676 KB answer = YES
49 Correct 8 ms 5724 KB answer = YES
50 Correct 7 ms 5976 KB answer = YES
51 Correct 13 ms 5980 KB answer = YES
52 Correct 12 ms 6108 KB answer = NO
53 Correct 2 ms 3676 KB answer = YES
54 Correct 4 ms 3932 KB answer = YES
55 Correct 7 ms 4660 KB answer = YES
56 Correct 16 ms 5468 KB answer = YES
57 Correct 11 ms 5540 KB answer = YES
58 Correct 10 ms 5468 KB answer = YES
59 Correct 7 ms 5280 KB answer = YES
60 Correct 13 ms 5468 KB answer = YES
61 Correct 4 ms 4444 KB answer = YES
62 Correct 5 ms 5832 KB answer = NO
63 Correct 87 ms 28184 KB answer = YES
64 Correct 83 ms 27984 KB answer = NO
65 Correct 91 ms 27988 KB answer = YES
66 Correct 3 ms 3928 KB answer = YES
67 Correct 74 ms 31180 KB answer = YES
68 Correct 84 ms 31180 KB answer = YES
69 Correct 53 ms 30924 KB answer = YES
70 Correct 82 ms 49424 KB answer = YES
71 Correct 52 ms 31320 KB answer = YES
72 Correct 179 ms 23756 KB answer = YES
73 Correct 76 ms 23784 KB answer = YES
74 Correct 40 ms 20424 KB answer = YES
75 Correct 85 ms 20364 KB answer = NO
76 Correct 16 ms 5940 KB answer = YES
77 Correct 34 ms 8600 KB answer = YES
78 Correct 61 ms 12252 KB answer = YES
79 Correct 140 ms 20820 KB answer = YES
80 Correct 96 ms 20428 KB answer = YES
81 Correct 40 ms 26964 KB answer = NO
82 Correct 73 ms 30924 KB answer = YES
83 Correct 88 ms 31596 KB answer = YES
84 Correct 285 ms 31512 KB answer = YES
85 Correct 80 ms 31176 KB answer = YES
86 Correct 54 ms 31184 KB answer = YES
87 Correct 130 ms 26052 KB answer = NO
88 Correct 100 ms 27244 KB answer = YES
89 Correct 65 ms 24588 KB answer = YES
90 Correct 63 ms 24628 KB answer = YES
91 Correct 81 ms 24944 KB answer = YES
92 Correct 31 ms 13668 KB answer = YES
93 Correct 29 ms 13528 KB answer = YES
94 Correct 296 ms 31172 KB answer = NO
95 Correct 46 ms 24584 KB answer = NO
96 Correct 150 ms 41272 KB answer = YES
97 Correct 54 ms 30720 KB answer = NO