#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44
using namespace std;
const int MAXVAL = 300000;
const int MAXN = 30000;
vector < pair <int, int> > g[4 * MAXN + 1];
bool vis[4 * MAXN + 1];
queue <int> Q;
int n;
void dfs(int nod, int &cur) {
vis[nod] = 1;
for(auto it : g[nod]) {
if(vis[it.first] == 0) {
if(it.first > 2 * n) {
if(it.first <= 3 * n) {
cur += it.second;
}
else {
cur -= it.second;
}
}
dfs(it.first, cur);
}
}
}
int deg[4 * MAXN + 1];
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, k;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> k;
for(i = 1; i <= 2 * n; i++) {
int l, r, s;
cin >> l >> r >> s;
l += 2 * n;
r += 3 * n;
g[i].push_back({l, s});
g[l].push_back({i, s});
g[i].push_back({r, s});
g[r].push_back({i, s});
}
for(i = 2 * n + 1; i <= 4 * n; i++) {
if(g[i].size() == 1) {
Q.push(i);
vis[i] = 1;
}
}
int sl = 0, sr = 0;
while(Q.size()) {
int nod = Q.front();
Q.pop();
for(auto it : g[nod]) {
if(vis[it.first] == 0) {
if(it.first <= 2 * n) {
vis[it.first] = 1;
Q.push(it.first);
if(nod <= 3 * n) {
sl += it.second;
}
else {
sr += it.second;
}
for(auto itr : g[it.first]) {
deg[itr.first]++;
}
}
else if(deg[it.first] + 1 == g[it.first].size()) {
vis[it.first] = 1;
Q.push(it.first);
}
}
}
}
//cerr << sl << " " << sr << "\n";
bitset < MAXVAL + 1 > bs;
bs[0] = 1;
int tot = 0;
for(i = 1; i <= 2 * n; i++) {
if(vis[i] == 0) {
int cur = 0;
dfs(i, cur);
cur = abs(cur);
tot += cur;
bs |= (bs << cur);
//cerr << cur << "\n";
}
}
for(i = 1; i <= 4 * n; i++) {
if(vis[i] == 0) {
cout << "NO";
return 0;
}
}
for(i = 0; i <= MAXVAL; i++) {
if(abs(sl - sr + tot - 2 * i) <= k && bs[i]) {
cout << "YES";
return 0;
}
}
cout << "NO";
//cin.close();
//cout.close();
return 0;
}
Compilation message
tug.cpp: In function 'int main()':
tug.cpp:81:43: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
else if(deg[it.first] + 1 == g[it.first].size()) {
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3320 KB |
Output is correct |
2 |
Correct |
4 ms |
3196 KB |
Output is correct |
3 |
Correct |
4 ms |
3192 KB |
Output is correct |
4 |
Correct |
4 ms |
3192 KB |
Output is correct |
5 |
Correct |
4 ms |
3192 KB |
Output is correct |
6 |
Correct |
5 ms |
3320 KB |
Output is correct |
7 |
Correct |
4 ms |
3192 KB |
Output is correct |
8 |
Correct |
4 ms |
3192 KB |
Output is correct |
9 |
Correct |
4 ms |
3192 KB |
Output is correct |
10 |
Correct |
5 ms |
3192 KB |
Output is correct |
11 |
Correct |
4 ms |
3320 KB |
Output is correct |
12 |
Correct |
4 ms |
3192 KB |
Output is correct |
13 |
Correct |
4 ms |
3324 KB |
Output is correct |
14 |
Correct |
4 ms |
3320 KB |
Output is correct |
15 |
Correct |
4 ms |
3192 KB |
Output is correct |
16 |
Correct |
5 ms |
3192 KB |
Output is correct |
17 |
Correct |
4 ms |
3192 KB |
Output is correct |
18 |
Correct |
4 ms |
3192 KB |
Output is correct |
19 |
Correct |
4 ms |
3192 KB |
Output is correct |
20 |
Correct |
4 ms |
3320 KB |
Output is correct |
21 |
Correct |
4 ms |
3320 KB |
Output is correct |
22 |
Correct |
4 ms |
3320 KB |
Output is correct |
23 |
Correct |
4 ms |
3320 KB |
Output is correct |
24 |
Correct |
4 ms |
3192 KB |
Output is correct |
25 |
Correct |
5 ms |
3192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3320 KB |
Output is correct |
2 |
Correct |
4 ms |
3196 KB |
Output is correct |
3 |
Correct |
4 ms |
3192 KB |
Output is correct |
4 |
Correct |
4 ms |
3192 KB |
Output is correct |
5 |
Correct |
4 ms |
3192 KB |
Output is correct |
6 |
Correct |
5 ms |
3320 KB |
Output is correct |
7 |
Correct |
4 ms |
3192 KB |
Output is correct |
8 |
Correct |
4 ms |
3192 KB |
Output is correct |
9 |
Correct |
4 ms |
3192 KB |
Output is correct |
10 |
Correct |
5 ms |
3192 KB |
Output is correct |
11 |
Correct |
4 ms |
3320 KB |
Output is correct |
12 |
Correct |
4 ms |
3192 KB |
Output is correct |
13 |
Correct |
4 ms |
3324 KB |
Output is correct |
14 |
Correct |
4 ms |
3320 KB |
Output is correct |
15 |
Correct |
4 ms |
3192 KB |
Output is correct |
16 |
Correct |
5 ms |
3192 KB |
Output is correct |
17 |
Correct |
4 ms |
3192 KB |
Output is correct |
18 |
Correct |
4 ms |
3192 KB |
Output is correct |
19 |
Correct |
4 ms |
3192 KB |
Output is correct |
20 |
Correct |
4 ms |
3320 KB |
Output is correct |
21 |
Correct |
4 ms |
3320 KB |
Output is correct |
22 |
Correct |
4 ms |
3320 KB |
Output is correct |
23 |
Correct |
4 ms |
3320 KB |
Output is correct |
24 |
Correct |
4 ms |
3192 KB |
Output is correct |
25 |
Correct |
5 ms |
3192 KB |
Output is correct |
26 |
Correct |
34 ms |
3704 KB |
Output is correct |
27 |
Correct |
11 ms |
3576 KB |
Output is correct |
28 |
Correct |
34 ms |
3576 KB |
Output is correct |
29 |
Correct |
11 ms |
3576 KB |
Output is correct |
30 |
Correct |
35 ms |
3576 KB |
Output is correct |
31 |
Correct |
11 ms |
3524 KB |
Output is correct |
32 |
Correct |
33 ms |
3576 KB |
Output is correct |
33 |
Correct |
10 ms |
3576 KB |
Output is correct |
34 |
Correct |
9 ms |
3500 KB |
Output is correct |
35 |
Correct |
11 ms |
3576 KB |
Output is correct |
36 |
Correct |
35 ms |
3576 KB |
Output is correct |
37 |
Correct |
11 ms |
3576 KB |
Output is correct |
38 |
Correct |
35 ms |
3576 KB |
Output is correct |
39 |
Correct |
11 ms |
3576 KB |
Output is correct |
40 |
Correct |
33 ms |
3612 KB |
Output is correct |
41 |
Correct |
11 ms |
3576 KB |
Output is correct |
42 |
Correct |
34 ms |
3580 KB |
Output is correct |
43 |
Correct |
11 ms |
3576 KB |
Output is correct |
44 |
Correct |
36 ms |
3704 KB |
Output is correct |
45 |
Correct |
10 ms |
3576 KB |
Output is correct |
46 |
Correct |
35 ms |
3576 KB |
Output is correct |
47 |
Correct |
8 ms |
3576 KB |
Output is correct |
48 |
Correct |
21 ms |
3576 KB |
Output is correct |
49 |
Correct |
20 ms |
3576 KB |
Output is correct |
50 |
Correct |
34 ms |
3576 KB |
Output is correct |
51 |
Correct |
35 ms |
3576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
4856 KB |
Output is correct |
2 |
Correct |
25 ms |
4984 KB |
Output is correct |
3 |
Correct |
95 ms |
4864 KB |
Output is correct |
4 |
Correct |
24 ms |
4984 KB |
Output is correct |
5 |
Correct |
92 ms |
4828 KB |
Output is correct |
6 |
Correct |
24 ms |
4956 KB |
Output is correct |
7 |
Correct |
96 ms |
5000 KB |
Output is correct |
8 |
Correct |
25 ms |
4984 KB |
Output is correct |
9 |
Correct |
97 ms |
4728 KB |
Output is correct |
10 |
Correct |
27 ms |
4984 KB |
Output is correct |
11 |
Correct |
95 ms |
4856 KB |
Output is correct |
12 |
Correct |
26 ms |
4984 KB |
Output is correct |
13 |
Correct |
98 ms |
4728 KB |
Output is correct |
14 |
Correct |
95 ms |
4856 KB |
Output is correct |
15 |
Correct |
25 ms |
4984 KB |
Output is correct |
16 |
Correct |
94 ms |
4856 KB |
Output is correct |
17 |
Correct |
23 ms |
4984 KB |
Output is correct |
18 |
Correct |
96 ms |
4864 KB |
Output is correct |
19 |
Correct |
23 ms |
4984 KB |
Output is correct |
20 |
Correct |
103 ms |
4856 KB |
Output is correct |
21 |
Correct |
20 ms |
5112 KB |
Output is correct |
22 |
Correct |
52 ms |
4984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3320 KB |
Output is correct |
2 |
Correct |
4 ms |
3196 KB |
Output is correct |
3 |
Correct |
4 ms |
3192 KB |
Output is correct |
4 |
Correct |
4 ms |
3192 KB |
Output is correct |
5 |
Correct |
4 ms |
3192 KB |
Output is correct |
6 |
Correct |
5 ms |
3320 KB |
Output is correct |
7 |
Correct |
4 ms |
3192 KB |
Output is correct |
8 |
Correct |
4 ms |
3192 KB |
Output is correct |
9 |
Correct |
4 ms |
3192 KB |
Output is correct |
10 |
Correct |
5 ms |
3192 KB |
Output is correct |
11 |
Correct |
4 ms |
3320 KB |
Output is correct |
12 |
Correct |
4 ms |
3192 KB |
Output is correct |
13 |
Correct |
4 ms |
3324 KB |
Output is correct |
14 |
Correct |
4 ms |
3320 KB |
Output is correct |
15 |
Correct |
4 ms |
3192 KB |
Output is correct |
16 |
Correct |
5 ms |
3192 KB |
Output is correct |
17 |
Correct |
4 ms |
3192 KB |
Output is correct |
18 |
Correct |
4 ms |
3192 KB |
Output is correct |
19 |
Correct |
4 ms |
3192 KB |
Output is correct |
20 |
Correct |
4 ms |
3320 KB |
Output is correct |
21 |
Correct |
4 ms |
3320 KB |
Output is correct |
22 |
Correct |
4 ms |
3320 KB |
Output is correct |
23 |
Correct |
4 ms |
3320 KB |
Output is correct |
24 |
Correct |
4 ms |
3192 KB |
Output is correct |
25 |
Correct |
5 ms |
3192 KB |
Output is correct |
26 |
Correct |
34 ms |
3704 KB |
Output is correct |
27 |
Correct |
11 ms |
3576 KB |
Output is correct |
28 |
Correct |
34 ms |
3576 KB |
Output is correct |
29 |
Correct |
11 ms |
3576 KB |
Output is correct |
30 |
Correct |
35 ms |
3576 KB |
Output is correct |
31 |
Correct |
11 ms |
3524 KB |
Output is correct |
32 |
Correct |
33 ms |
3576 KB |
Output is correct |
33 |
Correct |
10 ms |
3576 KB |
Output is correct |
34 |
Correct |
9 ms |
3500 KB |
Output is correct |
35 |
Correct |
11 ms |
3576 KB |
Output is correct |
36 |
Correct |
35 ms |
3576 KB |
Output is correct |
37 |
Correct |
11 ms |
3576 KB |
Output is correct |
38 |
Correct |
35 ms |
3576 KB |
Output is correct |
39 |
Correct |
11 ms |
3576 KB |
Output is correct |
40 |
Correct |
33 ms |
3612 KB |
Output is correct |
41 |
Correct |
11 ms |
3576 KB |
Output is correct |
42 |
Correct |
34 ms |
3580 KB |
Output is correct |
43 |
Correct |
11 ms |
3576 KB |
Output is correct |
44 |
Correct |
36 ms |
3704 KB |
Output is correct |
45 |
Correct |
10 ms |
3576 KB |
Output is correct |
46 |
Correct |
35 ms |
3576 KB |
Output is correct |
47 |
Correct |
8 ms |
3576 KB |
Output is correct |
48 |
Correct |
21 ms |
3576 KB |
Output is correct |
49 |
Correct |
20 ms |
3576 KB |
Output is correct |
50 |
Correct |
34 ms |
3576 KB |
Output is correct |
51 |
Correct |
35 ms |
3576 KB |
Output is correct |
52 |
Correct |
90 ms |
4856 KB |
Output is correct |
53 |
Correct |
25 ms |
4984 KB |
Output is correct |
54 |
Correct |
95 ms |
4864 KB |
Output is correct |
55 |
Correct |
24 ms |
4984 KB |
Output is correct |
56 |
Correct |
92 ms |
4828 KB |
Output is correct |
57 |
Correct |
24 ms |
4956 KB |
Output is correct |
58 |
Correct |
96 ms |
5000 KB |
Output is correct |
59 |
Correct |
25 ms |
4984 KB |
Output is correct |
60 |
Correct |
97 ms |
4728 KB |
Output is correct |
61 |
Correct |
27 ms |
4984 KB |
Output is correct |
62 |
Correct |
95 ms |
4856 KB |
Output is correct |
63 |
Correct |
26 ms |
4984 KB |
Output is correct |
64 |
Correct |
98 ms |
4728 KB |
Output is correct |
65 |
Correct |
95 ms |
4856 KB |
Output is correct |
66 |
Correct |
25 ms |
4984 KB |
Output is correct |
67 |
Correct |
94 ms |
4856 KB |
Output is correct |
68 |
Correct |
23 ms |
4984 KB |
Output is correct |
69 |
Correct |
96 ms |
4864 KB |
Output is correct |
70 |
Correct |
23 ms |
4984 KB |
Output is correct |
71 |
Correct |
103 ms |
4856 KB |
Output is correct |
72 |
Correct |
20 ms |
5112 KB |
Output is correct |
73 |
Correct |
52 ms |
4984 KB |
Output is correct |
74 |
Correct |
451 ms |
8144 KB |
Output is correct |
75 |
Correct |
62 ms |
8056 KB |
Output is correct |
76 |
Correct |
464 ms |
7928 KB |
Output is correct |
77 |
Correct |
80 ms |
7928 KB |
Output is correct |
78 |
Correct |
479 ms |
7928 KB |
Output is correct |
79 |
Correct |
466 ms |
8064 KB |
Output is correct |
80 |
Correct |
85 ms |
7928 KB |
Output is correct |
81 |
Correct |
69 ms |
7928 KB |
Output is correct |
82 |
Correct |
447 ms |
7928 KB |
Output is correct |
83 |
Correct |
469 ms |
8184 KB |
Output is correct |
84 |
Correct |
91 ms |
8024 KB |
Output is correct |
85 |
Correct |
464 ms |
7928 KB |
Output is correct |
86 |
Correct |
80 ms |
7928 KB |
Output is correct |
87 |
Correct |
456 ms |
7928 KB |
Output is correct |
88 |
Correct |
82 ms |
8156 KB |
Output is correct |
89 |
Correct |
471 ms |
8032 KB |
Output is correct |
90 |
Correct |
69 ms |
7900 KB |
Output is correct |
91 |
Correct |
458 ms |
8008 KB |
Output is correct |
92 |
Correct |
75 ms |
7928 KB |
Output is correct |
93 |
Correct |
467 ms |
8012 KB |
Output is correct |
94 |
Correct |
80 ms |
9080 KB |
Output is correct |
95 |
Correct |
246 ms |
8568 KB |
Output is correct |
96 |
Correct |
275 ms |
8740 KB |
Output is correct |
97 |
Correct |
485 ms |
8008 KB |
Output is correct |
98 |
Correct |
468 ms |
8056 KB |
Output is correct |