Submission #677186

# Submission time Handle Problem Language Result Execution time Memory
677186 2023-01-02T13:56:57 Z Melika0gh Tug of War (BOI15_tug) C++17
41 / 100
66 ms 22016 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double   ld;
#define sync	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define pb		push_back
#define pii		pair<int, int>
#define mp		make_pair
#define fi		first
#define se		second


const int maxn = 3e4 + 10;
int n, k, lst;
vector<int> adj[maxn * 2], tmp[maxn * 2];
vector<int> cycle[maxn];
int s[maxn * 2], l[maxn * 2], r[maxn * 2], vis[maxn * 2], comp[maxn * 2], ans, N, E;
bool mark[maxn * 2];
vector<int> res, path;

void Dfs(int v, int p)
{
	vis[v] = 1;
	comp[v] = lst;
	int tmp_res = 0;
	N++;
	for(int id : adj[v])
	{
		E++;
		if(id == p)
			continue;
		
		int u = l[id] + r[id] - v;
		if(!vis[u])
		{
			path.pb(id);
			Dfs(u, id);
			path.pop_back();
		}
		else if(vis[u] == 1)
		{
			//cout << v << " - " << u << endl;
			path.pb(id);
			int pnt = path.size() - 1;
			while(pnt >= 0)
			{
				mark[l[path[pnt]]] = mark[r[path[pnt]]] = 1;
				if(pnt % 2)
					tmp_res += s[path[pnt]];
				else
					tmp_res -= s[path[pnt]];
					
			//	cout << l[path[pnt]] << " - " << r[path[pnt]] << " : " << s[path[pnt]] << endl;
				cycle[lst].pb(path[pnt]);
				if((l[path[pnt]] == u || r[path[pnt]] == u) && path[pnt] != id)
					break;
				
				pnt--;
			}
			/*mark[l[path[pnt]]] = mark[r[path[pnt]]] = 1;
			cycle[lst].pb(path[pnt]);*/
			/*if(pnt % 2)
				tmp_res += s[path[pnt]];
			else
				tmp_res -= s[path[pnt]];*/
		}
		//cout << endl;
	}
	if(tmp_res)
	{
		//cout << lst << " : " << tmp_res << endl;
		res.pb(tmp_res);
	}
	vis[v] = 2;
}


void Dfs2(int v, int p)
{
	for(int id : adj[v])
	{
		int u = l[id] + r[id] - v;
		if(mark[u] || u == p)
			continue;
		
		Dfs2(u, v);
		if(u > n)
			ans -= s[id];
		else
			ans += s[id];
	}
	
	return;
}


bool Check(int mask)
{
	int pos[2 * n + 1];
	for(int i = 0; i < 2 * n + 1; i++)
		pos[i] = 0;
	for(int i = 0; i < 2 * n; i++)
	{
		if((mask >> i) & 1)
		{
			if(pos[l[i+1]])
			{
				return false;
			}
			pos[l[i+1]] = i+1;
		}
		else
		{
			if(pos[r[i+1]])
				return false;
			pos[r[i+1]] = i+1;
		}
	}
	
	int ans = 0;
	for(int i = 1; i <= n; i++)
		ans += s[pos[i]];
	
	for(int i = n+1; i <= 2*n; i++)
		ans -= s[pos[i]];
	
	return abs(ans) <= k;
}

void Solve()
{
	for(int mask = 0; mask < (1 << 2 * n) - 1; mask++)
	{
		if(Check(mask))
		{
			cout << "YES" << '\n';
			return;
		}
	}
	
	cout << "NO" << '\n';
	return;
}

int main()
{
	sync;
	cin >> n >> k;
	for(int i = 1; i <= 2 * n; i++)
	{
		cin >> l[i] >> r[i] >> s[i];
		r[i] += n;
		adj[l[i]].pb(i);
		adj[r[i]].pb(i);
		
	}
	if(n <= 10)
	{
		Solve();
		return 0;
	}
	for(int i = 1; i <= 2 * n; i++)
	{
		if(!vis[i])
		{
			lst++;
			N = 0;
			E = 0;
			Dfs(i, 0);
			E /= 2;
			if(N != E)
			{
				cout << "NO" << '\n';
				return 0;
			}
		}
	}
//	cout << lst << endl;
	for(int i = 1; i <= 2 * n; i++)
	{
		if(mark[i])
		{
			Dfs2(i, 0);
		}
	}
	sort(res.begin(), res.end());
//	reverse(res.begin(), res.end());
//	cout << ans << endl;
	for(int x : res)
	{
		if(ans < 0)
			ans += abs(x);
		else
			ans += 0 - abs(x);
		
	//	cout << x << " : " << ans << endl;
	}
	
	if(abs(ans) > k)
		cout << "NO" << '\n';
	else
		cout << "YES" << '\n';
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 37 ms 3796 KB Output is correct
2 Correct 3 ms 3796 KB Output is correct
3 Correct 33 ms 3796 KB Output is correct
4 Correct 2 ms 3856 KB Output is correct
5 Correct 50 ms 3852 KB Output is correct
6 Correct 28 ms 3848 KB Output is correct
7 Correct 3 ms 3796 KB Output is correct
8 Correct 14 ms 3864 KB Output is correct
9 Correct 4 ms 3796 KB Output is correct
10 Correct 35 ms 3796 KB Output is correct
11 Correct 4 ms 3796 KB Output is correct
12 Correct 3 ms 3796 KB Output is correct
13 Correct 6 ms 3796 KB Output is correct
14 Correct 3 ms 3796 KB Output is correct
15 Correct 2 ms 3796 KB Output is correct
16 Correct 40 ms 3844 KB Output is correct
17 Correct 6 ms 3796 KB Output is correct
18 Correct 4 ms 3796 KB Output is correct
19 Correct 3 ms 3844 KB Output is correct
20 Correct 8 ms 3856 KB Output is correct
21 Correct 27 ms 3796 KB Output is correct
22 Correct 4 ms 3796 KB Output is correct
23 Correct 34 ms 3796 KB Output is correct
24 Correct 3 ms 3796 KB Output is correct
25 Correct 41 ms 3796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 3796 KB Output is correct
2 Correct 3 ms 3796 KB Output is correct
3 Correct 33 ms 3796 KB Output is correct
4 Correct 2 ms 3856 KB Output is correct
5 Correct 50 ms 3852 KB Output is correct
6 Correct 28 ms 3848 KB Output is correct
7 Correct 3 ms 3796 KB Output is correct
8 Correct 14 ms 3864 KB Output is correct
9 Correct 4 ms 3796 KB Output is correct
10 Correct 35 ms 3796 KB Output is correct
11 Correct 4 ms 3796 KB Output is correct
12 Correct 3 ms 3796 KB Output is correct
13 Correct 6 ms 3796 KB Output is correct
14 Correct 3 ms 3796 KB Output is correct
15 Correct 2 ms 3796 KB Output is correct
16 Correct 40 ms 3844 KB Output is correct
17 Correct 6 ms 3796 KB Output is correct
18 Correct 4 ms 3796 KB Output is correct
19 Correct 3 ms 3844 KB Output is correct
20 Correct 8 ms 3856 KB Output is correct
21 Correct 27 ms 3796 KB Output is correct
22 Correct 4 ms 3796 KB Output is correct
23 Correct 34 ms 3796 KB Output is correct
24 Correct 3 ms 3796 KB Output is correct
25 Correct 41 ms 3796 KB Output is correct
26 Incorrect 4 ms 4180 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 5380 KB Output is correct
2 Correct 8 ms 5076 KB Output is correct
3 Correct 11 ms 5476 KB Output is correct
4 Correct 9 ms 4948 KB Output is correct
5 Correct 10 ms 5400 KB Output is correct
6 Correct 9 ms 4928 KB Output is correct
7 Correct 10 ms 5404 KB Output is correct
8 Correct 8 ms 5032 KB Output is correct
9 Correct 10 ms 5460 KB Output is correct
10 Correct 12 ms 5076 KB Output is correct
11 Correct 10 ms 5460 KB Output is correct
12 Correct 8 ms 5012 KB Output is correct
13 Correct 9 ms 5436 KB Output is correct
14 Correct 10 ms 5460 KB Output is correct
15 Correct 8 ms 4948 KB Output is correct
16 Correct 10 ms 5404 KB Output is correct
17 Correct 9 ms 5076 KB Output is correct
18 Correct 10 ms 5460 KB Output is correct
19 Correct 8 ms 5052 KB Output is correct
20 Correct 12 ms 5440 KB Output is correct
21 Correct 66 ms 22016 KB Output is correct
22 Correct 10 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 3796 KB Output is correct
2 Correct 3 ms 3796 KB Output is correct
3 Correct 33 ms 3796 KB Output is correct
4 Correct 2 ms 3856 KB Output is correct
5 Correct 50 ms 3852 KB Output is correct
6 Correct 28 ms 3848 KB Output is correct
7 Correct 3 ms 3796 KB Output is correct
8 Correct 14 ms 3864 KB Output is correct
9 Correct 4 ms 3796 KB Output is correct
10 Correct 35 ms 3796 KB Output is correct
11 Correct 4 ms 3796 KB Output is correct
12 Correct 3 ms 3796 KB Output is correct
13 Correct 6 ms 3796 KB Output is correct
14 Correct 3 ms 3796 KB Output is correct
15 Correct 2 ms 3796 KB Output is correct
16 Correct 40 ms 3844 KB Output is correct
17 Correct 6 ms 3796 KB Output is correct
18 Correct 4 ms 3796 KB Output is correct
19 Correct 3 ms 3844 KB Output is correct
20 Correct 8 ms 3856 KB Output is correct
21 Correct 27 ms 3796 KB Output is correct
22 Correct 4 ms 3796 KB Output is correct
23 Correct 34 ms 3796 KB Output is correct
24 Correct 3 ms 3796 KB Output is correct
25 Correct 41 ms 3796 KB Output is correct
26 Incorrect 4 ms 4180 KB Output isn't correct
27 Halted 0 ms 0 KB -