#include <stdio.h>
#include <stdlib.h>
#define N 6005
int n_, s, n, m, k, m_, par[N], *eh[N], eo[N], dep[N], chain[N], o;
int cc(const void *i, const void *j) {
return *(int*)i-*(int*)j;
}
void pus(int i, int j) {
int o = eo[i]++;
if (!o) eh[i] = (int*)malloc(sizeof **eh * 2);
else if (0 == (o & o - 1)) eh[i] = (int*)realloc(eh[i], 2 * o * sizeof **eh);
eh[i][o] = j;
}
void dfs(int u) {
for (int j = 0; j < eo[u]; j += 2) if (eh[u][j] != par[u]) {
int v = eh[u][j], l = eh[u][j+1];
dep[v] = l + dep[u];
dfs(eh[u][j]);
}
}
int main(void) {
scanf("%d%d%d", &n, &m, &k);
n_ = n;
m_ = m;
n = n + m;
for (int p, l, i = 1; i <= n; ++i) {
scanf("%d%d%d", &p, &l, &s);
++s;
par[i] = p;
pus(p, i);
pus(p, l + 1);
}
/* sub 2 */
for (int i = n_ + 1; i <= n; ++i) {
if (dep[i] == k) {
puts("YES");
continue;
}
o = 0;
for (int cur = i; cur != 0; cur = par[cur])
chain[o++] = dep[cur];
chain[o++] = 0;
qsort(chain, o, sizeof *chain, cc);
for (int j = 1; j <= n_; ++j) {
if (dep[j] + s > k)
continue;
int left = k - s - dep[j];
int md, lb = 0, ub = o;
while (ub - lb > 1)
if (chain[md = lb + (ub - lb) / 2] <= left) lb = md;
else ub = md;
if (chain[lb] == left) {
puts("YES");
goto Next;
}
}
puts("NO");
Next:;
}
}
Compilation message
fil.c: In function 'pus':
fil.c:15:24: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
15 | else if (0 == (o & o - 1)) eh[i] = (int*)realloc(eh[i], 2 * o * sizeof **eh);
| ~~^~~
fil.c: In function 'main':
fil.c:28:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d%d%d", &n, &m, &k);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
fil.c:34:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%d%d%d", &p, &l, &s);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
0 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |