Submission #481997

# Submission time Handle Problem Language Result Execution time Memory
481997 2021-10-22T15:07:22 Z pooyashams Tug of War (BOI15_tug) C++17
71 / 100
195 ms 17888 KB
#include <algorithm>
#include <numeric>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>

using namespace std;
typedef long long ll;

const int maxn = 12e4+10;

bitset<maxn> dp;

vector<int> G[maxn];
int lfts[maxn];
int rits[maxn];
int strn[maxn];
set<int> conts[maxn];

bool vis[maxn];

int dfs(int v)
{
	vis[v] = true;
	int out = strn[v];
	for(int u: G[v])
	{
		if(vis[u]) continue;
		out -= dfs(u);
	}
	return out;
}

inline void die()
{
	cout << "NO" << endl;
	return exit(0);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, k; cin >> n >> k;
	for(int i = 0; i < 2*n; i++)
	{
		cin >> lfts[i] >> rits[i] >> strn[i];
		lfts[i]--; rits[i]--;
		rits[i] += n;
		conts[lfts[i]].insert(i);
		conts[rits[i]].insert(i);
	}
	queue<int> theq;
	for(int i = 0; i < 2*n; i++)
	{
		if(conts[i].size() == 0)
			die();
		if(conts[i].size() == 1)
			theq.push(i);
	}
	int dsum = 0; // diff sum
	while(!theq.empty())
	{
		int t = theq.front(); theq.pop();
		int i = *conts[t].begin();
		int r =  lfts[i] + rits[i] - t;
		vis[i] = true;
		conts[t].erase(i);
		conts[r].erase(i);
		if(conts[r].size() == 0)
			die();
		if(conts[r].size() == 1)
			theq.push(r);
		dsum += strn[i] * ( (t/n) * 2 - 1  );
		//cerr << t << endl;
	}
	dsum = abs(dsum);
	for(int i = 0; i < 2*n; i++)
	{
		if(conts[i].size() != 2) continue;
		int x = *conts[i].begin();
		int y = *(++conts[i].begin());
		G[x].push_back(y);
		G[y].push_back(x);
	}
	dp[0] = true;
	int tsum = 0;
	for(int v = 0; v < 2*n; v++)
	{
		if(vis[v]) continue;
		int x = abs(dfs(v));
		//cerr << x << endl;
		dp = dp | (dp << x);
		tsum += x;
	}
	bool ans = false;
	for(int i = 0; i < maxn; i++)
	{
		if(!dp[i]) continue;
		ans = ans or (abs(tsum-i-i-dsum) <= k);
		ans = ans or (abs(tsum-i-i+dsum) <= k);
	}
	if(ans)
		cout << "YES" << endl;
	else
		die();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8816 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 5 ms 8780 KB Output is correct
4 Correct 6 ms 8808 KB Output is correct
5 Correct 5 ms 8780 KB Output is correct
6 Correct 7 ms 8780 KB Output is correct
7 Correct 5 ms 8812 KB Output is correct
8 Correct 4 ms 8780 KB Output is correct
9 Correct 4 ms 8780 KB Output is correct
10 Correct 5 ms 8780 KB Output is correct
11 Correct 5 ms 8812 KB Output is correct
12 Correct 4 ms 8836 KB Output is correct
13 Correct 5 ms 8812 KB Output is correct
14 Correct 5 ms 8780 KB Output is correct
15 Correct 4 ms 8780 KB Output is correct
16 Correct 6 ms 8780 KB Output is correct
17 Correct 5 ms 8780 KB Output is correct
18 Correct 6 ms 8736 KB Output is correct
19 Correct 5 ms 8784 KB Output is correct
20 Correct 6 ms 8780 KB Output is correct
21 Correct 6 ms 8732 KB Output is correct
22 Correct 5 ms 8780 KB Output is correct
23 Correct 4 ms 8780 KB Output is correct
24 Correct 5 ms 8804 KB Output is correct
25 Correct 5 ms 8780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8816 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 5 ms 8780 KB Output is correct
4 Correct 6 ms 8808 KB Output is correct
5 Correct 5 ms 8780 KB Output is correct
6 Correct 7 ms 8780 KB Output is correct
7 Correct 5 ms 8812 KB Output is correct
8 Correct 4 ms 8780 KB Output is correct
9 Correct 4 ms 8780 KB Output is correct
10 Correct 5 ms 8780 KB Output is correct
11 Correct 5 ms 8812 KB Output is correct
12 Correct 4 ms 8836 KB Output is correct
13 Correct 5 ms 8812 KB Output is correct
14 Correct 5 ms 8780 KB Output is correct
15 Correct 4 ms 8780 KB Output is correct
16 Correct 6 ms 8780 KB Output is correct
17 Correct 5 ms 8780 KB Output is correct
18 Correct 6 ms 8736 KB Output is correct
19 Correct 5 ms 8784 KB Output is correct
20 Correct 6 ms 8780 KB Output is correct
21 Correct 6 ms 8732 KB Output is correct
22 Correct 5 ms 8780 KB Output is correct
23 Correct 4 ms 8780 KB Output is correct
24 Correct 5 ms 8804 KB Output is correct
25 Correct 5 ms 8780 KB Output is correct
26 Correct 17 ms 9420 KB Output is correct
27 Correct 8 ms 9328 KB Output is correct
28 Correct 16 ms 9424 KB Output is correct
29 Correct 7 ms 9420 KB Output is correct
30 Correct 16 ms 9420 KB Output is correct
31 Correct 8 ms 9420 KB Output is correct
32 Correct 15 ms 9428 KB Output is correct
33 Correct 8 ms 9420 KB Output is correct
34 Correct 7 ms 9292 KB Output is correct
35 Correct 8 ms 9304 KB Output is correct
36 Correct 17 ms 9420 KB Output is correct
37 Correct 10 ms 9420 KB Output is correct
38 Correct 15 ms 9420 KB Output is correct
39 Correct 8 ms 9420 KB Output is correct
40 Correct 15 ms 9420 KB Output is correct
41 Correct 8 ms 9320 KB Output is correct
42 Correct 15 ms 9424 KB Output is correct
43 Correct 8 ms 9420 KB Output is correct
44 Correct 15 ms 9328 KB Output is correct
45 Correct 7 ms 9420 KB Output is correct
46 Correct 15 ms 9420 KB Output is correct
47 Correct 6 ms 9164 KB Output is correct
48 Correct 11 ms 9292 KB Output is correct
49 Correct 11 ms 9328 KB Output is correct
50 Correct 16 ms 9420 KB Output is correct
51 Correct 16 ms 9420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 11724 KB Output is correct
2 Correct 16 ms 11084 KB Output is correct
3 Correct 49 ms 11704 KB Output is correct
4 Correct 13 ms 11124 KB Output is correct
5 Correct 42 ms 11724 KB Output is correct
6 Correct 17 ms 11084 KB Output is correct
7 Correct 60 ms 11772 KB Output is correct
8 Correct 12 ms 11084 KB Output is correct
9 Correct 42 ms 11724 KB Output is correct
10 Correct 12 ms 11084 KB Output is correct
11 Correct 46 ms 11852 KB Output is correct
12 Correct 13 ms 11084 KB Output is correct
13 Correct 42 ms 11724 KB Output is correct
14 Correct 42 ms 11760 KB Output is correct
15 Correct 12 ms 11156 KB Output is correct
16 Correct 41 ms 11724 KB Output is correct
17 Correct 12 ms 11156 KB Output is correct
18 Correct 44 ms 11764 KB Output is correct
19 Correct 12 ms 11084 KB Output is correct
20 Correct 43 ms 11724 KB Output is correct
21 Correct 12 ms 11084 KB Output is correct
22 Correct 29 ms 11260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8816 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 5 ms 8780 KB Output is correct
4 Correct 6 ms 8808 KB Output is correct
5 Correct 5 ms 8780 KB Output is correct
6 Correct 7 ms 8780 KB Output is correct
7 Correct 5 ms 8812 KB Output is correct
8 Correct 4 ms 8780 KB Output is correct
9 Correct 4 ms 8780 KB Output is correct
10 Correct 5 ms 8780 KB Output is correct
11 Correct 5 ms 8812 KB Output is correct
12 Correct 4 ms 8836 KB Output is correct
13 Correct 5 ms 8812 KB Output is correct
14 Correct 5 ms 8780 KB Output is correct
15 Correct 4 ms 8780 KB Output is correct
16 Correct 6 ms 8780 KB Output is correct
17 Correct 5 ms 8780 KB Output is correct
18 Correct 6 ms 8736 KB Output is correct
19 Correct 5 ms 8784 KB Output is correct
20 Correct 6 ms 8780 KB Output is correct
21 Correct 6 ms 8732 KB Output is correct
22 Correct 5 ms 8780 KB Output is correct
23 Correct 4 ms 8780 KB Output is correct
24 Correct 5 ms 8804 KB Output is correct
25 Correct 5 ms 8780 KB Output is correct
26 Correct 17 ms 9420 KB Output is correct
27 Correct 8 ms 9328 KB Output is correct
28 Correct 16 ms 9424 KB Output is correct
29 Correct 7 ms 9420 KB Output is correct
30 Correct 16 ms 9420 KB Output is correct
31 Correct 8 ms 9420 KB Output is correct
32 Correct 15 ms 9428 KB Output is correct
33 Correct 8 ms 9420 KB Output is correct
34 Correct 7 ms 9292 KB Output is correct
35 Correct 8 ms 9304 KB Output is correct
36 Correct 17 ms 9420 KB Output is correct
37 Correct 10 ms 9420 KB Output is correct
38 Correct 15 ms 9420 KB Output is correct
39 Correct 8 ms 9420 KB Output is correct
40 Correct 15 ms 9420 KB Output is correct
41 Correct 8 ms 9320 KB Output is correct
42 Correct 15 ms 9424 KB Output is correct
43 Correct 8 ms 9420 KB Output is correct
44 Correct 15 ms 9328 KB Output is correct
45 Correct 7 ms 9420 KB Output is correct
46 Correct 15 ms 9420 KB Output is correct
47 Correct 6 ms 9164 KB Output is correct
48 Correct 11 ms 9292 KB Output is correct
49 Correct 11 ms 9328 KB Output is correct
50 Correct 16 ms 9420 KB Output is correct
51 Correct 16 ms 9420 KB Output is correct
52 Correct 42 ms 11724 KB Output is correct
53 Correct 16 ms 11084 KB Output is correct
54 Correct 49 ms 11704 KB Output is correct
55 Correct 13 ms 11124 KB Output is correct
56 Correct 42 ms 11724 KB Output is correct
57 Correct 17 ms 11084 KB Output is correct
58 Correct 60 ms 11772 KB Output is correct
59 Correct 12 ms 11084 KB Output is correct
60 Correct 42 ms 11724 KB Output is correct
61 Correct 12 ms 11084 KB Output is correct
62 Correct 46 ms 11852 KB Output is correct
63 Correct 13 ms 11084 KB Output is correct
64 Correct 42 ms 11724 KB Output is correct
65 Correct 42 ms 11760 KB Output is correct
66 Correct 12 ms 11156 KB Output is correct
67 Correct 41 ms 11724 KB Output is correct
68 Correct 12 ms 11156 KB Output is correct
69 Correct 44 ms 11764 KB Output is correct
70 Correct 12 ms 11084 KB Output is correct
71 Correct 43 ms 11724 KB Output is correct
72 Correct 12 ms 11084 KB Output is correct
73 Correct 29 ms 11260 KB Output is correct
74 Correct 195 ms 17824 KB Output is correct
75 Correct 64 ms 17860 KB Output is correct
76 Correct 185 ms 17852 KB Output is correct
77 Correct 62 ms 17864 KB Output is correct
78 Correct 183 ms 17888 KB Output is correct
79 Correct 180 ms 17860 KB Output is correct
80 Correct 56 ms 17860 KB Output is correct
81 Incorrect 72 ms 17728 KB Output isn't correct
82 Halted 0 ms 0 KB -