Submission #667185

# Submission time Handle Problem Language Result Execution time Memory
667185 2022-11-30T16:14:11 Z Kahou Tug of War (BOI15_tug) C++14
100 / 100
93 ms 8212 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;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3540 KB Output is correct
2 Correct 7 ms 3592 KB Output is correct
3 Correct 7 ms 3540 KB Output is correct
4 Correct 8 ms 3540 KB Output is correct
5 Correct 9 ms 3608 KB Output is correct
6 Correct 10 ms 3540 KB Output is correct
7 Correct 9 ms 3540 KB Output is correct
8 Correct 8 ms 3628 KB Output is correct
9 Correct 8 ms 3540 KB Output is correct
10 Correct 7 ms 3620 KB Output is correct
11 Correct 7 ms 3540 KB Output is correct
12 Correct 8 ms 3540 KB Output is correct
13 Correct 7 ms 3540 KB Output is correct
14 Correct 7 ms 3540 KB Output is correct
15 Correct 7 ms 3540 KB Output is correct
16 Correct 7 ms 3540 KB Output is correct
17 Correct 8 ms 3540 KB Output is correct
18 Correct 7 ms 3540 KB Output is correct
19 Correct 9 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 3376 KB Output is correct
23 Correct 7 ms 3540 KB Output is correct
24 Correct 7 ms 3620 KB Output is correct
25 Correct 7 ms 3624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3540 KB Output is correct
2 Correct 7 ms 3592 KB Output is correct
3 Correct 7 ms 3540 KB Output is correct
4 Correct 8 ms 3540 KB Output is correct
5 Correct 9 ms 3608 KB Output is correct
6 Correct 10 ms 3540 KB Output is correct
7 Correct 9 ms 3540 KB Output is correct
8 Correct 8 ms 3628 KB Output is correct
9 Correct 8 ms 3540 KB Output is correct
10 Correct 7 ms 3620 KB Output is correct
11 Correct 7 ms 3540 KB Output is correct
12 Correct 8 ms 3540 KB Output is correct
13 Correct 7 ms 3540 KB Output is correct
14 Correct 7 ms 3540 KB Output is correct
15 Correct 7 ms 3540 KB Output is correct
16 Correct 7 ms 3540 KB Output is correct
17 Correct 8 ms 3540 KB Output is correct
18 Correct 7 ms 3540 KB Output is correct
19 Correct 9 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 3376 KB Output is correct
23 Correct 7 ms 3540 KB Output is correct
24 Correct 7 ms 3620 KB Output is correct
25 Correct 7 ms 3624 KB Output is correct
26 Correct 12 ms 3936 KB Output is correct
27 Correct 19 ms 3888 KB Output is correct
28 Correct 12 ms 3924 KB Output is correct
29 Correct 19 ms 3796 KB Output is correct
30 Correct 13 ms 3936 KB Output is correct
31 Correct 19 ms 3796 KB Output is correct
32 Correct 12 ms 4052 KB Output is correct
33 Correct 19 ms 3796 KB Output is correct
34 Correct 10 ms 3796 KB Output is correct
35 Correct 20 ms 3888 KB Output is correct
36 Correct 13 ms 3924 KB Output is correct
37 Correct 18 ms 3892 KB Output is correct
38 Correct 12 ms 3924 KB Output is correct
39 Correct 19 ms 3888 KB Output is correct
40 Correct 14 ms 3924 KB Output is correct
41 Correct 19 ms 3796 KB Output is correct
42 Correct 12 ms 3924 KB Output is correct
43 Correct 19 ms 3796 KB Output is correct
44 Correct 12 ms 3920 KB Output is correct
45 Correct 23 ms 3884 KB Output is correct
46 Correct 16 ms 3924 KB Output is correct
47 Correct 3 ms 3668 KB Output is correct
48 Correct 4 ms 3668 KB Output is correct
49 Correct 4 ms 3668 KB Output is correct
50 Correct 12 ms 3924 KB Output is correct
51 Correct 10 ms 3924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 4948 KB Output is correct
2 Correct 7 ms 4340 KB Output is correct
3 Correct 9 ms 4948 KB Output is correct
4 Correct 10 ms 4372 KB Output is correct
5 Correct 9 ms 5008 KB Output is correct
6 Correct 8 ms 4452 KB Output is correct
7 Correct 15 ms 5004 KB Output is correct
8 Correct 8 ms 4308 KB Output is correct
9 Correct 9 ms 4948 KB Output is correct
10 Correct 8 ms 4436 KB Output is correct
11 Correct 10 ms 4948 KB Output is correct
12 Correct 7 ms 4308 KB Output is correct
13 Correct 11 ms 4948 KB Output is correct
14 Correct 10 ms 4924 KB Output is correct
15 Correct 8 ms 4436 KB Output is correct
16 Correct 10 ms 4888 KB Output is correct
17 Correct 8 ms 4420 KB Output is correct
18 Correct 9 ms 4948 KB Output is correct
19 Correct 7 ms 4308 KB Output is correct
20 Correct 11 ms 4948 KB Output is correct
21 Correct 13 ms 4828 KB Output is correct
22 Correct 12 ms 4820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3540 KB Output is correct
2 Correct 7 ms 3592 KB Output is correct
3 Correct 7 ms 3540 KB Output is correct
4 Correct 8 ms 3540 KB Output is correct
5 Correct 9 ms 3608 KB Output is correct
6 Correct 10 ms 3540 KB Output is correct
7 Correct 9 ms 3540 KB Output is correct
8 Correct 8 ms 3628 KB Output is correct
9 Correct 8 ms 3540 KB Output is correct
10 Correct 7 ms 3620 KB Output is correct
11 Correct 7 ms 3540 KB Output is correct
12 Correct 8 ms 3540 KB Output is correct
13 Correct 7 ms 3540 KB Output is correct
14 Correct 7 ms 3540 KB Output is correct
15 Correct 7 ms 3540 KB Output is correct
16 Correct 7 ms 3540 KB Output is correct
17 Correct 8 ms 3540 KB Output is correct
18 Correct 7 ms 3540 KB Output is correct
19 Correct 9 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 3376 KB Output is correct
23 Correct 7 ms 3540 KB Output is correct
24 Correct 7 ms 3620 KB Output is correct
25 Correct 7 ms 3624 KB Output is correct
26 Correct 12 ms 3936 KB Output is correct
27 Correct 19 ms 3888 KB Output is correct
28 Correct 12 ms 3924 KB Output is correct
29 Correct 19 ms 3796 KB Output is correct
30 Correct 13 ms 3936 KB Output is correct
31 Correct 19 ms 3796 KB Output is correct
32 Correct 12 ms 4052 KB Output is correct
33 Correct 19 ms 3796 KB Output is correct
34 Correct 10 ms 3796 KB Output is correct
35 Correct 20 ms 3888 KB Output is correct
36 Correct 13 ms 3924 KB Output is correct
37 Correct 18 ms 3892 KB Output is correct
38 Correct 12 ms 3924 KB Output is correct
39 Correct 19 ms 3888 KB Output is correct
40 Correct 14 ms 3924 KB Output is correct
41 Correct 19 ms 3796 KB Output is correct
42 Correct 12 ms 3924 KB Output is correct
43 Correct 19 ms 3796 KB Output is correct
44 Correct 12 ms 3920 KB Output is correct
45 Correct 23 ms 3884 KB Output is correct
46 Correct 16 ms 3924 KB Output is correct
47 Correct 3 ms 3668 KB Output is correct
48 Correct 4 ms 3668 KB Output is correct
49 Correct 4 ms 3668 KB Output is correct
50 Correct 12 ms 3924 KB Output is correct
51 Correct 10 ms 3924 KB Output is correct
52 Correct 10 ms 4948 KB Output is correct
53 Correct 7 ms 4340 KB Output is correct
54 Correct 9 ms 4948 KB Output is correct
55 Correct 10 ms 4372 KB Output is correct
56 Correct 9 ms 5008 KB Output is correct
57 Correct 8 ms 4452 KB Output is correct
58 Correct 15 ms 5004 KB Output is correct
59 Correct 8 ms 4308 KB Output is correct
60 Correct 9 ms 4948 KB Output is correct
61 Correct 8 ms 4436 KB Output is correct
62 Correct 10 ms 4948 KB Output is correct
63 Correct 7 ms 4308 KB Output is correct
64 Correct 11 ms 4948 KB Output is correct
65 Correct 10 ms 4924 KB Output is correct
66 Correct 8 ms 4436 KB Output is correct
67 Correct 10 ms 4888 KB Output is correct
68 Correct 8 ms 4420 KB Output is correct
69 Correct 9 ms 4948 KB Output is correct
70 Correct 7 ms 4308 KB Output is correct
71 Correct 11 ms 4948 KB Output is correct
72 Correct 13 ms 4828 KB Output is correct
73 Correct 12 ms 4820 KB Output is correct
74 Correct 52 ms 8204 KB Output is correct
75 Correct 83 ms 7252 KB Output is correct
76 Correct 39 ms 8204 KB Output is correct
77 Correct 75 ms 7252 KB Output is correct
78 Correct 51 ms 8176 KB Output is correct
79 Correct 47 ms 8140 KB Output is correct
80 Correct 93 ms 7232 KB Output is correct
81 Correct 38 ms 7112 KB Output is correct
82 Correct 30 ms 8212 KB Output is correct
83 Correct 39 ms 8148 KB Output is correct
84 Correct 74 ms 7216 KB Output is correct
85 Correct 54 ms 8212 KB Output is correct
86 Correct 87 ms 7248 KB Output is correct
87 Correct 45 ms 8200 KB Output is correct
88 Correct 78 ms 7372 KB Output is correct
89 Correct 43 ms 8208 KB Output is correct
90 Correct 73 ms 7252 KB Output is correct
91 Correct 43 ms 8204 KB Output is correct
92 Correct 93 ms 7256 KB Output is correct
93 Correct 40 ms 8140 KB Output is correct
94 Correct 29 ms 7656 KB Output is correct
95 Correct 29 ms 7616 KB Output is correct
96 Correct 33 ms 7544 KB Output is correct
97 Correct 39 ms 8180 KB Output is correct
98 Correct 42 ms 8136 KB Output is correct