답안 #667184

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
667184 2022-11-30T16:13:55 Z Kahou Tug of War (BOI15_tug) C++14
100 / 100
101 ms 9028 KB
// radal lanat bet.

#include<bits/stdc++.h>
using namespace std;
#define F first
#define s second
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;

const int N = 6e4 + 5;
int n, k, s[N], bk[N], to[N], from[N], t, dp[N], w[N][2], cnt[20*N], X;
int vis[N], mark[N];
int CC[N];
int sum;
vector<int> adj[N];
vector<int> vc[N];
vector<int> pth;

bitset<20*N> bs;

void dfs(int u, int p) {
	vis[u] = 1;
	CC[u] = t;
	for (int id:adj[u]) {
		if (id == p) continue;
		int v = to[id]^from[id]^u;
		if (!vis[v]) {
			pth.push_back(id);
			dfs(v, id);
			pth.pop_back();
		}
		else if (vis[v] == 1) {
			bk[t]++;
			if (!vc[t].empty()) continue;
			int pt = pth.size()-1;
			mark[from[id]] = mark[to[id]] = 1;
			vc[t].push_back(id);
			while (pt > 0 && from[pth[pt]] != v && to[pth[pt]] != v) {
				mark[from[pth[pt]]] = mark[to[pth[pt]]] = 1;
				vc[t].push_back(pth[pt--]);
			}
			mark[from[pth[pt]]] = mark[to[pth[pt]]] = 1;
			vc[t].push_back(pth[pt]);
		}
	}
	vis[u] = 2;
}

void dfs2(int u, int p) {
	for (int id:adj[u]) {
		int v = from[id]^to[id]^u;
		if (v == p || mark[v]) continue;
		dfs2(v, u);
		dp[u] += dp[v];
		if (u <= n) dp[u] += s[id];
	}
}

void solve() {
	cin >> n >> k;
	for (int i = 1; i <= 2*n; i++) {
		int l, r;
		cin >> l >> r >> s[i];
		r += n;
		adj[l].push_back(i);
		adj[r].push_back(i);
		from[i] = l;
		to[i] = r;
		sum += s[i];
	}
	
	for (int u = 1; u <= 2*n; u++) {
		if (!vis[u]) {
			t++;
			dfs(u, 0);
			if (bk[t] > 1) {
				cout << "NO" << endl;
				return;
			}
		}
	}
	for (int u = 1; u <= 2*n; u++) {
		if (mark[u]) {
			dfs2(u, 0);
			w[CC[u]][0] += dp[u];
			w[CC[u]][1] += dp[u];
		}
	}
	for (int x = 1; x <= t; x++) {
		for (int i = 0; i < (int)vc[x].size(); i++) {
			int id = vc[x][i];
			w[x][i&1] += s[id];
		}
	}

	for (int x = 1; x <= t; x++) {
		if (w[x][0] > w[x][1]) swap(w[x][0], w[x][1]);
		X += w[x][0];
		cnt[w[x][1]-w[x][0]]++;
	}

	if (X > (sum+k)/2) {
		cout << "NO" << endl;
		return;
	}
	if (X >= (sum-k+1)/2) {
		cout << "YES" << endl;
		return;
	}
	
	for (int x = 1; x < 20*N; x++) {
		while (cnt[x] >= 3) {
			cnt[x] -= 2;
			cnt[x<<1]++;
		}
	}
	bs[0] = 1;
	for (int x = 1; x < 20*N; x++) {
		for (int i = 1; i <= cnt[x]; i++) {
			bs = (bs<<x*i)|bs;
		}
	}

	int iy = bs._Find_next((sum-k+1)/2-X-1);
	cout << (iy <= (sum+k)/2-X ? "YES":"NO") << endl;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3540 KB Output is correct
2 Correct 7 ms 3624 KB Output is correct
3 Correct 7 ms 3572 KB Output is correct
4 Correct 8 ms 3700 KB Output is correct
5 Correct 8 ms 3524 KB Output is correct
6 Correct 7 ms 3540 KB Output is correct
7 Correct 8 ms 3624 KB Output is correct
8 Correct 7 ms 3540 KB Output is correct
9 Correct 8 ms 3540 KB Output is correct
10 Correct 8 ms 3576 KB Output is correct
11 Correct 7 ms 3616 KB Output is correct
12 Correct 7 ms 3624 KB Output is correct
13 Correct 7 ms 3620 KB Output is correct
14 Correct 7 ms 3568 KB Output is correct
15 Correct 7 ms 3628 KB Output is correct
16 Correct 7 ms 3632 KB Output is correct
17 Correct 7 ms 3540 KB Output is correct
18 Correct 7 ms 3540 KB Output is correct
19 Correct 10 ms 3540 KB Output is correct
20 Correct 2 ms 3412 KB Output is correct
21 Correct 2 ms 3412 KB Output is correct
22 Correct 3 ms 3412 KB Output is correct
23 Correct 8 ms 3540 KB Output is correct
24 Correct 7 ms 3620 KB Output is correct
25 Correct 8 ms 3624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3540 KB Output is correct
2 Correct 7 ms 3624 KB Output is correct
3 Correct 7 ms 3572 KB Output is correct
4 Correct 8 ms 3700 KB Output is correct
5 Correct 8 ms 3524 KB Output is correct
6 Correct 7 ms 3540 KB Output is correct
7 Correct 8 ms 3624 KB Output is correct
8 Correct 7 ms 3540 KB Output is correct
9 Correct 8 ms 3540 KB Output is correct
10 Correct 8 ms 3576 KB Output is correct
11 Correct 7 ms 3616 KB Output is correct
12 Correct 7 ms 3624 KB Output is correct
13 Correct 7 ms 3620 KB Output is correct
14 Correct 7 ms 3568 KB Output is correct
15 Correct 7 ms 3628 KB Output is correct
16 Correct 7 ms 3632 KB Output is correct
17 Correct 7 ms 3540 KB Output is correct
18 Correct 7 ms 3540 KB Output is correct
19 Correct 10 ms 3540 KB Output is correct
20 Correct 2 ms 3412 KB Output is correct
21 Correct 2 ms 3412 KB Output is correct
22 Correct 3 ms 3412 KB Output is correct
23 Correct 8 ms 3540 KB Output is correct
24 Correct 7 ms 3620 KB Output is correct
25 Correct 8 ms 3624 KB Output is correct
26 Correct 12 ms 3924 KB Output is correct
27 Correct 20 ms 3924 KB Output is correct
28 Correct 13 ms 4052 KB Output is correct
29 Correct 19 ms 3924 KB Output is correct
30 Correct 13 ms 3924 KB Output is correct
31 Correct 20 ms 3844 KB Output is correct
32 Correct 12 ms 3924 KB Output is correct
33 Correct 19 ms 3924 KB Output is correct
34 Correct 10 ms 3924 KB Output is correct
35 Correct 24 ms 3924 KB Output is correct
36 Correct 13 ms 3984 KB Output is correct
37 Correct 19 ms 3924 KB Output is correct
38 Correct 13 ms 3972 KB Output is correct
39 Correct 20 ms 3820 KB Output is correct
40 Correct 13 ms 3992 KB Output is correct
41 Correct 22 ms 3840 KB Output is correct
42 Correct 13 ms 3924 KB Output is correct
43 Correct 20 ms 3924 KB Output is correct
44 Correct 13 ms 3924 KB Output is correct
45 Correct 20 ms 3924 KB Output is correct
46 Correct 13 ms 3964 KB Output is correct
47 Correct 3 ms 3684 KB Output is correct
48 Correct 4 ms 3712 KB Output is correct
49 Correct 4 ms 3796 KB Output is correct
50 Correct 11 ms 3996 KB Output is correct
51 Correct 10 ms 3940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 5140 KB Output is correct
2 Correct 11 ms 4592 KB Output is correct
3 Correct 11 ms 5120 KB Output is correct
4 Correct 9 ms 4608 KB Output is correct
5 Correct 9 ms 5204 KB Output is correct
6 Correct 8 ms 4596 KB Output is correct
7 Correct 12 ms 5204 KB Output is correct
8 Correct 8 ms 4564 KB Output is correct
9 Correct 11 ms 5120 KB Output is correct
10 Correct 8 ms 4692 KB Output is correct
11 Correct 10 ms 5124 KB Output is correct
12 Correct 8 ms 4564 KB Output is correct
13 Correct 10 ms 5232 KB Output is correct
14 Correct 10 ms 5124 KB Output is correct
15 Correct 10 ms 4608 KB Output is correct
16 Correct 10 ms 5204 KB Output is correct
17 Correct 11 ms 4576 KB Output is correct
18 Correct 11 ms 5204 KB Output is correct
19 Correct 8 ms 4564 KB Output is correct
20 Correct 9 ms 5160 KB Output is correct
21 Correct 9 ms 5124 KB Output is correct
22 Correct 12 ms 4988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3540 KB Output is correct
2 Correct 7 ms 3624 KB Output is correct
3 Correct 7 ms 3572 KB Output is correct
4 Correct 8 ms 3700 KB Output is correct
5 Correct 8 ms 3524 KB Output is correct
6 Correct 7 ms 3540 KB Output is correct
7 Correct 8 ms 3624 KB Output is correct
8 Correct 7 ms 3540 KB Output is correct
9 Correct 8 ms 3540 KB Output is correct
10 Correct 8 ms 3576 KB Output is correct
11 Correct 7 ms 3616 KB Output is correct
12 Correct 7 ms 3624 KB Output is correct
13 Correct 7 ms 3620 KB Output is correct
14 Correct 7 ms 3568 KB Output is correct
15 Correct 7 ms 3628 KB Output is correct
16 Correct 7 ms 3632 KB Output is correct
17 Correct 7 ms 3540 KB Output is correct
18 Correct 7 ms 3540 KB Output is correct
19 Correct 10 ms 3540 KB Output is correct
20 Correct 2 ms 3412 KB Output is correct
21 Correct 2 ms 3412 KB Output is correct
22 Correct 3 ms 3412 KB Output is correct
23 Correct 8 ms 3540 KB Output is correct
24 Correct 7 ms 3620 KB Output is correct
25 Correct 8 ms 3624 KB Output is correct
26 Correct 12 ms 3924 KB Output is correct
27 Correct 20 ms 3924 KB Output is correct
28 Correct 13 ms 4052 KB Output is correct
29 Correct 19 ms 3924 KB Output is correct
30 Correct 13 ms 3924 KB Output is correct
31 Correct 20 ms 3844 KB Output is correct
32 Correct 12 ms 3924 KB Output is correct
33 Correct 19 ms 3924 KB Output is correct
34 Correct 10 ms 3924 KB Output is correct
35 Correct 24 ms 3924 KB Output is correct
36 Correct 13 ms 3984 KB Output is correct
37 Correct 19 ms 3924 KB Output is correct
38 Correct 13 ms 3972 KB Output is correct
39 Correct 20 ms 3820 KB Output is correct
40 Correct 13 ms 3992 KB Output is correct
41 Correct 22 ms 3840 KB Output is correct
42 Correct 13 ms 3924 KB Output is correct
43 Correct 20 ms 3924 KB Output is correct
44 Correct 13 ms 3924 KB Output is correct
45 Correct 20 ms 3924 KB Output is correct
46 Correct 13 ms 3964 KB Output is correct
47 Correct 3 ms 3684 KB Output is correct
48 Correct 4 ms 3712 KB Output is correct
49 Correct 4 ms 3796 KB Output is correct
50 Correct 11 ms 3996 KB Output is correct
51 Correct 10 ms 3940 KB Output is correct
52 Correct 10 ms 5140 KB Output is correct
53 Correct 11 ms 4592 KB Output is correct
54 Correct 11 ms 5120 KB Output is correct
55 Correct 9 ms 4608 KB Output is correct
56 Correct 9 ms 5204 KB Output is correct
57 Correct 8 ms 4596 KB Output is correct
58 Correct 12 ms 5204 KB Output is correct
59 Correct 8 ms 4564 KB Output is correct
60 Correct 11 ms 5120 KB Output is correct
61 Correct 8 ms 4692 KB Output is correct
62 Correct 10 ms 5124 KB Output is correct
63 Correct 8 ms 4564 KB Output is correct
64 Correct 10 ms 5232 KB Output is correct
65 Correct 10 ms 5124 KB Output is correct
66 Correct 10 ms 4608 KB Output is correct
67 Correct 10 ms 5204 KB Output is correct
68 Correct 11 ms 4576 KB Output is correct
69 Correct 11 ms 5204 KB Output is correct
70 Correct 8 ms 4564 KB Output is correct
71 Correct 9 ms 5160 KB Output is correct
72 Correct 9 ms 5124 KB Output is correct
73 Correct 12 ms 4988 KB Output is correct
74 Correct 38 ms 8956 KB Output is correct
75 Correct 76 ms 8068 KB Output is correct
76 Correct 40 ms 8908 KB Output is correct
77 Correct 75 ms 8072 KB Output is correct
78 Correct 40 ms 8908 KB Output is correct
79 Correct 61 ms 9004 KB Output is correct
80 Correct 89 ms 8072 KB Output is correct
81 Correct 40 ms 8000 KB Output is correct
82 Correct 32 ms 9028 KB Output is correct
83 Correct 42 ms 8908 KB Output is correct
84 Correct 78 ms 8068 KB Output is correct
85 Correct 49 ms 8988 KB Output is correct
86 Correct 101 ms 8056 KB Output is correct
87 Correct 41 ms 9024 KB Output is correct
88 Correct 76 ms 8004 KB Output is correct
89 Correct 41 ms 8944 KB Output is correct
90 Correct 80 ms 8068 KB Output is correct
91 Correct 42 ms 9016 KB Output is correct
92 Correct 80 ms 7956 KB Output is correct
93 Correct 42 ms 8984 KB Output is correct
94 Correct 30 ms 8436 KB Output is correct
95 Correct 35 ms 8376 KB Output is correct
96 Correct 34 ms 8392 KB Output is correct
97 Correct 44 ms 8928 KB Output is correct
98 Correct 37 ms 8956 KB Output is correct