#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;
}
if(type == 2 || type == 1)
par[v] = u;
bfs(v, u, type);
}
if(type == 3) {
if(incycle[u] == 0 && u <= 2 * n) {
int lmao1 = 0, v;
for(int i = 0; i < graph[u].size(); i++) {
if(par[graph[u][i]] == u) {
v = graph[u][i];
break;
}
}
if(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);
//cout << i << " " << 2 * n + l[i] << "\n";
//cout << i << " " << 3 * n + r[i] << "\n";
}
for(int i = 1; i <= 2 * n; i++) {
if(!vis[i]) {
bfs(i, i, 2);
if(num != 0)
bye();
}
}
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= 2 * n; i++) {
if(!vis[i])
bfs(i, i, 1);
}
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= 2 * n; i++) {
if(!vis[i] && incycle[i] == 1)
bfs(i, i, 2);
}
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= 2 * n; i++) {
if(!vis[i]) {
bfs(i, i, 3);
}
}
//cout << sum << "\n";
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] <= 3 * n)
sum1 += s[temp[j]];
}
for(int j = 0; j < m; j++) {
if(temp[j] <= 2 * n && temp[(j - 1 + m) % m] <= 3 * n)
sum2 += s[temp[j]];
}
sum += min(sum1, sum2);
int y = min(sum1, sum2);
sum1 -= y; sum2 -= y;
if(max(sum1, sum2) == 0) continue;
if(used[max(sum1, sum2)] == 0)
can.pb(max(sum1, sum2));
used[max(sum1, sum2)]++;
}
// << sum << "\n";
sort(can.begin(), can.end());
int lol = can.size();
for(int i = 0; i <= 1; i++) {
for(int j = 0; j < 5 * N; j++)
dp[i][j] = 20 * N;
}
dp[0][0] = 0;
for(int i = 0; i < lol; i++) {
for(int j = 0; j < 5 * 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 < 5 * N; j++) {
dp[0][j] = dp[1][j];
dp[1][j] = 20 * N;
}
}
//cout << "wtf\n";
for(int i = 0; i < 5 * 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:63:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < graph[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~
tug.cpp:62:17: warning: unused variable 'lmao1' [-Wunused-variable]
int lmao1 = 0, v;
^~~~~
tug.cpp: In function 'int main()':
tug.cpp:113:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < vec.size(); i++) {
~~^~~~~~~~~~~~
tug.cpp: In function 'void bfs(int, int, int)':
tug.cpp:69:13: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(v <= 3 * n)
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5624 KB |
Output is correct |
2 |
Correct |
14 ms |
5748 KB |
Output is correct |
3 |
Correct |
18 ms |
5824 KB |
Output is correct |
4 |
Correct |
15 ms |
5900 KB |
Output is correct |
5 |
Correct |
14 ms |
5900 KB |
Output is correct |
6 |
Correct |
11 ms |
5900 KB |
Output is correct |
7 |
Correct |
15 ms |
5992 KB |
Output is correct |
8 |
Correct |
21 ms |
6008 KB |
Output is correct |
9 |
Correct |
14 ms |
6008 KB |
Output is correct |
10 |
Correct |
13 ms |
6008 KB |
Output is correct |
11 |
Correct |
15 ms |
6008 KB |
Output is correct |
12 |
Correct |
11 ms |
6008 KB |
Output is correct |
13 |
Correct |
14 ms |
6008 KB |
Output is correct |
14 |
Correct |
13 ms |
6008 KB |
Output is correct |
15 |
Correct |
14 ms |
6008 KB |
Output is correct |
16 |
Correct |
13 ms |
6008 KB |
Output is correct |
17 |
Correct |
13 ms |
6008 KB |
Output is correct |
18 |
Correct |
13 ms |
6008 KB |
Output is correct |
19 |
Correct |
14 ms |
6008 KB |
Output is correct |
20 |
Correct |
12 ms |
6008 KB |
Output is correct |
21 |
Correct |
5 ms |
6008 KB |
Output is correct |
22 |
Correct |
10 ms |
6008 KB |
Output is correct |
23 |
Correct |
11 ms |
6008 KB |
Output is correct |
24 |
Correct |
13 ms |
6008 KB |
Output is correct |
25 |
Correct |
20 ms |
6008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5624 KB |
Output is correct |
2 |
Correct |
14 ms |
5748 KB |
Output is correct |
3 |
Correct |
18 ms |
5824 KB |
Output is correct |
4 |
Correct |
15 ms |
5900 KB |
Output is correct |
5 |
Correct |
14 ms |
5900 KB |
Output is correct |
6 |
Correct |
11 ms |
5900 KB |
Output is correct |
7 |
Correct |
15 ms |
5992 KB |
Output is correct |
8 |
Correct |
21 ms |
6008 KB |
Output is correct |
9 |
Correct |
14 ms |
6008 KB |
Output is correct |
10 |
Correct |
13 ms |
6008 KB |
Output is correct |
11 |
Correct |
15 ms |
6008 KB |
Output is correct |
12 |
Correct |
11 ms |
6008 KB |
Output is correct |
13 |
Correct |
14 ms |
6008 KB |
Output is correct |
14 |
Correct |
13 ms |
6008 KB |
Output is correct |
15 |
Correct |
14 ms |
6008 KB |
Output is correct |
16 |
Correct |
13 ms |
6008 KB |
Output is correct |
17 |
Correct |
13 ms |
6008 KB |
Output is correct |
18 |
Correct |
13 ms |
6008 KB |
Output is correct |
19 |
Correct |
14 ms |
6008 KB |
Output is correct |
20 |
Correct |
12 ms |
6008 KB |
Output is correct |
21 |
Correct |
5 ms |
6008 KB |
Output is correct |
22 |
Correct |
10 ms |
6008 KB |
Output is correct |
23 |
Correct |
11 ms |
6008 KB |
Output is correct |
24 |
Correct |
13 ms |
6008 KB |
Output is correct |
25 |
Correct |
20 ms |
6008 KB |
Output is correct |
26 |
Correct |
30 ms |
6408 KB |
Output is correct |
27 |
Correct |
212 ms |
6576 KB |
Output is correct |
28 |
Correct |
26 ms |
6640 KB |
Output is correct |
29 |
Correct |
223 ms |
6776 KB |
Output is correct |
30 |
Correct |
25 ms |
6776 KB |
Output is correct |
31 |
Correct |
204 ms |
6888 KB |
Output is correct |
32 |
Correct |
24 ms |
6888 KB |
Output is correct |
33 |
Correct |
202 ms |
6984 KB |
Output is correct |
34 |
Correct |
21 ms |
6984 KB |
Output is correct |
35 |
Correct |
215 ms |
6984 KB |
Output is correct |
36 |
Correct |
24 ms |
7000 KB |
Output is correct |
37 |
Correct |
201 ms |
7072 KB |
Output is correct |
38 |
Correct |
24 ms |
7096 KB |
Output is correct |
39 |
Correct |
201 ms |
7176 KB |
Output is correct |
40 |
Correct |
27 ms |
7348 KB |
Output is correct |
41 |
Correct |
202 ms |
7348 KB |
Output is correct |
42 |
Correct |
25 ms |
7348 KB |
Output is correct |
43 |
Correct |
204 ms |
7492 KB |
Output is correct |
44 |
Correct |
26 ms |
7492 KB |
Output is correct |
45 |
Correct |
208 ms |
7592 KB |
Output is correct |
46 |
Correct |
24 ms |
7592 KB |
Output is correct |
47 |
Correct |
7 ms |
7592 KB |
Output is correct |
48 |
Correct |
24 ms |
7608 KB |
Output is correct |
49 |
Correct |
27 ms |
7656 KB |
Output is correct |
50 |
Correct |
27 ms |
7832 KB |
Output is correct |
51 |
Correct |
26 ms |
7832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
9652 KB |
Output is correct |
2 |
Correct |
16 ms |
9652 KB |
Output is correct |
3 |
Correct |
31 ms |
9652 KB |
Output is correct |
4 |
Correct |
18 ms |
9652 KB |
Output is correct |
5 |
Correct |
32 ms |
9652 KB |
Output is correct |
6 |
Correct |
18 ms |
9652 KB |
Output is correct |
7 |
Correct |
29 ms |
9652 KB |
Output is correct |
8 |
Correct |
18 ms |
9652 KB |
Output is correct |
9 |
Correct |
36 ms |
9780 KB |
Output is correct |
10 |
Correct |
20 ms |
9780 KB |
Output is correct |
11 |
Correct |
28 ms |
9780 KB |
Output is correct |
12 |
Correct |
19 ms |
9780 KB |
Output is correct |
13 |
Correct |
29 ms |
9780 KB |
Output is correct |
14 |
Correct |
38 ms |
9780 KB |
Output is correct |
15 |
Correct |
22 ms |
9780 KB |
Output is correct |
16 |
Correct |
36 ms |
9780 KB |
Output is correct |
17 |
Correct |
17 ms |
9780 KB |
Output is correct |
18 |
Correct |
33 ms |
9780 KB |
Output is correct |
19 |
Correct |
19 ms |
9780 KB |
Output is correct |
20 |
Correct |
37 ms |
9780 KB |
Output is correct |
21 |
Correct |
31 ms |
9780 KB |
Output is correct |
22 |
Correct |
38 ms |
9780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5624 KB |
Output is correct |
2 |
Correct |
14 ms |
5748 KB |
Output is correct |
3 |
Correct |
18 ms |
5824 KB |
Output is correct |
4 |
Correct |
15 ms |
5900 KB |
Output is correct |
5 |
Correct |
14 ms |
5900 KB |
Output is correct |
6 |
Correct |
11 ms |
5900 KB |
Output is correct |
7 |
Correct |
15 ms |
5992 KB |
Output is correct |
8 |
Correct |
21 ms |
6008 KB |
Output is correct |
9 |
Correct |
14 ms |
6008 KB |
Output is correct |
10 |
Correct |
13 ms |
6008 KB |
Output is correct |
11 |
Correct |
15 ms |
6008 KB |
Output is correct |
12 |
Correct |
11 ms |
6008 KB |
Output is correct |
13 |
Correct |
14 ms |
6008 KB |
Output is correct |
14 |
Correct |
13 ms |
6008 KB |
Output is correct |
15 |
Correct |
14 ms |
6008 KB |
Output is correct |
16 |
Correct |
13 ms |
6008 KB |
Output is correct |
17 |
Correct |
13 ms |
6008 KB |
Output is correct |
18 |
Correct |
13 ms |
6008 KB |
Output is correct |
19 |
Correct |
14 ms |
6008 KB |
Output is correct |
20 |
Correct |
12 ms |
6008 KB |
Output is correct |
21 |
Correct |
5 ms |
6008 KB |
Output is correct |
22 |
Correct |
10 ms |
6008 KB |
Output is correct |
23 |
Correct |
11 ms |
6008 KB |
Output is correct |
24 |
Correct |
13 ms |
6008 KB |
Output is correct |
25 |
Correct |
20 ms |
6008 KB |
Output is correct |
26 |
Correct |
30 ms |
6408 KB |
Output is correct |
27 |
Correct |
212 ms |
6576 KB |
Output is correct |
28 |
Correct |
26 ms |
6640 KB |
Output is correct |
29 |
Correct |
223 ms |
6776 KB |
Output is correct |
30 |
Correct |
25 ms |
6776 KB |
Output is correct |
31 |
Correct |
204 ms |
6888 KB |
Output is correct |
32 |
Correct |
24 ms |
6888 KB |
Output is correct |
33 |
Correct |
202 ms |
6984 KB |
Output is correct |
34 |
Correct |
21 ms |
6984 KB |
Output is correct |
35 |
Correct |
215 ms |
6984 KB |
Output is correct |
36 |
Correct |
24 ms |
7000 KB |
Output is correct |
37 |
Correct |
201 ms |
7072 KB |
Output is correct |
38 |
Correct |
24 ms |
7096 KB |
Output is correct |
39 |
Correct |
201 ms |
7176 KB |
Output is correct |
40 |
Correct |
27 ms |
7348 KB |
Output is correct |
41 |
Correct |
202 ms |
7348 KB |
Output is correct |
42 |
Correct |
25 ms |
7348 KB |
Output is correct |
43 |
Correct |
204 ms |
7492 KB |
Output is correct |
44 |
Correct |
26 ms |
7492 KB |
Output is correct |
45 |
Correct |
208 ms |
7592 KB |
Output is correct |
46 |
Correct |
24 ms |
7592 KB |
Output is correct |
47 |
Correct |
7 ms |
7592 KB |
Output is correct |
48 |
Correct |
24 ms |
7608 KB |
Output is correct |
49 |
Correct |
27 ms |
7656 KB |
Output is correct |
50 |
Correct |
27 ms |
7832 KB |
Output is correct |
51 |
Correct |
26 ms |
7832 KB |
Output is correct |
52 |
Correct |
29 ms |
9652 KB |
Output is correct |
53 |
Correct |
16 ms |
9652 KB |
Output is correct |
54 |
Correct |
31 ms |
9652 KB |
Output is correct |
55 |
Correct |
18 ms |
9652 KB |
Output is correct |
56 |
Correct |
32 ms |
9652 KB |
Output is correct |
57 |
Correct |
18 ms |
9652 KB |
Output is correct |
58 |
Correct |
29 ms |
9652 KB |
Output is correct |
59 |
Correct |
18 ms |
9652 KB |
Output is correct |
60 |
Correct |
36 ms |
9780 KB |
Output is correct |
61 |
Correct |
20 ms |
9780 KB |
Output is correct |
62 |
Correct |
28 ms |
9780 KB |
Output is correct |
63 |
Correct |
19 ms |
9780 KB |
Output is correct |
64 |
Correct |
29 ms |
9780 KB |
Output is correct |
65 |
Correct |
38 ms |
9780 KB |
Output is correct |
66 |
Correct |
22 ms |
9780 KB |
Output is correct |
67 |
Correct |
36 ms |
9780 KB |
Output is correct |
68 |
Correct |
17 ms |
9780 KB |
Output is correct |
69 |
Correct |
33 ms |
9780 KB |
Output is correct |
70 |
Correct |
19 ms |
9780 KB |
Output is correct |
71 |
Correct |
37 ms |
9780 KB |
Output is correct |
72 |
Correct |
31 ms |
9780 KB |
Output is correct |
73 |
Correct |
38 ms |
9780 KB |
Output is correct |
74 |
Correct |
134 ms |
15152 KB |
Output is correct |
75 |
Correct |
1228 ms |
15152 KB |
Output is correct |
76 |
Correct |
135 ms |
16784 KB |
Output is correct |
77 |
Correct |
1168 ms |
16784 KB |
Output is correct |
78 |
Correct |
168 ms |
18432 KB |
Output is correct |
79 |
Correct |
133 ms |
19232 KB |
Output is correct |
80 |
Correct |
1112 ms |
19232 KB |
Output is correct |
81 |
Correct |
103 ms |
19884 KB |
Output is correct |
82 |
Correct |
64 ms |
21656 KB |
Output is correct |
83 |
Correct |
121 ms |
22468 KB |
Output is correct |
84 |
Correct |
1063 ms |
22468 KB |
Output is correct |
85 |
Correct |
132 ms |
24060 KB |
Output is correct |
86 |
Correct |
1088 ms |
24060 KB |
Output is correct |
87 |
Correct |
112 ms |
25832 KB |
Output is correct |
88 |
Correct |
1078 ms |
25832 KB |
Output is correct |
89 |
Correct |
98 ms |
27360 KB |
Output is correct |
90 |
Correct |
1058 ms |
27360 KB |
Output is correct |
91 |
Correct |
112 ms |
29112 KB |
Output is correct |
92 |
Correct |
1093 ms |
29112 KB |
Output is correct |
93 |
Correct |
97 ms |
30604 KB |
Output is correct |
94 |
Correct |
56 ms |
30604 KB |
Output is correct |
95 |
Correct |
98 ms |
31412 KB |
Output is correct |
96 |
Correct |
94 ms |
32268 KB |
Output is correct |
97 |
Correct |
111 ms |
33844 KB |
Output is correct |
98 |
Correct |
101 ms |
34772 KB |
Output is correct |