제출 #1307583

#제출 시각아이디문제언어결과실행 시간메모리
1307583lvsTug of War (BOI15_tug)C++20
41 / 100
3094 ms1608 KiB
#include <bits/stdc++.h>

#define zet ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int N = 6e4+555, M = 1e6+557, mod = 1e9+7;
int n, k, l[N], r[N], a[N], dp[N][5];
vector <int> st;
bool res, s, used[N];
vector <int> mp[N], m[N];

void ff(int v, int ps) {
	used[ps] = 1;
	if (v == n) {
		res = 1;
		return;
	}
	for (auto to : m[v+1]) {
		if(!used[to]) ff(v+1, to);
	}
	used[ps] = 0;
	return;
}
void f(int v, int ps) {
	used[ps] = 1;
	if (v == n) {
		for (auto to : m[1]) {
			if (!used[to]) ff(1, to);
		}
	}
	for (auto to : mp[v+1]) {
		if (!used[to]) f(v+1, to);
	}
	used[ps] = 0;
	return;
}

bool comp(pair<int, int> a, pair<int, int> b) {
	return a.second <= b.second;
}
int main() {
	zet
	cin >> n >> k;
	for (int i = 0; i < 2*n; i++) {
		cin >> l[i] >> r[i] >> a[i];
		if (a[i] != 1) s = 1;
		mp[l[i]].push_back(i);
		m[r[i]].push_back(i);
	}
	if (n <= 10) {
		for (int mk = 1; mk < (1 << (2*n)); mk++) {
			int cnt = 0;
			int ll[n+5], rr[n+5];
			for (int i = 0; i <= n; i++) ll[i] = rr[i] = 0;
			for (int i = 0; i < 2*n; i++) {
				if ((1<<i)&mk) cnt++;
			}
			if (cnt != n) continue;
			int ans = 0;
			bool x = 0;
			for (int i = 0; i < 2*n; i++) {
				if ((1<<i) & mk) {
					ll[l[i]]++;
					if (ll[l[i]] > 1) {
						x = 1;
						break;
					}
					ans += a[i];
				}
				else {
					rr[r[i]]++;
					if (rr[r[i]] > 1) {
						x = 1;
						break;
					}
					ans -= a[i];
				}
			}
			if (abs(ans) <= k && !x){
				res = 1;
				break;
			}
		}
	} else if(!s){
		res = 1;
		for (int i = 1; i<= n; i++) {
			if (m[i].size() == 0 || mp[i].size() == 0) res = 0;
		}
	}
	else {
		for (auto to : mp[1]) {
			f(1, to);
		}
	}
	cout << ((res) ? "YES" : "NO");
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...