#include <bits/stdc++.h>
using namespace std;
const int MAXN = 30005;
int n, k;
int l[2 * MAXN], r[2 * MAXN], s[2 * MAXN];
int pa, sum;
set <int> Adj[2 * MAXN];
set <int> D[2 * MAXN];
bool Used[2 * MAXN];
bitset <20 * MAXN> dp;
int c[2];
vector <int> N[2 * MAXN];
bool filled[2 * MAXN];
void DFS(int node, int color, int p = -1)
{
c[color] += s[node];
Used[node] = true;
for(auto x : N[node])
{
if(!Used[x])
{
DFS(x, 1 - color, node);
}
}
}
int main()
{
// freopen("Input.txt", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
priority_queue <pair <int, int> > pq;
cin >> n >> k;
for(int i = 1; i <= 2 * n; i++)
{
cin >> l[i] >> r[i] >> s[i];
sum += s[i];
Adj[l[i]].emplace(i);
Adj[r[i] + n].emplace(i);
D[l[i]].insert(i);
D[r[i] + n].insert(i);
}
for(int i = 1; i <= 2 * n; i++)
{
pq.emplace(-(int)D[i].size(), i);
}
while(pq.empty() == false)
{
int node = pq.top().second;
if(pq.top().first < -1)
{
break;
}
// cout << node << endl;
pq.pop();
if(D[node].empty())
{
cout << "NO\n";
return 0;
}
int tt = *D[node].begin();
if(node <= n)
{
pa += s[tt];
}
Used[tt] = true;
D[l[tt]].erase(tt);
D[r[tt] + n].erase(tt);
Adj[l[tt]].erase(tt);
Adj[r[tt] + n].erase(tt);
filled[node] = true;
if(!filled[l[tt]])
{
pq.emplace(-(int)D[l[tt]].size(), l[tt]);
}
if(!filled[r[tt] + n])
{
pq.emplace(-(int)D[r[tt] + n].size(), r[tt] + n);
}
}
for(int i = 1; i <= 2 * n; i++)
{
if(Adj[i].size() == 2)
{
int t1 = *Adj[i].begin();
Adj[i].erase(Adj[i].begin());
int t2 = *Adj[i].begin();
Adj[i].erase(t2);
N[t1].emplace_back(t2);
N[t2].emplace_back(t1);
}
}
dp[0] = 1;
for(int i = 1; i <= 2 * n; i++)
{
if(!Used[i])
{
c[0] = 0;
c[1] = 0;
DFS(i, 0);
int tmp = min(c[0], c[1]);
// cout << c[0] << ' ' << c[1] << endl;
pa += tmp;
dp = dp | (dp << (max(c[0], c[1]) - min(c[0], c[1])));
}
}
for(int i = 0; i <= sum; i++)
{
if(dp[i])
{
int t1 = i + pa;
int t2 = sum - t1;
if(abs(t1 - t2) <= k)
{
cout << "YES\n";
return 0;
}
}
}
cout << "NO\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7500 KB |
Output is correct |
2 |
Correct |
4 ms |
7516 KB |
Output is correct |
3 |
Correct |
6 ms |
7520 KB |
Output is correct |
4 |
Correct |
6 ms |
7516 KB |
Output is correct |
5 |
Correct |
6 ms |
7500 KB |
Output is correct |
6 |
Correct |
4 ms |
7500 KB |
Output is correct |
7 |
Correct |
4 ms |
7500 KB |
Output is correct |
8 |
Correct |
4 ms |
7500 KB |
Output is correct |
9 |
Correct |
4 ms |
7516 KB |
Output is correct |
10 |
Correct |
4 ms |
7500 KB |
Output is correct |
11 |
Correct |
4 ms |
7500 KB |
Output is correct |
12 |
Correct |
4 ms |
7520 KB |
Output is correct |
13 |
Correct |
4 ms |
7520 KB |
Output is correct |
14 |
Correct |
4 ms |
7560 KB |
Output is correct |
15 |
Correct |
4 ms |
7500 KB |
Output is correct |
16 |
Correct |
4 ms |
7576 KB |
Output is correct |
17 |
Correct |
4 ms |
7520 KB |
Output is correct |
18 |
Correct |
4 ms |
7500 KB |
Output is correct |
19 |
Correct |
4 ms |
7536 KB |
Output is correct |
20 |
Correct |
4 ms |
7500 KB |
Output is correct |
21 |
Correct |
4 ms |
7500 KB |
Output is correct |
22 |
Correct |
4 ms |
7500 KB |
Output is correct |
23 |
Correct |
4 ms |
7516 KB |
Output is correct |
24 |
Correct |
6 ms |
7520 KB |
Output is correct |
25 |
Correct |
6 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7500 KB |
Output is correct |
2 |
Correct |
4 ms |
7516 KB |
Output is correct |
3 |
Correct |
6 ms |
7520 KB |
Output is correct |
4 |
Correct |
6 ms |
7516 KB |
Output is correct |
5 |
Correct |
6 ms |
7500 KB |
Output is correct |
6 |
Correct |
4 ms |
7500 KB |
Output is correct |
7 |
Correct |
4 ms |
7500 KB |
Output is correct |
8 |
Correct |
4 ms |
7500 KB |
Output is correct |
9 |
Correct |
4 ms |
7516 KB |
Output is correct |
10 |
Correct |
4 ms |
7500 KB |
Output is correct |
11 |
Correct |
4 ms |
7500 KB |
Output is correct |
12 |
Correct |
4 ms |
7520 KB |
Output is correct |
13 |
Correct |
4 ms |
7520 KB |
Output is correct |
14 |
Correct |
4 ms |
7560 KB |
Output is correct |
15 |
Correct |
4 ms |
7500 KB |
Output is correct |
16 |
Correct |
4 ms |
7576 KB |
Output is correct |
17 |
Correct |
4 ms |
7520 KB |
Output is correct |
18 |
Correct |
4 ms |
7500 KB |
Output is correct |
19 |
Correct |
4 ms |
7536 KB |
Output is correct |
20 |
Correct |
4 ms |
7500 KB |
Output is correct |
21 |
Correct |
4 ms |
7500 KB |
Output is correct |
22 |
Correct |
4 ms |
7500 KB |
Output is correct |
23 |
Correct |
4 ms |
7516 KB |
Output is correct |
24 |
Correct |
6 ms |
7520 KB |
Output is correct |
25 |
Correct |
6 ms |
7516 KB |
Output is correct |
26 |
Correct |
53 ms |
8568 KB |
Output is correct |
27 |
Correct |
14 ms |
8524 KB |
Output is correct |
28 |
Correct |
62 ms |
8556 KB |
Output is correct |
29 |
Correct |
15 ms |
8592 KB |
Output is correct |
30 |
Correct |
75 ms |
8568 KB |
Output is correct |
31 |
Correct |
15 ms |
8592 KB |
Output is correct |
32 |
Correct |
56 ms |
8524 KB |
Output is correct |
33 |
Correct |
13 ms |
8492 KB |
Output is correct |
34 |
Correct |
10 ms |
8552 KB |
Output is correct |
35 |
Correct |
14 ms |
8556 KB |
Output is correct |
36 |
Correct |
54 ms |
8524 KB |
Output is correct |
37 |
Correct |
13 ms |
8524 KB |
Output is correct |
38 |
Correct |
59 ms |
8644 KB |
Output is correct |
39 |
Correct |
16 ms |
8592 KB |
Output is correct |
40 |
Correct |
57 ms |
8508 KB |
Output is correct |
41 |
Correct |
13 ms |
8524 KB |
Output is correct |
42 |
Correct |
56 ms |
8524 KB |
Output is correct |
43 |
Correct |
13 ms |
8524 KB |
Output is correct |
44 |
Correct |
62 ms |
8524 KB |
Output is correct |
45 |
Correct |
15 ms |
8564 KB |
Output is correct |
46 |
Correct |
56 ms |
8568 KB |
Output is correct |
47 |
Correct |
6 ms |
8396 KB |
Output is correct |
48 |
Correct |
31 ms |
8524 KB |
Output is correct |
49 |
Correct |
31 ms |
8548 KB |
Output is correct |
50 |
Correct |
53 ms |
8524 KB |
Output is correct |
51 |
Correct |
55 ms |
8524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
12108 KB |
Output is correct |
2 |
Correct |
19 ms |
12108 KB |
Output is correct |
3 |
Correct |
216 ms |
12072 KB |
Output is correct |
4 |
Correct |
18 ms |
12140 KB |
Output is correct |
5 |
Correct |
167 ms |
12092 KB |
Output is correct |
6 |
Correct |
21 ms |
12108 KB |
Output is correct |
7 |
Correct |
182 ms |
12108 KB |
Output is correct |
8 |
Correct |
21 ms |
12072 KB |
Output is correct |
9 |
Correct |
169 ms |
12108 KB |
Output is correct |
10 |
Correct |
19 ms |
12144 KB |
Output is correct |
11 |
Correct |
195 ms |
12140 KB |
Output is correct |
12 |
Correct |
20 ms |
12040 KB |
Output is correct |
13 |
Correct |
170 ms |
12108 KB |
Output is correct |
14 |
Correct |
185 ms |
12108 KB |
Output is correct |
15 |
Correct |
18 ms |
12140 KB |
Output is correct |
16 |
Correct |
196 ms |
12136 KB |
Output is correct |
17 |
Correct |
21 ms |
12108 KB |
Output is correct |
18 |
Correct |
220 ms |
12116 KB |
Output is correct |
19 |
Correct |
19 ms |
12032 KB |
Output is correct |
20 |
Correct |
183 ms |
12156 KB |
Output is correct |
21 |
Correct |
19 ms |
12108 KB |
Output is correct |
22 |
Correct |
106 ms |
12120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7500 KB |
Output is correct |
2 |
Correct |
4 ms |
7516 KB |
Output is correct |
3 |
Correct |
6 ms |
7520 KB |
Output is correct |
4 |
Correct |
6 ms |
7516 KB |
Output is correct |
5 |
Correct |
6 ms |
7500 KB |
Output is correct |
6 |
Correct |
4 ms |
7500 KB |
Output is correct |
7 |
Correct |
4 ms |
7500 KB |
Output is correct |
8 |
Correct |
4 ms |
7500 KB |
Output is correct |
9 |
Correct |
4 ms |
7516 KB |
Output is correct |
10 |
Correct |
4 ms |
7500 KB |
Output is correct |
11 |
Correct |
4 ms |
7500 KB |
Output is correct |
12 |
Correct |
4 ms |
7520 KB |
Output is correct |
13 |
Correct |
4 ms |
7520 KB |
Output is correct |
14 |
Correct |
4 ms |
7560 KB |
Output is correct |
15 |
Correct |
4 ms |
7500 KB |
Output is correct |
16 |
Correct |
4 ms |
7576 KB |
Output is correct |
17 |
Correct |
4 ms |
7520 KB |
Output is correct |
18 |
Correct |
4 ms |
7500 KB |
Output is correct |
19 |
Correct |
4 ms |
7536 KB |
Output is correct |
20 |
Correct |
4 ms |
7500 KB |
Output is correct |
21 |
Correct |
4 ms |
7500 KB |
Output is correct |
22 |
Correct |
4 ms |
7500 KB |
Output is correct |
23 |
Correct |
4 ms |
7516 KB |
Output is correct |
24 |
Correct |
6 ms |
7520 KB |
Output is correct |
25 |
Correct |
6 ms |
7516 KB |
Output is correct |
26 |
Correct |
53 ms |
8568 KB |
Output is correct |
27 |
Correct |
14 ms |
8524 KB |
Output is correct |
28 |
Correct |
62 ms |
8556 KB |
Output is correct |
29 |
Correct |
15 ms |
8592 KB |
Output is correct |
30 |
Correct |
75 ms |
8568 KB |
Output is correct |
31 |
Correct |
15 ms |
8592 KB |
Output is correct |
32 |
Correct |
56 ms |
8524 KB |
Output is correct |
33 |
Correct |
13 ms |
8492 KB |
Output is correct |
34 |
Correct |
10 ms |
8552 KB |
Output is correct |
35 |
Correct |
14 ms |
8556 KB |
Output is correct |
36 |
Correct |
54 ms |
8524 KB |
Output is correct |
37 |
Correct |
13 ms |
8524 KB |
Output is correct |
38 |
Correct |
59 ms |
8644 KB |
Output is correct |
39 |
Correct |
16 ms |
8592 KB |
Output is correct |
40 |
Correct |
57 ms |
8508 KB |
Output is correct |
41 |
Correct |
13 ms |
8524 KB |
Output is correct |
42 |
Correct |
56 ms |
8524 KB |
Output is correct |
43 |
Correct |
13 ms |
8524 KB |
Output is correct |
44 |
Correct |
62 ms |
8524 KB |
Output is correct |
45 |
Correct |
15 ms |
8564 KB |
Output is correct |
46 |
Correct |
56 ms |
8568 KB |
Output is correct |
47 |
Correct |
6 ms |
8396 KB |
Output is correct |
48 |
Correct |
31 ms |
8524 KB |
Output is correct |
49 |
Correct |
31 ms |
8548 KB |
Output is correct |
50 |
Correct |
53 ms |
8524 KB |
Output is correct |
51 |
Correct |
55 ms |
8524 KB |
Output is correct |
52 |
Correct |
168 ms |
12108 KB |
Output is correct |
53 |
Correct |
19 ms |
12108 KB |
Output is correct |
54 |
Correct |
216 ms |
12072 KB |
Output is correct |
55 |
Correct |
18 ms |
12140 KB |
Output is correct |
56 |
Correct |
167 ms |
12092 KB |
Output is correct |
57 |
Correct |
21 ms |
12108 KB |
Output is correct |
58 |
Correct |
182 ms |
12108 KB |
Output is correct |
59 |
Correct |
21 ms |
12072 KB |
Output is correct |
60 |
Correct |
169 ms |
12108 KB |
Output is correct |
61 |
Correct |
19 ms |
12144 KB |
Output is correct |
62 |
Correct |
195 ms |
12140 KB |
Output is correct |
63 |
Correct |
20 ms |
12040 KB |
Output is correct |
64 |
Correct |
170 ms |
12108 KB |
Output is correct |
65 |
Correct |
185 ms |
12108 KB |
Output is correct |
66 |
Correct |
18 ms |
12140 KB |
Output is correct |
67 |
Correct |
196 ms |
12136 KB |
Output is correct |
68 |
Correct |
21 ms |
12108 KB |
Output is correct |
69 |
Correct |
220 ms |
12116 KB |
Output is correct |
70 |
Correct |
19 ms |
12032 KB |
Output is correct |
71 |
Correct |
183 ms |
12156 KB |
Output is correct |
72 |
Correct |
19 ms |
12108 KB |
Output is correct |
73 |
Correct |
106 ms |
12120 KB |
Output is correct |
74 |
Correct |
829 ms |
20960 KB |
Output is correct |
75 |
Correct |
144 ms |
21024 KB |
Output is correct |
76 |
Correct |
854 ms |
20896 KB |
Output is correct |
77 |
Correct |
135 ms |
21128 KB |
Output is correct |
78 |
Correct |
848 ms |
20936 KB |
Output is correct |
79 |
Correct |
862 ms |
20932 KB |
Output is correct |
80 |
Correct |
127 ms |
20928 KB |
Output is correct |
81 |
Correct |
142 ms |
21044 KB |
Output is correct |
82 |
Correct |
826 ms |
21108 KB |
Output is correct |
83 |
Correct |
850 ms |
20932 KB |
Output is correct |
84 |
Correct |
127 ms |
20932 KB |
Output is correct |
85 |
Correct |
831 ms |
20940 KB |
Output is correct |
86 |
Correct |
155 ms |
21016 KB |
Output is correct |
87 |
Correct |
816 ms |
21020 KB |
Output is correct |
88 |
Correct |
141 ms |
20968 KB |
Output is correct |
89 |
Correct |
967 ms |
21004 KB |
Output is correct |
90 |
Correct |
135 ms |
21020 KB |
Output is correct |
91 |
Correct |
808 ms |
21024 KB |
Output is correct |
92 |
Correct |
129 ms |
20928 KB |
Output is correct |
93 |
Correct |
865 ms |
20980 KB |
Output is correct |
94 |
Correct |
77 ms |
20904 KB |
Output is correct |
95 |
Correct |
481 ms |
20972 KB |
Output is correct |
96 |
Correct |
459 ms |
20980 KB |
Output is correct |
97 |
Correct |
844 ms |
20916 KB |
Output is correct |
98 |
Correct |
830 ms |
20976 KB |
Output is correct |