Submission #47921

# Submission time Handle Problem Language Result Execution time Memory
47921 2018-05-09T01:34:26 Z cheater2k Tug of War (BOI15_tug) C++17
100 / 100
2279 ms 27516 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 60005;
const int MAX = 600005;

int n, diff, a[N], s[N];
int par[N], nver[N], nedge[N];
vector < pair<int,int> > g[N];
int deg[N];
int sum;
bool del[N], vedge[N];
vector <int> cycle, weight;

int anc(int p) { return p == par[p] ? p : par[p] = anc(par[p]); }

void add(int u, int v, int i) {
	g[u].push_back({v, i}); ++deg[u];
	g[v].push_back({u, i}); ++deg[v];
}

void find_cycle(int u, int r) {
	del[u] = true;
	cycle.push_back(u);
	for (auto &e : g[u]) if (!del[e.first]) {
		weight.push_back(s[e.second]);
		vedge[e.second] = true;
		return find_cycle(e.first, r);
	}
	// cannot visit any vertices
	for (auto &e : g[u]) if (e.first == r && !vedge[e.second]) {
		weight.push_back(s[e.second]);
	}
}

void dfs_tree(int u, int p) {
	if (u <= n) sum += a[u];
	for (auto &e : g[u]) {
		int v = e.first; if (v == p || deg[v] == 2) continue;
		dfs_tree(v, u);
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	cin >> n >> diff;

	for (int i = 1; i <= n + n; ++i) { // reset Disjoint Set
		par[i] = i;
		nver[i] = 1;
		nedge[i] = 0;
	}

	for (int i = 1; i <= n + n; ++i) {
		int u, v; cin >> u >> v >> s[i];
		v += n;
		int pu = anc(u), pv = anc(v);
		if (pu == pv) {
			if (nver[pu] > nedge[pu]) ++nedge[pu], add(u, v, i);
			else { // invalid
				return printf("NO\n"), 0;
			}
		} else { // join u and v
			nver[pu] += nver[pv]; nver[pv] = 0;
			nedge[pu] += nedge[pv] + 1; nedge[pv] = 0;
			par[pv] = pu;
			add(u, v, i);
			if (nver[pu] < nedge[pu]) {
				return printf("NO\n"), 0;
			}
		}
	}

	queue <int> q;
	for (int i = 1; i <= n + n; ++i) if (deg[i] == 1) q.push(i);

	while(!q.empty()) {
		int u = q.front(); q.pop();
		del[u] = true;
		for (auto &e : g[u]) {
			int v = e.first, w = s[e.second]; 
			if (del[v]) continue;
			a[u] = w;
			if (--deg[v] == 1) q.push(v);
		}
	}

	// DP
	bitset <MAX> bs;
	bs.set(0);

	for (int i = 1; i <= n + n; ++i) if (!del[i]) { // in cycle
		cycle.clear();
		weight.clear();
		find_cycle(i, i);
		sum = 0;
		for (int j : cycle) {
			dfs_tree(j, j);
		}

		int sz = cycle.size();
		int sum1 = 0, sum2 = 0;

		for (int i = 0; i < sz; ++i) {
			int w = weight[i];
			if (cycle[i] <= n) sum1 += w;
			if (cycle[(i + 1) % sz] <= n) sum2 += w;
		}

		// group 1
		int c = sum + sum1; // first choice
		int d = sum + sum2; // second choice
		bs = (bs << c) | (bs << d);
	}

	// check
	sum = 0;
	for (int i = 1; i <= n + n; ++i) sum += s[i];

	for (int i = 1; i <= sum; ++i) if (bs[i]) {
		int j = sum - i; // group 2
		if (abs(i - j) <= diff) {
			return printf("YES\n"), 0;		
		}
	}

	printf("NO\n");
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2168 KB Output is correct
2 Correct 4 ms 2168 KB Output is correct
3 Correct 4 ms 2220 KB Output is correct
4 Correct 4 ms 2220 KB Output is correct
5 Correct 4 ms 2224 KB Output is correct
6 Correct 4 ms 2260 KB Output is correct
7 Correct 4 ms 2336 KB Output is correct
8 Correct 4 ms 2412 KB Output is correct
9 Correct 4 ms 2416 KB Output is correct
10 Correct 4 ms 2428 KB Output is correct
11 Correct 4 ms 2428 KB Output is correct
12 Correct 4 ms 2428 KB Output is correct
13 Correct 4 ms 2464 KB Output is correct
14 Correct 4 ms 2504 KB Output is correct
15 Correct 4 ms 2612 KB Output is correct
16 Correct 4 ms 2612 KB Output is correct
17 Correct 4 ms 2612 KB Output is correct
18 Correct 4 ms 2612 KB Output is correct
19 Correct 4 ms 2612 KB Output is correct
20 Correct 4 ms 2612 KB Output is correct
21 Correct 3 ms 2612 KB Output is correct
22 Correct 3 ms 2612 KB Output is correct
23 Correct 4 ms 2612 KB Output is correct
24 Correct 4 ms 2612 KB Output is correct
25 Correct 4 ms 2612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2168 KB Output is correct
2 Correct 4 ms 2168 KB Output is correct
3 Correct 4 ms 2220 KB Output is correct
4 Correct 4 ms 2220 KB Output is correct
5 Correct 4 ms 2224 KB Output is correct
6 Correct 4 ms 2260 KB Output is correct
7 Correct 4 ms 2336 KB Output is correct
8 Correct 4 ms 2412 KB Output is correct
9 Correct 4 ms 2416 KB Output is correct
10 Correct 4 ms 2428 KB Output is correct
11 Correct 4 ms 2428 KB Output is correct
12 Correct 4 ms 2428 KB Output is correct
13 Correct 4 ms 2464 KB Output is correct
14 Correct 4 ms 2504 KB Output is correct
15 Correct 4 ms 2612 KB Output is correct
16 Correct 4 ms 2612 KB Output is correct
17 Correct 4 ms 2612 KB Output is correct
18 Correct 4 ms 2612 KB Output is correct
19 Correct 4 ms 2612 KB Output is correct
20 Correct 4 ms 2612 KB Output is correct
21 Correct 3 ms 2612 KB Output is correct
22 Correct 3 ms 2612 KB Output is correct
23 Correct 4 ms 2612 KB Output is correct
24 Correct 4 ms 2612 KB Output is correct
25 Correct 4 ms 2612 KB Output is correct
26 Correct 156 ms 2964 KB Output is correct
27 Correct 25 ms 2992 KB Output is correct
28 Correct 153 ms 3056 KB Output is correct
29 Correct 26 ms 3104 KB Output is correct
30 Correct 161 ms 3172 KB Output is correct
31 Correct 27 ms 3216 KB Output is correct
32 Correct 155 ms 3280 KB Output is correct
33 Correct 26 ms 3332 KB Output is correct
34 Correct 16 ms 3380 KB Output is correct
35 Correct 25 ms 3424 KB Output is correct
36 Correct 152 ms 3512 KB Output is correct
37 Correct 25 ms 3512 KB Output is correct
38 Correct 155 ms 3712 KB Output is correct
39 Correct 26 ms 3712 KB Output is correct
40 Correct 165 ms 3712 KB Output is correct
41 Correct 26 ms 3728 KB Output is correct
42 Correct 152 ms 3800 KB Output is correct
43 Correct 25 ms 3824 KB Output is correct
44 Correct 153 ms 3984 KB Output is correct
45 Correct 26 ms 3984 KB Output is correct
46 Correct 152 ms 3984 KB Output is correct
47 Correct 4 ms 3984 KB Output is correct
48 Correct 80 ms 4032 KB Output is correct
49 Correct 82 ms 4240 KB Output is correct
50 Correct 153 ms 4288 KB Output is correct
51 Correct 154 ms 4288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 776 ms 5068 KB Output is correct
2 Correct 6 ms 5068 KB Output is correct
3 Correct 752 ms 5068 KB Output is correct
4 Correct 7 ms 5068 KB Output is correct
5 Correct 770 ms 5068 KB Output is correct
6 Correct 6 ms 5068 KB Output is correct
7 Correct 743 ms 5152 KB Output is correct
8 Correct 8 ms 5152 KB Output is correct
9 Correct 730 ms 5152 KB Output is correct
10 Correct 7 ms 5152 KB Output is correct
11 Correct 756 ms 5200 KB Output is correct
12 Correct 6 ms 5200 KB Output is correct
13 Correct 731 ms 5200 KB Output is correct
14 Correct 739 ms 5200 KB Output is correct
15 Correct 7 ms 5200 KB Output is correct
16 Correct 756 ms 5200 KB Output is correct
17 Correct 7 ms 5200 KB Output is correct
18 Correct 757 ms 5200 KB Output is correct
19 Correct 5 ms 5200 KB Output is correct
20 Correct 729 ms 5200 KB Output is correct
21 Correct 7 ms 5200 KB Output is correct
22 Correct 386 ms 5516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2168 KB Output is correct
2 Correct 4 ms 2168 KB Output is correct
3 Correct 4 ms 2220 KB Output is correct
4 Correct 4 ms 2220 KB Output is correct
5 Correct 4 ms 2224 KB Output is correct
6 Correct 4 ms 2260 KB Output is correct
7 Correct 4 ms 2336 KB Output is correct
8 Correct 4 ms 2412 KB Output is correct
9 Correct 4 ms 2416 KB Output is correct
10 Correct 4 ms 2428 KB Output is correct
11 Correct 4 ms 2428 KB Output is correct
12 Correct 4 ms 2428 KB Output is correct
13 Correct 4 ms 2464 KB Output is correct
14 Correct 4 ms 2504 KB Output is correct
15 Correct 4 ms 2612 KB Output is correct
16 Correct 4 ms 2612 KB Output is correct
17 Correct 4 ms 2612 KB Output is correct
18 Correct 4 ms 2612 KB Output is correct
19 Correct 4 ms 2612 KB Output is correct
20 Correct 4 ms 2612 KB Output is correct
21 Correct 3 ms 2612 KB Output is correct
22 Correct 3 ms 2612 KB Output is correct
23 Correct 4 ms 2612 KB Output is correct
24 Correct 4 ms 2612 KB Output is correct
25 Correct 4 ms 2612 KB Output is correct
26 Correct 156 ms 2964 KB Output is correct
27 Correct 25 ms 2992 KB Output is correct
28 Correct 153 ms 3056 KB Output is correct
29 Correct 26 ms 3104 KB Output is correct
30 Correct 161 ms 3172 KB Output is correct
31 Correct 27 ms 3216 KB Output is correct
32 Correct 155 ms 3280 KB Output is correct
33 Correct 26 ms 3332 KB Output is correct
34 Correct 16 ms 3380 KB Output is correct
35 Correct 25 ms 3424 KB Output is correct
36 Correct 152 ms 3512 KB Output is correct
37 Correct 25 ms 3512 KB Output is correct
38 Correct 155 ms 3712 KB Output is correct
39 Correct 26 ms 3712 KB Output is correct
40 Correct 165 ms 3712 KB Output is correct
41 Correct 26 ms 3728 KB Output is correct
42 Correct 152 ms 3800 KB Output is correct
43 Correct 25 ms 3824 KB Output is correct
44 Correct 153 ms 3984 KB Output is correct
45 Correct 26 ms 3984 KB Output is correct
46 Correct 152 ms 3984 KB Output is correct
47 Correct 4 ms 3984 KB Output is correct
48 Correct 80 ms 4032 KB Output is correct
49 Correct 82 ms 4240 KB Output is correct
50 Correct 153 ms 4288 KB Output is correct
51 Correct 154 ms 4288 KB Output is correct
52 Correct 776 ms 5068 KB Output is correct
53 Correct 6 ms 5068 KB Output is correct
54 Correct 752 ms 5068 KB Output is correct
55 Correct 7 ms 5068 KB Output is correct
56 Correct 770 ms 5068 KB Output is correct
57 Correct 6 ms 5068 KB Output is correct
58 Correct 743 ms 5152 KB Output is correct
59 Correct 8 ms 5152 KB Output is correct
60 Correct 730 ms 5152 KB Output is correct
61 Correct 7 ms 5152 KB Output is correct
62 Correct 756 ms 5200 KB Output is correct
63 Correct 6 ms 5200 KB Output is correct
64 Correct 731 ms 5200 KB Output is correct
65 Correct 739 ms 5200 KB Output is correct
66 Correct 7 ms 5200 KB Output is correct
67 Correct 756 ms 5200 KB Output is correct
68 Correct 7 ms 5200 KB Output is correct
69 Correct 757 ms 5200 KB Output is correct
70 Correct 5 ms 5200 KB Output is correct
71 Correct 729 ms 5200 KB Output is correct
72 Correct 7 ms 5200 KB Output is correct
73 Correct 386 ms 5516 KB Output is correct
74 Correct 2279 ms 8208 KB Output is correct
75 Correct 131 ms 8780 KB Output is correct
76 Correct 2227 ms 9712 KB Output is correct
77 Correct 132 ms 10400 KB Output is correct
78 Correct 2200 ms 11340 KB Output is correct
79 Correct 2223 ms 12144 KB Output is correct
80 Correct 122 ms 12836 KB Output is correct
81 Correct 124 ms 13636 KB Output is correct
82 Correct 2214 ms 14800 KB Output is correct
83 Correct 2216 ms 15512 KB Output is correct
84 Correct 121 ms 16072 KB Output is correct
85 Correct 2202 ms 17000 KB Output is correct
86 Correct 156 ms 17704 KB Output is correct
87 Correct 2275 ms 18888 KB Output is correct
88 Correct 130 ms 19312 KB Output is correct
89 Correct 2231 ms 20388 KB Output is correct
90 Correct 132 ms 20944 KB Output is correct
91 Correct 2232 ms 21992 KB Output is correct
92 Correct 131 ms 22580 KB Output is correct
93 Correct 2242 ms 23644 KB Output is correct
94 Correct 20 ms 23644 KB Output is correct
95 Correct 1135 ms 25476 KB Output is correct
96 Correct 1180 ms 26288 KB Output is correct
97 Correct 2243 ms 26564 KB Output is correct
98 Correct 2253 ms 27516 KB Output is correct