#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n";
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 60005
const int modulo = 1e9 + 7;
set<int> adj[N];
int l[N], r[N], w[N], done[N];
int32_t main()
{
fastio();
int n, k;
cin>>n>>k;
int all = 0;
for (int i = 1; i <= 2 * n; i++)
{
cin>>l[i]>>r[i]>>w[i];
adj[l[i]].insert(i);
adj[r[i] + n].insert(i);
}
int sum = 0;
queue<int> q;
for (int i = 1; i <= 2 * n; i++)
if (adj[i].size() == 1) q.push(i);
int flag = 0;
while(!q.empty())
{
int top = q.front();
q.pop();
done[top] = 1;
if (adj[top].empty())
{
flag = 1;
break;
}
if (top <= n) sum += w[*adj[top].begin()];
else sum -= w[*adj[top].begin()];
for (auto i : adj[top])
{
int to = l[i];
if (to == top) to = r[i] + n;
adj[to].erase(i);
if (adj[to].size() == 1) q.push(to);
}
}
if (flag)
{
cout<<"NO\n";
return 0;
}
vector<int> v;
for (int i = 1; i <= 2 * n; i++)
{
if (done[i]) continue;
if (adj[i].empty())
{
flag = 1;
break;
}
int curr = i, steps = 0, tot = 0;
while(done[curr] == 0 && !adj[curr].empty())
{
done[curr] = 1;
if (adj[curr].size() > 2)
{
flag = 1;
break;
}
int ind = *adj[curr].begin();
if (steps % 2) tot -= w[ind];
else tot += w[ind];
steps++;
int to = l[ind];
if (to == curr) to = r[ind] + n;
adj[to].erase(ind);
adj[curr].erase(ind);
curr = to;
}
v.pb(abs(tot));
all += abs(tot);
}
if (flag)
{
cout<<"NO\n";
return 0;
}
v.pb(0);
int dp[v.size() + 5][all + 5];
memset(dp, 0, sizeof(dp));
sort(v.rbegin(), v.rend());
dp[0][0] = 1;
for (int i = 0; i < (int)v.size(); i++)
{
for (int j = 0; j <= all - v[i]; j++)
{
dp[i + 1][j + v[i]] |= dp[i][j];
dp[i + 1][abs(j - v[i])] |= dp[i][j];
}
}
sum = abs(sum);
for (int i = 0; i <= all; i++)
if (dp[v.size()][i] && min(abs(sum + i), abs(sum - i)) <= k) flag = 1;
if (flag) cout<<"YES\n";
else cout<<"NO\n";
cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " seconds\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3148 KB |
Output is correct |
3 |
Correct |
2 ms |
3156 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Correct |
2 ms |
3144 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
2 ms |
3156 KB |
Output is correct |
8 |
Correct |
2 ms |
3156 KB |
Output is correct |
9 |
Correct |
2 ms |
3156 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
2 ms |
3156 KB |
Output is correct |
12 |
Correct |
2 ms |
3112 KB |
Output is correct |
13 |
Correct |
2 ms |
3156 KB |
Output is correct |
14 |
Correct |
2 ms |
3144 KB |
Output is correct |
15 |
Correct |
2 ms |
3156 KB |
Output is correct |
16 |
Correct |
2 ms |
3156 KB |
Output is correct |
17 |
Correct |
2 ms |
3156 KB |
Output is correct |
18 |
Correct |
2 ms |
3144 KB |
Output is correct |
19 |
Correct |
2 ms |
3156 KB |
Output is correct |
20 |
Correct |
2 ms |
3156 KB |
Output is correct |
21 |
Correct |
2 ms |
3156 KB |
Output is correct |
22 |
Correct |
2 ms |
3148 KB |
Output is correct |
23 |
Correct |
2 ms |
3156 KB |
Output is correct |
24 |
Correct |
2 ms |
3156 KB |
Output is correct |
25 |
Correct |
2 ms |
3156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3148 KB |
Output is correct |
3 |
Correct |
2 ms |
3156 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Correct |
2 ms |
3144 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
2 ms |
3156 KB |
Output is correct |
8 |
Correct |
2 ms |
3156 KB |
Output is correct |
9 |
Correct |
2 ms |
3156 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
2 ms |
3156 KB |
Output is correct |
12 |
Correct |
2 ms |
3112 KB |
Output is correct |
13 |
Correct |
2 ms |
3156 KB |
Output is correct |
14 |
Correct |
2 ms |
3144 KB |
Output is correct |
15 |
Correct |
2 ms |
3156 KB |
Output is correct |
16 |
Correct |
2 ms |
3156 KB |
Output is correct |
17 |
Correct |
2 ms |
3156 KB |
Output is correct |
18 |
Correct |
2 ms |
3144 KB |
Output is correct |
19 |
Correct |
2 ms |
3156 KB |
Output is correct |
20 |
Correct |
2 ms |
3156 KB |
Output is correct |
21 |
Correct |
2 ms |
3156 KB |
Output is correct |
22 |
Correct |
2 ms |
3148 KB |
Output is correct |
23 |
Correct |
2 ms |
3156 KB |
Output is correct |
24 |
Correct |
2 ms |
3156 KB |
Output is correct |
25 |
Correct |
2 ms |
3156 KB |
Output is correct |
26 |
Correct |
102 ms |
108964 KB |
Output is correct |
27 |
Correct |
42 ms |
41552 KB |
Output is correct |
28 |
Correct |
88 ms |
107852 KB |
Output is correct |
29 |
Correct |
36 ms |
41556 KB |
Output is correct |
30 |
Correct |
88 ms |
109288 KB |
Output is correct |
31 |
Correct |
36 ms |
41556 KB |
Output is correct |
32 |
Correct |
105 ms |
108556 KB |
Output is correct |
33 |
Correct |
41 ms |
41664 KB |
Output is correct |
34 |
Correct |
25 ms |
20172 KB |
Output is correct |
35 |
Correct |
43 ms |
41656 KB |
Output is correct |
36 |
Correct |
99 ms |
108936 KB |
Output is correct |
37 |
Correct |
38 ms |
41684 KB |
Output is correct |
38 |
Correct |
126 ms |
110168 KB |
Output is correct |
39 |
Correct |
39 ms |
41560 KB |
Output is correct |
40 |
Correct |
91 ms |
107156 KB |
Output is correct |
41 |
Correct |
35 ms |
41724 KB |
Output is correct |
42 |
Correct |
103 ms |
109052 KB |
Output is correct |
43 |
Correct |
38 ms |
41744 KB |
Output is correct |
44 |
Correct |
95 ms |
109216 KB |
Output is correct |
45 |
Correct |
36 ms |
41612 KB |
Output is correct |
46 |
Correct |
90 ms |
109512 KB |
Output is correct |
47 |
Correct |
4 ms |
3540 KB |
Output is correct |
48 |
Correct |
31 ms |
30192 KB |
Output is correct |
49 |
Correct |
32 ms |
30160 KB |
Output is correct |
50 |
Runtime error |
100 ms |
262144 KB |
Execution killed with signal 9 |
51 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
5844 KB |
Output is correct |
2 |
Correct |
11 ms |
5512 KB |
Output is correct |
3 |
Correct |
18 ms |
5768 KB |
Output is correct |
4 |
Correct |
12 ms |
5532 KB |
Output is correct |
5 |
Correct |
19 ms |
5876 KB |
Output is correct |
6 |
Correct |
10 ms |
5496 KB |
Output is correct |
7 |
Correct |
10 ms |
5884 KB |
Output is correct |
8 |
Correct |
10 ms |
5460 KB |
Output is correct |
9 |
Correct |
13 ms |
5836 KB |
Output is correct |
10 |
Correct |
11 ms |
5460 KB |
Output is correct |
11 |
Correct |
12 ms |
5780 KB |
Output is correct |
12 |
Correct |
11 ms |
5468 KB |
Output is correct |
13 |
Correct |
11 ms |
5844 KB |
Output is correct |
14 |
Correct |
11 ms |
5808 KB |
Output is correct |
15 |
Correct |
11 ms |
5460 KB |
Output is correct |
16 |
Correct |
17 ms |
5768 KB |
Output is correct |
17 |
Correct |
16 ms |
5452 KB |
Output is correct |
18 |
Correct |
11 ms |
5844 KB |
Output is correct |
19 |
Correct |
10 ms |
5524 KB |
Output is correct |
20 |
Correct |
11 ms |
5844 KB |
Output is correct |
21 |
Correct |
10 ms |
5576 KB |
Output is correct |
22 |
Correct |
13 ms |
5716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3148 KB |
Output is correct |
3 |
Correct |
2 ms |
3156 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Correct |
2 ms |
3144 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
2 ms |
3156 KB |
Output is correct |
8 |
Correct |
2 ms |
3156 KB |
Output is correct |
9 |
Correct |
2 ms |
3156 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
2 ms |
3156 KB |
Output is correct |
12 |
Correct |
2 ms |
3112 KB |
Output is correct |
13 |
Correct |
2 ms |
3156 KB |
Output is correct |
14 |
Correct |
2 ms |
3144 KB |
Output is correct |
15 |
Correct |
2 ms |
3156 KB |
Output is correct |
16 |
Correct |
2 ms |
3156 KB |
Output is correct |
17 |
Correct |
2 ms |
3156 KB |
Output is correct |
18 |
Correct |
2 ms |
3144 KB |
Output is correct |
19 |
Correct |
2 ms |
3156 KB |
Output is correct |
20 |
Correct |
2 ms |
3156 KB |
Output is correct |
21 |
Correct |
2 ms |
3156 KB |
Output is correct |
22 |
Correct |
2 ms |
3148 KB |
Output is correct |
23 |
Correct |
2 ms |
3156 KB |
Output is correct |
24 |
Correct |
2 ms |
3156 KB |
Output is correct |
25 |
Correct |
2 ms |
3156 KB |
Output is correct |
26 |
Correct |
102 ms |
108964 KB |
Output is correct |
27 |
Correct |
42 ms |
41552 KB |
Output is correct |
28 |
Correct |
88 ms |
107852 KB |
Output is correct |
29 |
Correct |
36 ms |
41556 KB |
Output is correct |
30 |
Correct |
88 ms |
109288 KB |
Output is correct |
31 |
Correct |
36 ms |
41556 KB |
Output is correct |
32 |
Correct |
105 ms |
108556 KB |
Output is correct |
33 |
Correct |
41 ms |
41664 KB |
Output is correct |
34 |
Correct |
25 ms |
20172 KB |
Output is correct |
35 |
Correct |
43 ms |
41656 KB |
Output is correct |
36 |
Correct |
99 ms |
108936 KB |
Output is correct |
37 |
Correct |
38 ms |
41684 KB |
Output is correct |
38 |
Correct |
126 ms |
110168 KB |
Output is correct |
39 |
Correct |
39 ms |
41560 KB |
Output is correct |
40 |
Correct |
91 ms |
107156 KB |
Output is correct |
41 |
Correct |
35 ms |
41724 KB |
Output is correct |
42 |
Correct |
103 ms |
109052 KB |
Output is correct |
43 |
Correct |
38 ms |
41744 KB |
Output is correct |
44 |
Correct |
95 ms |
109216 KB |
Output is correct |
45 |
Correct |
36 ms |
41612 KB |
Output is correct |
46 |
Correct |
90 ms |
109512 KB |
Output is correct |
47 |
Correct |
4 ms |
3540 KB |
Output is correct |
48 |
Correct |
31 ms |
30192 KB |
Output is correct |
49 |
Correct |
32 ms |
30160 KB |
Output is correct |
50 |
Runtime error |
100 ms |
262144 KB |
Execution killed with signal 9 |
51 |
Halted |
0 ms |
0 KB |
- |