Submission #726450

# Submission time Handle Problem Language Result Execution time Memory
726450 2023-04-18T23:51:12 Z dozer Tug of War (BOI15_tug) C++14
100 / 100
913 ms 10304 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,inline") 
#pragma GCC optimize ("unroll-loops")
 
#pragma GCC target("bmi,bmi2,lzcnt,popcnt")                      
#pragma GCC target("movbe")                                      
#pragma GCC target("aes,pclmul,rdrnd")                           
#pragma GCC target("avx,avx2,f16c,fma,sse3,ssse3,sse4.1,sse4.2")

#define sp " "
#define endl "\n";
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 60005

const int modulo = 1e9 + 7;


set<int> adj[N];
int l[N], r[N], w[N], done[N];

int32_t main()
{
	fastio();

	int n, k;
	cin>>n>>k;
	for (int i = 1; i <= 2 * n; i++)
	{
		cin>>l[i]>>r[i]>>w[i];
		adj[l[i]].insert(i);
		adj[r[i] + n].insert(i);
	}

	int sum = 0;
	queue<int> q;
	for (int i = 1; i <= 2 * n; i++)
		if (adj[i].size() == 1) q.push(i);

	int flag = 0;
	while(!q.empty())
	{
		int top = q.front();
		q.pop();
		done[top] = 1;
		if (adj[top].empty())
		{
			flag = 1;
			break;
		}

		if (top <= n) sum += w[*adj[top].begin()];
		else sum -= w[*adj[top].begin()];

		for (auto i : adj[top])
		{
			int to = l[i];
			if (to == top) to = r[i] + n;
			adj[to].erase(i);
			if (adj[to].size() == 1) q.push(to);
		}
	}	

	if (flag)
	{
		cout<<"NO\n";
		return 0;
	}

	int all = abs(sum);
	vector<int> v;
	int maks = 0;
	for (int i = 1; i <= 2 * n; i++)
	{
		if (done[i]) continue;
		if (adj[i].empty())
		{
			flag = 1;
			break;
		}
		int curr = i, steps = 0, tot = 0;
		while(done[curr] == 0 && !adj[curr].empty())
		{
			done[curr] = 1;
			if (adj[curr].size() > 2)
			{
				flag = 1;
				break;
			}
			int ind = *adj[curr].begin();
			if (steps % 2) tot -= w[ind];
			else tot += w[ind];
			steps++;
			int to = l[ind];
			if (to == curr) to = r[ind] + n;
			adj[to].erase(ind);
			adj[curr].erase(ind);
			curr = to;
		}
		v.pb(abs(tot));	
		all += abs(tot);
		maks = max(maks, abs(tot));
	}
	if (flag)
	{
		cout<<"NO\n";
		return 0;
	}
	
	sum = abs(sum);
	v.pb(sum);

	bitset<600005> a, b;
	a.reset(), b.reset();
	sort(v.rbegin(), v.rend());

	a[0] = 1;
	for (int i = 0; i < (int)v.size(); i++)
	{
		b |= (a<<v[i]);
		b |= (a>>v[i]);
		for (int j = 0; j <= v[i]; j++)
			b[v[i] - j] = b[v[i] - j] | a[j];
		a = b;
		b.reset();
	}
	
	for (int i = 0; i <= k; i++)
		if (a[i]) flag = 1;


	if (flag) cout<<"YES\n";
	else cout<<"NO\n";

	cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " seconds\n";
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 3 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 3 ms 3412 KB Output is correct
8 Correct 3 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3412 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3412 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3412 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 2 ms 3412 KB Output is correct
21 Correct 2 ms 3412 KB Output is correct
22 Correct 2 ms 3412 KB Output is correct
23 Correct 2 ms 3412 KB Output is correct
24 Correct 2 ms 3412 KB Output is correct
25 Correct 2 ms 3412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 3 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 3 ms 3412 KB Output is correct
8 Correct 3 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3412 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3412 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3412 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 2 ms 3412 KB Output is correct
21 Correct 2 ms 3412 KB Output is correct
22 Correct 2 ms 3412 KB Output is correct
23 Correct 2 ms 3412 KB Output is correct
24 Correct 2 ms 3412 KB Output is correct
25 Correct 2 ms 3412 KB Output is correct
26 Correct 58 ms 3884 KB Output is correct
27 Correct 11 ms 3868 KB Output is correct
28 Correct 64 ms 3892 KB Output is correct
29 Correct 15 ms 3888 KB Output is correct
30 Correct 65 ms 3888 KB Output is correct
31 Correct 14 ms 3884 KB Output is correct
32 Correct 58 ms 3892 KB Output is correct
33 Correct 17 ms 3796 KB Output is correct
34 Correct 8 ms 3884 KB Output is correct
35 Correct 12 ms 3892 KB Output is correct
36 Correct 61 ms 3888 KB Output is correct
37 Correct 12 ms 3896 KB Output is correct
38 Correct 64 ms 3904 KB Output is correct
39 Correct 11 ms 3888 KB Output is correct
40 Correct 62 ms 3896 KB Output is correct
41 Correct 12 ms 3892 KB Output is correct
42 Correct 59 ms 3888 KB Output is correct
43 Correct 11 ms 3796 KB Output is correct
44 Correct 61 ms 3888 KB Output is correct
45 Correct 15 ms 3796 KB Output is correct
46 Correct 61 ms 3796 KB Output is correct
47 Correct 4 ms 3796 KB Output is correct
48 Correct 31 ms 3796 KB Output is correct
49 Correct 32 ms 3888 KB Output is correct
50 Correct 66 ms 3888 KB Output is correct
51 Correct 59 ms 3884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 197 ms 5636 KB Output is correct
2 Correct 10 ms 5588 KB Output is correct
3 Correct 186 ms 5732 KB Output is correct
4 Correct 12 ms 5588 KB Output is correct
5 Correct 209 ms 5732 KB Output is correct
6 Correct 10 ms 5508 KB Output is correct
7 Correct 173 ms 5732 KB Output is correct
8 Correct 10 ms 5588 KB Output is correct
9 Correct 188 ms 5728 KB Output is correct
10 Correct 10 ms 5588 KB Output is correct
11 Correct 175 ms 5716 KB Output is correct
12 Correct 10 ms 5588 KB Output is correct
13 Correct 181 ms 5724 KB Output is correct
14 Correct 186 ms 5736 KB Output is correct
15 Correct 10 ms 5588 KB Output is correct
16 Correct 179 ms 5728 KB Output is correct
17 Correct 10 ms 5624 KB Output is correct
18 Correct 217 ms 5736 KB Output is correct
19 Correct 10 ms 5588 KB Output is correct
20 Correct 178 ms 5736 KB Output is correct
21 Correct 10 ms 5624 KB Output is correct
22 Correct 98 ms 5708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 3 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 2 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 3 ms 3412 KB Output is correct
8 Correct 3 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3412 KB Output is correct
13 Correct 2 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3412 KB Output is correct
16 Correct 2 ms 3412 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 2 ms 3412 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 2 ms 3412 KB Output is correct
21 Correct 2 ms 3412 KB Output is correct
22 Correct 2 ms 3412 KB Output is correct
23 Correct 2 ms 3412 KB Output is correct
24 Correct 2 ms 3412 KB Output is correct
25 Correct 2 ms 3412 KB Output is correct
26 Correct 58 ms 3884 KB Output is correct
27 Correct 11 ms 3868 KB Output is correct
28 Correct 64 ms 3892 KB Output is correct
29 Correct 15 ms 3888 KB Output is correct
30 Correct 65 ms 3888 KB Output is correct
31 Correct 14 ms 3884 KB Output is correct
32 Correct 58 ms 3892 KB Output is correct
33 Correct 17 ms 3796 KB Output is correct
34 Correct 8 ms 3884 KB Output is correct
35 Correct 12 ms 3892 KB Output is correct
36 Correct 61 ms 3888 KB Output is correct
37 Correct 12 ms 3896 KB Output is correct
38 Correct 64 ms 3904 KB Output is correct
39 Correct 11 ms 3888 KB Output is correct
40 Correct 62 ms 3896 KB Output is correct
41 Correct 12 ms 3892 KB Output is correct
42 Correct 59 ms 3888 KB Output is correct
43 Correct 11 ms 3796 KB Output is correct
44 Correct 61 ms 3888 KB Output is correct
45 Correct 15 ms 3796 KB Output is correct
46 Correct 61 ms 3796 KB Output is correct
47 Correct 4 ms 3796 KB Output is correct
48 Correct 31 ms 3796 KB Output is correct
49 Correct 32 ms 3888 KB Output is correct
50 Correct 66 ms 3888 KB Output is correct
51 Correct 59 ms 3884 KB Output is correct
52 Correct 197 ms 5636 KB Output is correct
53 Correct 10 ms 5588 KB Output is correct
54 Correct 186 ms 5732 KB Output is correct
55 Correct 12 ms 5588 KB Output is correct
56 Correct 209 ms 5732 KB Output is correct
57 Correct 10 ms 5508 KB Output is correct
58 Correct 173 ms 5732 KB Output is correct
59 Correct 10 ms 5588 KB Output is correct
60 Correct 188 ms 5728 KB Output is correct
61 Correct 10 ms 5588 KB Output is correct
62 Correct 175 ms 5716 KB Output is correct
63 Correct 10 ms 5588 KB Output is correct
64 Correct 181 ms 5724 KB Output is correct
65 Correct 186 ms 5736 KB Output is correct
66 Correct 10 ms 5588 KB Output is correct
67 Correct 179 ms 5728 KB Output is correct
68 Correct 10 ms 5624 KB Output is correct
69 Correct 217 ms 5736 KB Output is correct
70 Correct 10 ms 5588 KB Output is correct
71 Correct 178 ms 5736 KB Output is correct
72 Correct 10 ms 5624 KB Output is correct
73 Correct 98 ms 5708 KB Output is correct
74 Correct 908 ms 10252 KB Output is correct
75 Correct 80 ms 9956 KB Output is correct
76 Correct 859 ms 10276 KB Output is correct
77 Correct 82 ms 9996 KB Output is correct
78 Correct 861 ms 10252 KB Output is correct
79 Correct 874 ms 10304 KB Output is correct
80 Correct 81 ms 10116 KB Output is correct
81 Correct 84 ms 9968 KB Output is correct
82 Correct 885 ms 9988 KB Output is correct
83 Correct 913 ms 10248 KB Output is correct
84 Correct 78 ms 9996 KB Output is correct
85 Correct 849 ms 10252 KB Output is correct
86 Correct 90 ms 9900 KB Output is correct
87 Correct 831 ms 10248 KB Output is correct
88 Correct 75 ms 9932 KB Output is correct
89 Correct 856 ms 10244 KB Output is correct
90 Correct 80 ms 9904 KB Output is correct
91 Correct 849 ms 10248 KB Output is correct
92 Correct 94 ms 10064 KB Output is correct
93 Correct 868 ms 10244 KB Output is correct
94 Correct 42 ms 9944 KB Output is correct
95 Correct 466 ms 10256 KB Output is correct
96 Correct 463 ms 10064 KB Output is correct
97 Correct 856 ms 10244 KB Output is correct
98 Correct 892 ms 10248 KB Output is correct