제출 #1029828

#제출 시각아이디문제언어결과실행 시간메모리
1029828mat_jurTug of War (BOI15_tug)C++17
18 / 100
34 ms904 KiB
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;}
auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";}
#define debug(X) cerr << "["#X"]: " << X << '\n';
#else 
#define cerr if(0)cout
#define debug(X) ;
#endif
using ll = long long;
#define all(v) (v).begin(), (v).end()
#define ssize(x) int(x.size())
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);

	int n, k;
	cin >> n >> k;
	int m = 2*n;
	vector<int> l(m), r(m), s(m);
	for (int i = 0; i < m; ++i) {
		cin >> l[i] >> r[i] >> s[i];
		--l[i];
		--r[i];
	}

	for (int bm = 0; bm < (1<<m); ++bm) {
		if (__builtin_popcount(bm) != n) continue;
		vector<int> a(n), b(n);
		int diff = 0;
		for (int i = 0; i < m; ++i) {
			if (bm & (1<<i)) {a[l[i]]++; diff += s[i];}
			else {b[r[i]]++; diff -= s[i];}
		}

		bool ok = true;
		for (int i = 0; i < n; ++i) 
			if (a[i] != 1 || b[i] != 1) ok = false;

		if (ok && abs(diff) <= k) {cout << "YES \n"; return 0;}
	}

	cout << "NO \n";

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...