답안 #726448

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
726448 2023-04-18T23:37:15 Z dozer Tug of War (BOI15_tug) C++14
100 / 100
956 ms 11172 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#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";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 3 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 3 ms 3412 KB Output is correct
7 Correct 3 ms 3412 KB Output is correct
8 Correct 2 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 3 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
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 3 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 3 ms 3412 KB Output is correct
7 Correct 3 ms 3412 KB Output is correct
8 Correct 2 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 3 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 62 ms 3884 KB Output is correct
27 Correct 11 ms 3796 KB Output is correct
28 Correct 61 ms 3916 KB Output is correct
29 Correct 12 ms 3900 KB Output is correct
30 Correct 58 ms 3796 KB Output is correct
31 Correct 12 ms 3888 KB Output is correct
32 Correct 58 ms 3884 KB Output is correct
33 Correct 12 ms 3888 KB Output is correct
34 Correct 8 ms 3796 KB Output is correct
35 Correct 12 ms 3856 KB Output is correct
36 Correct 60 ms 3892 KB Output is correct
37 Correct 12 ms 3796 KB Output is correct
38 Correct 58 ms 3888 KB Output is correct
39 Correct 11 ms 3888 KB Output is correct
40 Correct 56 ms 3884 KB Output is correct
41 Correct 12 ms 3888 KB Output is correct
42 Correct 60 ms 3796 KB Output is correct
43 Correct 13 ms 3796 KB Output is correct
44 Correct 61 ms 3796 KB Output is correct
45 Correct 11 ms 3796 KB Output is correct
46 Correct 60 ms 3904 KB Output is correct
47 Correct 4 ms 3796 KB Output is correct
48 Correct 34 ms 3880 KB Output is correct
49 Correct 35 ms 3884 KB Output is correct
50 Correct 59 ms 3948 KB Output is correct
51 Correct 61 ms 3936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 183 ms 5748 KB Output is correct
2 Correct 15 ms 5588 KB Output is correct
3 Correct 192 ms 5748 KB Output is correct
4 Correct 11 ms 5588 KB Output is correct
5 Correct 187 ms 5732 KB Output is correct
6 Correct 12 ms 5588 KB Output is correct
7 Correct 217 ms 5732 KB Output is correct
8 Correct 11 ms 5516 KB Output is correct
9 Correct 218 ms 5836 KB Output is correct
10 Correct 10 ms 5588 KB Output is correct
11 Correct 234 ms 5736 KB Output is correct
12 Correct 10 ms 5548 KB Output is correct
13 Correct 184 ms 5728 KB Output is correct
14 Correct 211 ms 5736 KB Output is correct
15 Correct 10 ms 5516 KB Output is correct
16 Correct 178 ms 5728 KB Output is correct
17 Correct 10 ms 5588 KB Output is correct
18 Correct 205 ms 5732 KB Output is correct
19 Correct 11 ms 5548 KB Output is correct
20 Correct 181 ms 5732 KB Output is correct
21 Correct 13 ms 5588 KB Output is correct
22 Correct 113 ms 5700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 3 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 3 ms 3412 KB Output is correct
7 Correct 3 ms 3412 KB Output is correct
8 Correct 2 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 3 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 62 ms 3884 KB Output is correct
27 Correct 11 ms 3796 KB Output is correct
28 Correct 61 ms 3916 KB Output is correct
29 Correct 12 ms 3900 KB Output is correct
30 Correct 58 ms 3796 KB Output is correct
31 Correct 12 ms 3888 KB Output is correct
32 Correct 58 ms 3884 KB Output is correct
33 Correct 12 ms 3888 KB Output is correct
34 Correct 8 ms 3796 KB Output is correct
35 Correct 12 ms 3856 KB Output is correct
36 Correct 60 ms 3892 KB Output is correct
37 Correct 12 ms 3796 KB Output is correct
38 Correct 58 ms 3888 KB Output is correct
39 Correct 11 ms 3888 KB Output is correct
40 Correct 56 ms 3884 KB Output is correct
41 Correct 12 ms 3888 KB Output is correct
42 Correct 60 ms 3796 KB Output is correct
43 Correct 13 ms 3796 KB Output is correct
44 Correct 61 ms 3796 KB Output is correct
45 Correct 11 ms 3796 KB Output is correct
46 Correct 60 ms 3904 KB Output is correct
47 Correct 4 ms 3796 KB Output is correct
48 Correct 34 ms 3880 KB Output is correct
49 Correct 35 ms 3884 KB Output is correct
50 Correct 59 ms 3948 KB Output is correct
51 Correct 61 ms 3936 KB Output is correct
52 Correct 183 ms 5748 KB Output is correct
53 Correct 15 ms 5588 KB Output is correct
54 Correct 192 ms 5748 KB Output is correct
55 Correct 11 ms 5588 KB Output is correct
56 Correct 187 ms 5732 KB Output is correct
57 Correct 12 ms 5588 KB Output is correct
58 Correct 217 ms 5732 KB Output is correct
59 Correct 11 ms 5516 KB Output is correct
60 Correct 218 ms 5836 KB Output is correct
61 Correct 10 ms 5588 KB Output is correct
62 Correct 234 ms 5736 KB Output is correct
63 Correct 10 ms 5548 KB Output is correct
64 Correct 184 ms 5728 KB Output is correct
65 Correct 211 ms 5736 KB Output is correct
66 Correct 10 ms 5516 KB Output is correct
67 Correct 178 ms 5728 KB Output is correct
68 Correct 10 ms 5588 KB Output is correct
69 Correct 205 ms 5732 KB Output is correct
70 Correct 11 ms 5548 KB Output is correct
71 Correct 181 ms 5732 KB Output is correct
72 Correct 13 ms 5588 KB Output is correct
73 Correct 113 ms 5700 KB Output is correct
74 Correct 876 ms 11068 KB Output is correct
75 Correct 103 ms 10804 KB Output is correct
76 Correct 853 ms 11064 KB Output is correct
77 Correct 77 ms 10704 KB Output is correct
78 Correct 956 ms 11068 KB Output is correct
79 Correct 871 ms 11084 KB Output is correct
80 Correct 82 ms 10736 KB Output is correct
81 Correct 89 ms 10780 KB Output is correct
82 Correct 872 ms 10804 KB Output is correct
83 Correct 837 ms 11064 KB Output is correct
84 Correct 78 ms 10700 KB Output is correct
85 Correct 864 ms 11172 KB Output is correct
86 Correct 95 ms 10856 KB Output is correct
87 Correct 838 ms 11144 KB Output is correct
88 Correct 88 ms 10700 KB Output is correct
89 Correct 847 ms 11072 KB Output is correct
90 Correct 80 ms 10804 KB Output is correct
91 Correct 829 ms 11068 KB Output is correct
92 Correct 84 ms 10716 KB Output is correct
93 Correct 841 ms 11060 KB Output is correct
94 Correct 38 ms 10688 KB Output is correct
95 Correct 458 ms 11000 KB Output is correct
96 Correct 449 ms 11088 KB Output is correct
97 Correct 857 ms 11148 KB Output is correct
98 Correct 878 ms 11060 KB Output is correct