// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
using db = long double;
template<class T> using pq = priority_queue<T, V<T>, greater<T>>;
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
const int LG = 18;
const int nax = 3e4 + 5;
const int kax = 20;
const int ZERO = nax * kax;
using B = bitset<2 * ZERO>;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, K; cin >> N >> K;
int M = N; N += N;
vi deg(N); vi W(N);
V<vpi> adj(N); for(int i = 0; i < N; i++) {
int u, v, w; cin >> u >> v >> w; --u, --v, v += M;
W[i] = w;
adj[u].pb(mp(v, i));
adj[v].pb(mp(u, i));
deg[u]++, deg[v]++;
}
queue<int> q; for(int i = 0; i < N; i++) {
if (deg[i] == 0) {
cout << "NO" << nl;
exit(0-0);
}
if (deg[i] == 1) q.push(i);
}
vi used(N), vis(N); int BAL = 0;
while(sz(q)) {
int u = q.front(); q.pop();
vis[u] = 1;
int x = 0;
for(auto& e : adj[u]) {
auto [v, i] = e;
if (used[i]) continue;
used[i] = 1;
deg[v]--;
if (deg[v] == 0) q.push(v);
x = W[i];
}
if (u < M) BAL += x;
else BAL -= x;
}
for(int i = 0; i < N; i++) {
if (deg[i] == 0) continue;
if (deg[i] != 2) {
cout << "NO" << nl;
exit(0-0);
}
}
int bal = 0;
function<void(int, int)> dfs = [&](int u, int d) {
vis[u] = 1;
for(auto& e : adj[u]) {
auto [v, i] = e;
if (used[i]) continue;
// cout << d << endl;
bal += d * W[i];
// cout << u << " -> " << v << " C " << d*W[i] << endl;
used[i] = 1; dfs(v, -d);
}
};
vi X;
for(int u = 0; u < M; u++) if (!vis[u]) {
bal = 0; dfs(u, 1); X.pb(abs(bal));
// cout << bal << endl << endl;
}
B dp; dp[ZERO + BAL] = 1;
for(auto x : X) if (x) {
dp = (dp << x) | (dp >> x);
}
for(int x = 0; x < 2*ZERO; x++) if (dp[x]) {
// cout << x << endl;
if (abs(x - ZERO) <= K) {
cout << "YES" << nl;
exit(0-0);
}
}
cout << "NO" << nl;
exit(0-0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1112 KB |
Output is correct |
2 |
Correct |
2 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Correct |
2 ms |
1112 KB |
Output is correct |
5 |
Correct |
2 ms |
1112 KB |
Output is correct |
6 |
Correct |
3 ms |
1112 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1112 KB |
Output is correct |
11 |
Correct |
2 ms |
1112 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1112 KB |
Output is correct |
14 |
Correct |
2 ms |
1112 KB |
Output is correct |
15 |
Correct |
2 ms |
1112 KB |
Output is correct |
16 |
Correct |
2 ms |
1112 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
2 ms |
1112 KB |
Output is correct |
19 |
Correct |
2 ms |
1116 KB |
Output is correct |
20 |
Correct |
2 ms |
1112 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Incorrect |
1 ms |
856 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1112 KB |
Output is correct |
2 |
Correct |
2 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Correct |
2 ms |
1112 KB |
Output is correct |
5 |
Correct |
2 ms |
1112 KB |
Output is correct |
6 |
Correct |
3 ms |
1112 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1112 KB |
Output is correct |
11 |
Correct |
2 ms |
1112 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1112 KB |
Output is correct |
14 |
Correct |
2 ms |
1112 KB |
Output is correct |
15 |
Correct |
2 ms |
1112 KB |
Output is correct |
16 |
Correct |
2 ms |
1112 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
2 ms |
1112 KB |
Output is correct |
19 |
Correct |
2 ms |
1116 KB |
Output is correct |
20 |
Correct |
2 ms |
1112 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Incorrect |
1 ms |
856 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2392 KB |
Output is correct |
2 |
Correct |
5 ms |
2392 KB |
Output is correct |
3 |
Correct |
6 ms |
2396 KB |
Output is correct |
4 |
Correct |
5 ms |
2396 KB |
Output is correct |
5 |
Correct |
6 ms |
2392 KB |
Output is correct |
6 |
Correct |
5 ms |
2392 KB |
Output is correct |
7 |
Correct |
6 ms |
2392 KB |
Output is correct |
8 |
Correct |
6 ms |
2392 KB |
Output is correct |
9 |
Correct |
6 ms |
2392 KB |
Output is correct |
10 |
Correct |
5 ms |
2392 KB |
Output is correct |
11 |
Correct |
6 ms |
2392 KB |
Output is correct |
12 |
Correct |
5 ms |
2392 KB |
Output is correct |
13 |
Correct |
6 ms |
2392 KB |
Output is correct |
14 |
Correct |
6 ms |
2396 KB |
Output is correct |
15 |
Correct |
5 ms |
2648 KB |
Output is correct |
16 |
Correct |
6 ms |
2396 KB |
Output is correct |
17 |
Correct |
5 ms |
2392 KB |
Output is correct |
18 |
Correct |
6 ms |
2392 KB |
Output is correct |
19 |
Correct |
5 ms |
2396 KB |
Output is correct |
20 |
Correct |
6 ms |
2392 KB |
Output is correct |
21 |
Correct |
6 ms |
2136 KB |
Output is correct |
22 |
Incorrect |
5 ms |
2392 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1112 KB |
Output is correct |
2 |
Correct |
2 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Correct |
2 ms |
1112 KB |
Output is correct |
5 |
Correct |
2 ms |
1112 KB |
Output is correct |
6 |
Correct |
3 ms |
1112 KB |
Output is correct |
7 |
Correct |
2 ms |
1116 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1112 KB |
Output is correct |
11 |
Correct |
2 ms |
1112 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1112 KB |
Output is correct |
14 |
Correct |
2 ms |
1112 KB |
Output is correct |
15 |
Correct |
2 ms |
1112 KB |
Output is correct |
16 |
Correct |
2 ms |
1112 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
2 ms |
1112 KB |
Output is correct |
19 |
Correct |
2 ms |
1116 KB |
Output is correct |
20 |
Correct |
2 ms |
1112 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Incorrect |
1 ms |
856 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |