#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 30000
#define N_ (N * 2)
#define W 20
#define S (N * W)
int ii[N_], jj[N_], ww[N_];
int *eh[N_], eo[N_];
void link(int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
char visited[N_], visited_[N_]; int qu1[N_], qu2[N_], cnt, cnt_;
void dfs1(int i) {
int o;
if (visited[i])
return;
visited[i] = 1, qu1[cnt++] = i;
for (o = 0; o < eo[i]; o++) {
int h = eh[i][o];
if (!visited_[h]) {
int j = i ^ ii[h] ^ jj[h];
visited_[h] = 1, qu2[cnt_++] = h;
dfs1(j);
}
}
}
int main() {
static int ae[N_], dd[N_], qu_[N_], kk[S + 1], dq[S + 1];
static char used[N_], dp[S + 1];
int n, d_, h, h_, i, j, d, w, s;
scanf("%d%d", &n, &d_);
for (i = 0; i < n * 2; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
for (h = 0; h < n * 2; h++) {
scanf("%d%d%d", &i, &j, &w), i--, j--, j += n;
ae[i] ^= h, dd[i]++;
ae[j] ^= h, dd[j]++;
ii[h] = i, jj[h] = j, ww[h] = w;
link(i, h), link(j, h);
}
d = 0;
for (i = 0; i < n * 2; i++)
if (!visited[i]) {
cnt = 0, cnt_ = 0;
dfs1(i);
if (cnt != cnt_) {
printf("NO\n");
return 0;
}
for (h = 0; h < cnt; h++)
dd[qu1[h]] = eo[qu1[h]];
cnt_ = 0;
for (h = 0; h < cnt; h++)
if (dd[qu1[h]] == 1)
qu_[cnt_++] = qu1[h];
while (cnt_) {
i = qu_[--cnt_], h = ae[i], j = i ^ ii[h] ^ jj[h];
used[h] = 1, d += i < n ? ww[h] : -ww[h];
ae[i] ^= h, dd[i]--;
ae[j] ^= h, dd[j]--;
if (dd[j] == 1)
qu_[cnt_++] = j;
}
for (h = 0; h < cnt; h++)
if (!used[qu2[h]]) {
h_ = qu2[h], i = ii[h_];
break;
}
j = i;
w = 0;
do {
h_ = ae[j] ^ h_;
w += j < n ? ww[h_] : -ww[h_];
j ^= ii[h_] ^ jj[h_];
} while (j != i);
if (w < 0)
w = -w;
d += w;
kk[w]++;
}
dp[0] = 1;
for (w = 0; w <= S; w++)
if (kk[w] > 0) {
int k = kk[w];
for (s = 0; s <= S && s * 2 <= d + d_; s++)
if (dp[s])
dq[s] = 0;
else if ((dq[s] = (s < w ? k : dq[s - w]) + 1) <= k)
dp[s] = 1;
}
for (s = 0; s <= S && s * 2 <= d + d_; s++)
if (dp[s] && d - s * 2 <= d_) {
printf("YES\n");
return 0;
}
printf("NO\n");
return 0;
}
Compilation message
tug.c: In function 'link':
tug.c:16:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
16 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
tug.c: In function 'main':
tug.c:46:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d%d", &n, &d_);
| ^~~~~~~~~~~~~~~~~~~~~~
tug.c:50:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d%d%d", &i, &j, &w), i--, j--, j += n;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
tug.c:87:8: warning: 'h_' may be used uninitialized in this function [-Wmaybe-uninitialized]
87 | h_ = ae[j] ^ h_;
| ~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
292 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
292 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
292 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
292 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
296 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
7 ms |
744 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
7 ms |
724 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
7 ms |
736 KB |
Output is correct |
32 |
Correct |
2 ms |
584 KB |
Output is correct |
33 |
Correct |
7 ms |
736 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
7 ms |
716 KB |
Output is correct |
36 |
Correct |
2 ms |
596 KB |
Output is correct |
37 |
Correct |
7 ms |
724 KB |
Output is correct |
38 |
Correct |
3 ms |
596 KB |
Output is correct |
39 |
Correct |
7 ms |
692 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
6 ms |
724 KB |
Output is correct |
42 |
Correct |
3 ms |
596 KB |
Output is correct |
43 |
Correct |
7 ms |
724 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
7 ms |
716 KB |
Output is correct |
46 |
Correct |
2 ms |
596 KB |
Output is correct |
47 |
Correct |
2 ms |
596 KB |
Output is correct |
48 |
Correct |
3 ms |
596 KB |
Output is correct |
49 |
Correct |
3 ms |
596 KB |
Output is correct |
50 |
Correct |
3 ms |
692 KB |
Output is correct |
51 |
Correct |
3 ms |
812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1844 KB |
Output is correct |
2 |
Correct |
7 ms |
1748 KB |
Output is correct |
3 |
Correct |
9 ms |
1844 KB |
Output is correct |
4 |
Correct |
7 ms |
1748 KB |
Output is correct |
5 |
Correct |
9 ms |
1804 KB |
Output is correct |
6 |
Correct |
7 ms |
1748 KB |
Output is correct |
7 |
Correct |
9 ms |
1804 KB |
Output is correct |
8 |
Correct |
7 ms |
1820 KB |
Output is correct |
9 |
Correct |
9 ms |
1748 KB |
Output is correct |
10 |
Correct |
7 ms |
1748 KB |
Output is correct |
11 |
Correct |
8 ms |
1844 KB |
Output is correct |
12 |
Correct |
7 ms |
1748 KB |
Output is correct |
13 |
Correct |
9 ms |
1748 KB |
Output is correct |
14 |
Correct |
10 ms |
1748 KB |
Output is correct |
15 |
Correct |
7 ms |
1748 KB |
Output is correct |
16 |
Correct |
9 ms |
1764 KB |
Output is correct |
17 |
Correct |
7 ms |
1844 KB |
Output is correct |
18 |
Correct |
8 ms |
1876 KB |
Output is correct |
19 |
Correct |
7 ms |
1740 KB |
Output is correct |
20 |
Correct |
8 ms |
1876 KB |
Output is correct |
21 |
Correct |
8 ms |
2100 KB |
Output is correct |
22 |
Correct |
10 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
292 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
292 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
296 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
7 ms |
744 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
7 ms |
724 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
7 ms |
736 KB |
Output is correct |
32 |
Correct |
2 ms |
584 KB |
Output is correct |
33 |
Correct |
7 ms |
736 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
7 ms |
716 KB |
Output is correct |
36 |
Correct |
2 ms |
596 KB |
Output is correct |
37 |
Correct |
7 ms |
724 KB |
Output is correct |
38 |
Correct |
3 ms |
596 KB |
Output is correct |
39 |
Correct |
7 ms |
692 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
6 ms |
724 KB |
Output is correct |
42 |
Correct |
3 ms |
596 KB |
Output is correct |
43 |
Correct |
7 ms |
724 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
7 ms |
716 KB |
Output is correct |
46 |
Correct |
2 ms |
596 KB |
Output is correct |
47 |
Correct |
2 ms |
596 KB |
Output is correct |
48 |
Correct |
3 ms |
596 KB |
Output is correct |
49 |
Correct |
3 ms |
596 KB |
Output is correct |
50 |
Correct |
3 ms |
692 KB |
Output is correct |
51 |
Correct |
3 ms |
812 KB |
Output is correct |
52 |
Correct |
9 ms |
1844 KB |
Output is correct |
53 |
Correct |
7 ms |
1748 KB |
Output is correct |
54 |
Correct |
9 ms |
1844 KB |
Output is correct |
55 |
Correct |
7 ms |
1748 KB |
Output is correct |
56 |
Correct |
9 ms |
1804 KB |
Output is correct |
57 |
Correct |
7 ms |
1748 KB |
Output is correct |
58 |
Correct |
9 ms |
1804 KB |
Output is correct |
59 |
Correct |
7 ms |
1820 KB |
Output is correct |
60 |
Correct |
9 ms |
1748 KB |
Output is correct |
61 |
Correct |
7 ms |
1748 KB |
Output is correct |
62 |
Correct |
8 ms |
1844 KB |
Output is correct |
63 |
Correct |
7 ms |
1748 KB |
Output is correct |
64 |
Correct |
9 ms |
1748 KB |
Output is correct |
65 |
Correct |
10 ms |
1748 KB |
Output is correct |
66 |
Correct |
7 ms |
1748 KB |
Output is correct |
67 |
Correct |
9 ms |
1764 KB |
Output is correct |
68 |
Correct |
7 ms |
1844 KB |
Output is correct |
69 |
Correct |
8 ms |
1876 KB |
Output is correct |
70 |
Correct |
7 ms |
1740 KB |
Output is correct |
71 |
Correct |
8 ms |
1876 KB |
Output is correct |
72 |
Correct |
8 ms |
2100 KB |
Output is correct |
73 |
Correct |
10 ms |
1876 KB |
Output is correct |
74 |
Correct |
27 ms |
5448 KB |
Output is correct |
75 |
Correct |
302 ms |
6336 KB |
Output is correct |
76 |
Correct |
26 ms |
5452 KB |
Output is correct |
77 |
Correct |
293 ms |
6336 KB |
Output is correct |
78 |
Correct |
26 ms |
5460 KB |
Output is correct |
79 |
Correct |
26 ms |
5464 KB |
Output is correct |
80 |
Correct |
291 ms |
6316 KB |
Output is correct |
81 |
Correct |
30 ms |
6136 KB |
Output is correct |
82 |
Correct |
22 ms |
6220 KB |
Output is correct |
83 |
Correct |
28 ms |
5400 KB |
Output is correct |
84 |
Correct |
291 ms |
6364 KB |
Output is correct |
85 |
Correct |
28 ms |
5452 KB |
Output is correct |
86 |
Correct |
306 ms |
6272 KB |
Output is correct |
87 |
Correct |
27 ms |
5448 KB |
Output is correct |
88 |
Correct |
296 ms |
6272 KB |
Output is correct |
89 |
Correct |
28 ms |
5468 KB |
Output is correct |
90 |
Correct |
296 ms |
6372 KB |
Output is correct |
91 |
Correct |
27 ms |
5404 KB |
Output is correct |
92 |
Correct |
289 ms |
6316 KB |
Output is correct |
93 |
Correct |
26 ms |
5476 KB |
Output is correct |
94 |
Correct |
26 ms |
4940 KB |
Output is correct |
95 |
Correct |
27 ms |
5044 KB |
Output is correct |
96 |
Correct |
28 ms |
4996 KB |
Output is correct |
97 |
Correct |
29 ms |
6244 KB |
Output is correct |
98 |
Correct |
32 ms |
6316 KB |
Output is correct |