Submission #1270681

#TimeUsernameProblemLanguageResultExecution timeMemory
1270681thdh__Tug of War (BOI15_tug)C++20
100 / 100
829 ms7108 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll

using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

/*
Competitive Programming notes that I need to study & fix my dumbass self:

1. Coding:
- Always be sure to check the memory of arrays (maybe use vectors), for loops
- Always try to maximize the memory if possible, even if you are going for subtasks
- Do not exploit #define int long long, it will kill you

2. Stress: 
- Always try generating big testcases and try if they run

3. Time management:
- Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked
- Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time

Time management schedule:
Offline / LAH days (4 problems - 3h):
15' thinking of solution / idea
1. no idea: skip
2. yes idea: continue thinking for <= 15'

+ implementing: <= 20'
+ brute-force: <= 5'
+ test generator: <= 5'

I hate offline because I am dumb
*/

typedef pair<int, int> ii;
const int N = 1e5+5;
const int M = 6e5+5;
const int B = 750;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;} 

int n,k;
vector<pair<ii, int>> adj[N];
int deg[N];
bool vis[N], check[N];
bitset<M> dp;
int tot = 0, totl = 0, cnt = 0;

void dfs(int u, int p) {
	vis[u] = 1;
	bool has = 0;
	for (auto i : adj[u]) 
	{
		int v = i.fi.fi, c = i.fi.se, id = i.se;
		if (id == p) continue;
		if (!vis[v]) 
		{
			// cout<<id<<" "<<c<<endl;
			tot += c;
			if (v <= n) totl += c;
			dfs(v, id);
			has = 1;
		}
	}
	if (!has) 
	{
		for (auto i : adj[u]) 
		{
			int v = i.fi.fi, c = i.fi.se, id = i.se;
			if (id == p || check[v]) continue;
			tot += c;
			if (v <= n) totl += c;
		}
	}
}

void solve()
{
	cin>>n>>k;
	int sum = 0;
	for (int i = 1; i <= 2*n; i++) 
	{
		int u, v, c;
		cin>>u>>v>>c;
		deg[u]++; deg[v+n]++;
		// cout<<u<<" "<<v+n<<" "<<c<<endl;
		adj[u].pb({{v+n, c}, i}); adj[v+n].pb({{u, c}, i});
		sum += c;
	}
	for (int i = 1; i <= 2*n; i++) 
	{
		if (deg[i] == 0) 
		{
			cout<<"NO"; return;
		}
	}
	// for (int i = 1; i <= 2*n; i++) 
	// {
	// 	cout<<deg[i]<<" ";
	// }
	// cout<<endl;
	priority_queue<ii, vector<ii>, greater<ii>> q;
	for (int i = 1; i <= 2*n; i++) if (deg[i] == 1) q.push({deg[i], i});
	int suml = 0;
	while (!q.empty()) 
	{
		int d = q.top().fi, u = q.top().se;
		q.pop();
		vis[u] = 1;
		check[u] = 1;
		for (auto i : adj[u]) 
		{
			int v = i.fi.fi, c = i.fi.se;
			if (vis[v]) continue;
			if (u <= n) suml += c;
			deg[v]--;
			if (deg[v] == 1) q.push({deg[v], v});
		}
	}
	// cout<<suml<<endl;
	dp.set(suml);
	// dfs(5, -1);
	// cout<<tot<<" "<<totl<<endl;
	// cout<<suml<<endl;
	// for (int i = 1; i <= n; i++) cout<<vis[i]<<" "; 
	for (int i = 1; i <= 2*n; i++) 
	{
		if (!vis[i]) 
		{
			tot = 0, totl = 0;
			dfs(i, 0);
			// cout<<i<<" "<<tot<<" "<<totl<<endl;
			dp = (dp << totl) | (dp << (tot-totl));
		}
	}
	int lo = (sum-k) / 2, hi = (sum+k) / 2;
	// cout<<lo<<" "<<hi<<endl;
	for (int i = 1; i <= sum; i++) 
	{
		if (abs(i - (sum-i)) <= k && dp[i]) 
		{
			cout<<"YES";
			return;
		}
	}
	cout<<"NO";
	// cout<<suml;
}

/*
Go through the mistakes you usually make and revise your code, for god's sake...
*/

signed main()
{
	bruh
	//freopen("input.inp","r",stdin);
	//freopen("output.inp","w",stdout);
	int t = 1;
	// cin>>t;
	while (t--)
	{
		solve();
		cout<<"\n";
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...