#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1e9 + 7;
const int Maxn = 3e4 + 10;
const ll Inf = 1000000000000000LL;
ll s[Maxn << 1], deg[Maxn << 1];
vector<pll> G[Maxn << 1];
queue<ll> q;
ll mk[Maxn << 1];
ll sm = 0, t = 1;
void DFS(ll u){
sm += 2;
sm -= G[u].size();
mk[u] = t;
for(auto adj : G[u]){
if(mk[adj.F] == 0) DFS(adj.F);
}
}
ll del[Maxn << 1];
vector<ll> V;
void DFS2(ll u, ll p, ll p2){
mk[u] = 1;
for(auto e : G[u]){
if(del[e.F]) continue;
if(e.F == p || e.F == p2) continue;
V.pb(s[e.S]);
if(!mk[e.F]) DFS2(e.F, u, p2);
}
}
bitset<300010> bt;
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n, k;
cin >> n >> k;
ll l, r;
for(int i = 0; i < n + n; i++){
cin >> l >> r >> s[i];
r += n;
G[l].pb({r, i});
G[r].pb({l, i});
deg[l] ++;
deg[r] ++;
}
for(int i = 1; i <= n + n; i++){
if(!mk[i]){
DFS(i);
t ++;
if(sm != 0) return cout << "NO", 0;
}
}
ll S = 0;
for(int i = 1; i <= n + n; i++){
if(deg[i] == 1) q.push(i);
}
while(q.size()){
ll fr = q.front(); q.pop();
del[fr] = 1;
for(auto e : G[fr]){
if(del[e.F]) continue;
if(fr > n) S -= s[e.S];
else S += s[e.S];
deg[e.F] --;
if(deg[e.F] == 1){
q.push(e.F);
}
}
}
//cerr << S << '\n';
memset(mk, 0, sizeof mk);
bt[0] = 1;
ll A = 0;
for(int i = 1; i <= n + n; i++){
if(del[i] || mk[i]) continue;
V.clear();
DFS2(i, 0, i);
ll cnt = 0;
for(auto x : V) cnt = x - cnt;
//cerr << cnt << '\n';
cnt = abs(cnt);
bt |= (bt << cnt);
A += cnt;
}
for(int i = 0; i <= 300000; i++){
if(bt[i]){
ll res = S + i - (A - i);
if(abs(res) <= k) return cout << "YES\n", 0;
}
}
cout << "NO";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2296 KB |
Output is correct |
2 |
Correct |
4 ms |
2296 KB |
Output is correct |
3 |
Correct |
5 ms |
2300 KB |
Output is correct |
4 |
Correct |
4 ms |
2296 KB |
Output is correct |
5 |
Correct |
5 ms |
2296 KB |
Output is correct |
6 |
Correct |
5 ms |
2296 KB |
Output is correct |
7 |
Correct |
4 ms |
2296 KB |
Output is correct |
8 |
Correct |
4 ms |
2296 KB |
Output is correct |
9 |
Correct |
5 ms |
2296 KB |
Output is correct |
10 |
Correct |
4 ms |
2296 KB |
Output is correct |
11 |
Correct |
4 ms |
2296 KB |
Output is correct |
12 |
Correct |
4 ms |
2424 KB |
Output is correct |
13 |
Correct |
4 ms |
2356 KB |
Output is correct |
14 |
Correct |
4 ms |
2300 KB |
Output is correct |
15 |
Correct |
4 ms |
2296 KB |
Output is correct |
16 |
Correct |
5 ms |
2296 KB |
Output is correct |
17 |
Correct |
4 ms |
2296 KB |
Output is correct |
18 |
Correct |
4 ms |
2296 KB |
Output is correct |
19 |
Correct |
4 ms |
2296 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
3 ms |
1784 KB |
Output is correct |
22 |
Correct |
4 ms |
2296 KB |
Output is correct |
23 |
Correct |
4 ms |
2316 KB |
Output is correct |
24 |
Correct |
4 ms |
2296 KB |
Output is correct |
25 |
Correct |
5 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2296 KB |
Output is correct |
2 |
Correct |
4 ms |
2296 KB |
Output is correct |
3 |
Correct |
5 ms |
2300 KB |
Output is correct |
4 |
Correct |
4 ms |
2296 KB |
Output is correct |
5 |
Correct |
5 ms |
2296 KB |
Output is correct |
6 |
Correct |
5 ms |
2296 KB |
Output is correct |
7 |
Correct |
4 ms |
2296 KB |
Output is correct |
8 |
Correct |
4 ms |
2296 KB |
Output is correct |
9 |
Correct |
5 ms |
2296 KB |
Output is correct |
10 |
Correct |
4 ms |
2296 KB |
Output is correct |
11 |
Correct |
4 ms |
2296 KB |
Output is correct |
12 |
Correct |
4 ms |
2424 KB |
Output is correct |
13 |
Correct |
4 ms |
2356 KB |
Output is correct |
14 |
Correct |
4 ms |
2300 KB |
Output is correct |
15 |
Correct |
4 ms |
2296 KB |
Output is correct |
16 |
Correct |
5 ms |
2296 KB |
Output is correct |
17 |
Correct |
4 ms |
2296 KB |
Output is correct |
18 |
Correct |
4 ms |
2296 KB |
Output is correct |
19 |
Correct |
4 ms |
2296 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
3 ms |
1784 KB |
Output is correct |
22 |
Correct |
4 ms |
2296 KB |
Output is correct |
23 |
Correct |
4 ms |
2316 KB |
Output is correct |
24 |
Correct |
4 ms |
2296 KB |
Output is correct |
25 |
Correct |
5 ms |
2296 KB |
Output is correct |
26 |
Correct |
39 ms |
2712 KB |
Output is correct |
27 |
Correct |
12 ms |
2756 KB |
Output is correct |
28 |
Correct |
39 ms |
2680 KB |
Output is correct |
29 |
Correct |
12 ms |
2680 KB |
Output is correct |
30 |
Correct |
39 ms |
2716 KB |
Output is correct |
31 |
Correct |
11 ms |
2680 KB |
Output is correct |
32 |
Correct |
52 ms |
2680 KB |
Output is correct |
33 |
Correct |
11 ms |
2660 KB |
Output is correct |
34 |
Correct |
9 ms |
2680 KB |
Output is correct |
35 |
Correct |
12 ms |
2680 KB |
Output is correct |
36 |
Correct |
39 ms |
2808 KB |
Output is correct |
37 |
Correct |
12 ms |
2680 KB |
Output is correct |
38 |
Correct |
40 ms |
2716 KB |
Output is correct |
39 |
Correct |
11 ms |
2680 KB |
Output is correct |
40 |
Correct |
38 ms |
2712 KB |
Output is correct |
41 |
Correct |
11 ms |
2680 KB |
Output is correct |
42 |
Correct |
39 ms |
2680 KB |
Output is correct |
43 |
Correct |
11 ms |
2680 KB |
Output is correct |
44 |
Correct |
38 ms |
2680 KB |
Output is correct |
45 |
Correct |
11 ms |
2680 KB |
Output is correct |
46 |
Correct |
40 ms |
2680 KB |
Output is correct |
47 |
Correct |
6 ms |
2168 KB |
Output is correct |
48 |
Correct |
23 ms |
2680 KB |
Output is correct |
49 |
Correct |
23 ms |
2808 KB |
Output is correct |
50 |
Correct |
43 ms |
2680 KB |
Output is correct |
51 |
Correct |
40 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
3840 KB |
Output is correct |
2 |
Correct |
14 ms |
3440 KB |
Output is correct |
3 |
Correct |
123 ms |
3832 KB |
Output is correct |
4 |
Correct |
14 ms |
3320 KB |
Output is correct |
5 |
Correct |
106 ms |
3832 KB |
Output is correct |
6 |
Correct |
14 ms |
3448 KB |
Output is correct |
7 |
Correct |
108 ms |
3832 KB |
Output is correct |
8 |
Correct |
15 ms |
3448 KB |
Output is correct |
9 |
Correct |
113 ms |
3832 KB |
Output is correct |
10 |
Correct |
15 ms |
3448 KB |
Output is correct |
11 |
Correct |
109 ms |
3836 KB |
Output is correct |
12 |
Correct |
14 ms |
3448 KB |
Output is correct |
13 |
Correct |
104 ms |
3832 KB |
Output is correct |
14 |
Correct |
107 ms |
3832 KB |
Output is correct |
15 |
Correct |
15 ms |
3320 KB |
Output is correct |
16 |
Correct |
110 ms |
3832 KB |
Output is correct |
17 |
Correct |
14 ms |
3452 KB |
Output is correct |
18 |
Correct |
107 ms |
3832 KB |
Output is correct |
19 |
Correct |
14 ms |
3320 KB |
Output is correct |
20 |
Correct |
107 ms |
3960 KB |
Output is correct |
21 |
Correct |
15 ms |
3448 KB |
Output is correct |
22 |
Correct |
62 ms |
4092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2296 KB |
Output is correct |
2 |
Correct |
4 ms |
2296 KB |
Output is correct |
3 |
Correct |
5 ms |
2300 KB |
Output is correct |
4 |
Correct |
4 ms |
2296 KB |
Output is correct |
5 |
Correct |
5 ms |
2296 KB |
Output is correct |
6 |
Correct |
5 ms |
2296 KB |
Output is correct |
7 |
Correct |
4 ms |
2296 KB |
Output is correct |
8 |
Correct |
4 ms |
2296 KB |
Output is correct |
9 |
Correct |
5 ms |
2296 KB |
Output is correct |
10 |
Correct |
4 ms |
2296 KB |
Output is correct |
11 |
Correct |
4 ms |
2296 KB |
Output is correct |
12 |
Correct |
4 ms |
2424 KB |
Output is correct |
13 |
Correct |
4 ms |
2356 KB |
Output is correct |
14 |
Correct |
4 ms |
2300 KB |
Output is correct |
15 |
Correct |
4 ms |
2296 KB |
Output is correct |
16 |
Correct |
5 ms |
2296 KB |
Output is correct |
17 |
Correct |
4 ms |
2296 KB |
Output is correct |
18 |
Correct |
4 ms |
2296 KB |
Output is correct |
19 |
Correct |
4 ms |
2296 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
3 ms |
1784 KB |
Output is correct |
22 |
Correct |
4 ms |
2296 KB |
Output is correct |
23 |
Correct |
4 ms |
2316 KB |
Output is correct |
24 |
Correct |
4 ms |
2296 KB |
Output is correct |
25 |
Correct |
5 ms |
2296 KB |
Output is correct |
26 |
Correct |
39 ms |
2712 KB |
Output is correct |
27 |
Correct |
12 ms |
2756 KB |
Output is correct |
28 |
Correct |
39 ms |
2680 KB |
Output is correct |
29 |
Correct |
12 ms |
2680 KB |
Output is correct |
30 |
Correct |
39 ms |
2716 KB |
Output is correct |
31 |
Correct |
11 ms |
2680 KB |
Output is correct |
32 |
Correct |
52 ms |
2680 KB |
Output is correct |
33 |
Correct |
11 ms |
2660 KB |
Output is correct |
34 |
Correct |
9 ms |
2680 KB |
Output is correct |
35 |
Correct |
12 ms |
2680 KB |
Output is correct |
36 |
Correct |
39 ms |
2808 KB |
Output is correct |
37 |
Correct |
12 ms |
2680 KB |
Output is correct |
38 |
Correct |
40 ms |
2716 KB |
Output is correct |
39 |
Correct |
11 ms |
2680 KB |
Output is correct |
40 |
Correct |
38 ms |
2712 KB |
Output is correct |
41 |
Correct |
11 ms |
2680 KB |
Output is correct |
42 |
Correct |
39 ms |
2680 KB |
Output is correct |
43 |
Correct |
11 ms |
2680 KB |
Output is correct |
44 |
Correct |
38 ms |
2680 KB |
Output is correct |
45 |
Correct |
11 ms |
2680 KB |
Output is correct |
46 |
Correct |
40 ms |
2680 KB |
Output is correct |
47 |
Correct |
6 ms |
2168 KB |
Output is correct |
48 |
Correct |
23 ms |
2680 KB |
Output is correct |
49 |
Correct |
23 ms |
2808 KB |
Output is correct |
50 |
Correct |
43 ms |
2680 KB |
Output is correct |
51 |
Correct |
40 ms |
2680 KB |
Output is correct |
52 |
Correct |
110 ms |
3840 KB |
Output is correct |
53 |
Correct |
14 ms |
3440 KB |
Output is correct |
54 |
Correct |
123 ms |
3832 KB |
Output is correct |
55 |
Correct |
14 ms |
3320 KB |
Output is correct |
56 |
Correct |
106 ms |
3832 KB |
Output is correct |
57 |
Correct |
14 ms |
3448 KB |
Output is correct |
58 |
Correct |
108 ms |
3832 KB |
Output is correct |
59 |
Correct |
15 ms |
3448 KB |
Output is correct |
60 |
Correct |
113 ms |
3832 KB |
Output is correct |
61 |
Correct |
15 ms |
3448 KB |
Output is correct |
62 |
Correct |
109 ms |
3836 KB |
Output is correct |
63 |
Correct |
14 ms |
3448 KB |
Output is correct |
64 |
Correct |
104 ms |
3832 KB |
Output is correct |
65 |
Correct |
107 ms |
3832 KB |
Output is correct |
66 |
Correct |
15 ms |
3320 KB |
Output is correct |
67 |
Correct |
110 ms |
3832 KB |
Output is correct |
68 |
Correct |
14 ms |
3452 KB |
Output is correct |
69 |
Correct |
107 ms |
3832 KB |
Output is correct |
70 |
Correct |
14 ms |
3320 KB |
Output is correct |
71 |
Correct |
107 ms |
3960 KB |
Output is correct |
72 |
Correct |
15 ms |
3448 KB |
Output is correct |
73 |
Correct |
62 ms |
4092 KB |
Output is correct |
74 |
Correct |
536 ms |
6520 KB |
Output is correct |
75 |
Correct |
81 ms |
6584 KB |
Output is correct |
76 |
Correct |
536 ms |
6624 KB |
Output is correct |
77 |
Correct |
78 ms |
6648 KB |
Output is correct |
78 |
Correct |
532 ms |
6648 KB |
Output is correct |
79 |
Correct |
543 ms |
6520 KB |
Output is correct |
80 |
Correct |
79 ms |
6520 KB |
Output is correct |
81 |
Correct |
79 ms |
6548 KB |
Output is correct |
82 |
Correct |
528 ms |
6236 KB |
Output is correct |
83 |
Correct |
553 ms |
6392 KB |
Output is correct |
84 |
Correct |
74 ms |
6392 KB |
Output is correct |
85 |
Correct |
539 ms |
6416 KB |
Output is correct |
86 |
Correct |
77 ms |
6392 KB |
Output is correct |
87 |
Correct |
545 ms |
6520 KB |
Output is correct |
88 |
Correct |
78 ms |
6392 KB |
Output is correct |
89 |
Correct |
533 ms |
6392 KB |
Output is correct |
90 |
Correct |
72 ms |
6392 KB |
Output is correct |
91 |
Correct |
566 ms |
6392 KB |
Output is correct |
92 |
Correct |
83 ms |
6392 KB |
Output is correct |
93 |
Correct |
543 ms |
6392 KB |
Output is correct |
94 |
Correct |
40 ms |
5880 KB |
Output is correct |
95 |
Correct |
297 ms |
7160 KB |
Output is correct |
96 |
Correct |
311 ms |
7032 KB |
Output is correct |
97 |
Correct |
547 ms |
6392 KB |
Output is correct |
98 |
Correct |
556 ms |
6392 KB |
Output is correct |