#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;
const int N = 60005;
void bye() {
cout << "NO";
exit(0);
}
int n, lmao;
int l[N], r[N], s[N], sum, incycle[2 * N], par[2 * N], used[20 * N], dp[2][20 * N], total, num;
vector<int> graph[2 * N];
bool vis[2 * N];
vector<int> can;
vector<vector<int>> vec;
void bfs(int u, int p, int type) {
vis[u] = 1;
if(u > 2 * n)
num++;
else
num--;
for(int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i];
if(v == p) continue;
if(vis[v]) {
if(type == 1) {
if(incycle[v]) continue;
vector<int> temp;
int z = u;
while(z != v) {
incycle[z] = 1;
temp.pb(z);
z = par[z];
}
incycle[v] = 1;
temp.pb(v);
vec.pb(temp);
}
continue;
}
par[v] = u;
bfs(v, u, type);
}
if(type == 2) {
if(incycle[u] == 0 && u <= 2 * n && graph[u].size() == 1) {
int v = graph[u][0];
if(v > 2 * n && v <= 3 * n)
sum += s[u];
}
}
}
int main() {
cin.tie(0), ios::sync_with_stdio(0);
//freopen("test.inp", "r", stdin);
cin >> n >> lmao;
for(int i = 1; i <= 2 * n; i++) {
cin >> l[i] >> r[i] >> s[i];
total += s[i];
graph[i].pb(2 * n + l[i]);
graph[2 * n + l[i]].pb(i);
graph[i].pb(3 * n + r[i]);
graph[3 * n + r[i]].pb(i);
}
for(int i = 1; i <= 2 * n; i++) {
if(!vis[i]) {
bfs(i, i, 1);
if(num != 0)
bye();
}
}
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= 2 * n; i++) {
if(!vis[i] && incycle[i])
bfs(i, i, 2);
}
for(int i = 0; i < vec.size(); i++) {
vector<int> temp = vec[i];
int sum1 = 0, sum2 = 0, m = temp.size();
for(int j = 0; j < m; j++) {
if(temp[j] <= 2 * n && temp[(j + 1) % m] >= 2 * n + 1 && temp[(j + 1) % m] <= 3 * n)
sum1 += s[temp[j]];
}
for(int j = 0; j < m; j++) {
if(temp[j] <= 2 * n && temp[(j - 1 + m) % m] >= 2 * n + 1 && temp[(j - 1 + m) % m] <= 3 * n)
sum2 += s[temp[j]];
}
sum += min(sum1, sum2);
sum1 -= min(sum1, sum2); sum2 -= min(sum1, sum2);
if(max(sum1, sum2) == 0) continue;
if(used[max(sum1, sum2)] == 0)
can.pb(max(sum1, sum2));
used[max(sum1, sum2)]++;
}
sort(can.begin(), can.end());
int lol = can.size();
for(int i = 0; i <= 1; i++) {
for(int j = 0; j < 20 * N; j++)
dp[i][j] = 20 * N;
}
int sum = 0;
dp[0][0] = 0;
for(int i = 0; i < lol; i++) {
for(int j = 0; j < 20 * N; j++) {
if(dp[0][j] > used[can[i]]) continue;
dp[1][j] = 0;
if(dp[0][j] < used[can[i]])
dp[0][j + can[i]] = min(dp[0][j + can[i]], dp[0][j] + 1);
}
for(int j = 0; j < 20 * N; j++) {
dp[0][j] = dp[1][j];
dp[1][j] = 20 * N;
}
}
for(int i = 0; i < 20 * N; i++) {
if(dp[0][i] < 20 * N) {
int temp = i + sum;
int lef = total - temp;
if(abs(lef - temp) <= lmao) {
cout << "YES";
exit(0);
}
}
}
bye();
}
Compilation message
tug.cpp: In function 'void bfs(int, int, int)':
tug.cpp:37:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < graph[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~
tug.cpp: In function 'int main()':
tug.cpp:92:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < vec.size(); i++) {
~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
12664 KB |
Output is correct |
2 |
Correct |
43 ms |
12796 KB |
Output is correct |
3 |
Correct |
34 ms |
12796 KB |
Output is correct |
4 |
Correct |
36 ms |
12796 KB |
Output is correct |
5 |
Correct |
31 ms |
12796 KB |
Output is correct |
6 |
Correct |
30 ms |
12820 KB |
Output is correct |
7 |
Correct |
40 ms |
12904 KB |
Output is correct |
8 |
Incorrect |
40 ms |
12920 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
12664 KB |
Output is correct |
2 |
Correct |
43 ms |
12796 KB |
Output is correct |
3 |
Correct |
34 ms |
12796 KB |
Output is correct |
4 |
Correct |
36 ms |
12796 KB |
Output is correct |
5 |
Correct |
31 ms |
12796 KB |
Output is correct |
6 |
Correct |
30 ms |
12820 KB |
Output is correct |
7 |
Correct |
40 ms |
12904 KB |
Output is correct |
8 |
Incorrect |
40 ms |
12920 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
15168 KB |
Output is correct |
2 |
Correct |
16 ms |
15168 KB |
Output is correct |
3 |
Correct |
37 ms |
15196 KB |
Output is correct |
4 |
Runtime error |
516 ms |
263168 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
12664 KB |
Output is correct |
2 |
Correct |
43 ms |
12796 KB |
Output is correct |
3 |
Correct |
34 ms |
12796 KB |
Output is correct |
4 |
Correct |
36 ms |
12796 KB |
Output is correct |
5 |
Correct |
31 ms |
12796 KB |
Output is correct |
6 |
Correct |
30 ms |
12820 KB |
Output is correct |
7 |
Correct |
40 ms |
12904 KB |
Output is correct |
8 |
Incorrect |
40 ms |
12920 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |