#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <limits>
#include <vector>
#include <map>
using namespace std;
vector<int> chld[6001];
int tin[6001], tout[6001], p[6001], len[6001], depth[6001];
inline bool Upper(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }
void DFS(int v)
{
static int time = 0;
tin[v] = ++time;
for (int to: chld[v])
DFS(to);
tout[v] = time;
}
int main()
{
int n, m, des;
cin >> n >> m >> des;
int sym;
cin >> sym;
++sym;
p[0] = -1;
for (int i = 1; i <= n + m; ++i)
{
scanf("%d%d", &p[i], &len[i]);
chld[p[i]].push_back(i);
++len[i];
depth[i] = depth[p[i]] + len[i];
}
DFS(0);
vector<int> pref; pref.reserve(n + m);
vector<int> prefv; prefv.reserve(n + m);
vector<int> stNodes;
for (int i = 0; i <= n; ++i)
stNodes.push_back(i);
sort(stNodes.begin(), stNodes.end(), [](int u, int v) {
return depth[u] < depth[v];
});
for (int i = n + 1; i <= n + m; ++i)
{
pref.clear(); prefv.clear();
for (int j = i; j != -1; j = p[j])
{
prefv.push_back(j);
pref.push_back(depth[j]);
}
reverse(pref.begin(), pref.end());
reverse(prefv.begin(), prefv.end());
bool ans = pref.back() == des;
for (int from = 0; !ans && from <= n; ++from)
if (binary_search(pref.begin(), prev(pref.end()), depth[from] + sym + pref.back() - des))
{
ans = true;
break;
}
if (des - pref.back() > 0)
{
for (int x = 1; !ans && x * x <= des - pref.back(); ++x)
for (int y = 0; !ans && y <= 1; ++y, x = (des - pref.back()) / x)
{
if ((des - pref.back()) % x) continue;
int i = 0;
for (int from: stNodes)
{
// depth[from] - depth[to] + sym == x
// depth[to] = depth[from] + sym - x
while (i + 1 < pref.size() && pref[i + 1] <= depth[from] + sym - x)
++i;
if (pref[i] != depth[from] + sym - x) continue;
if (Upper(prefv[i], from))
{
ans = true;
break;
}
}
}
}
printf(ans ? "YES\n" : "NO\n");
}
}
Compilation message
fil.cpp: In function 'int main()':
fil.cpp:74:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (i + 1 < pref.size() && pref[i + 1] <= depth[from] + sym - x)
~~~~~~^~~~~~~~~~~~~
fil.cpp:32:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &p[i], &len[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
512 KB |
Output is correct |
3 |
Correct |
12 ms |
512 KB |
Output is correct |
4 |
Correct |
14 ms |
512 KB |
Output is correct |
5 |
Correct |
15 ms |
512 KB |
Output is correct |
6 |
Correct |
13 ms |
512 KB |
Output is correct |
7 |
Correct |
17 ms |
512 KB |
Output is correct |
8 |
Correct |
11 ms |
512 KB |
Output is correct |
9 |
Correct |
12 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
9 ms |
512 KB |
Output is correct |
12 |
Correct |
12 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
226 ms |
640 KB |
Output is correct |
2 |
Correct |
238 ms |
768 KB |
Output is correct |
3 |
Correct |
222 ms |
768 KB |
Output is correct |
4 |
Correct |
222 ms |
768 KB |
Output is correct |
5 |
Correct |
237 ms |
896 KB |
Output is correct |
6 |
Correct |
169 ms |
896 KB |
Output is correct |
7 |
Correct |
223 ms |
864 KB |
Output is correct |
8 |
Correct |
184 ms |
768 KB |
Output is correct |
9 |
Correct |
258 ms |
888 KB |
Output is correct |
10 |
Correct |
245 ms |
768 KB |
Output is correct |
11 |
Correct |
12 ms |
768 KB |
Output is correct |
12 |
Correct |
194 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
512 KB |
Output is correct |
3 |
Correct |
12 ms |
512 KB |
Output is correct |
4 |
Correct |
14 ms |
512 KB |
Output is correct |
5 |
Correct |
15 ms |
512 KB |
Output is correct |
6 |
Correct |
13 ms |
512 KB |
Output is correct |
7 |
Correct |
17 ms |
512 KB |
Output is correct |
8 |
Correct |
11 ms |
512 KB |
Output is correct |
9 |
Correct |
12 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
9 ms |
512 KB |
Output is correct |
12 |
Correct |
12 ms |
512 KB |
Output is correct |
13 |
Correct |
226 ms |
640 KB |
Output is correct |
14 |
Correct |
238 ms |
768 KB |
Output is correct |
15 |
Correct |
222 ms |
768 KB |
Output is correct |
16 |
Correct |
222 ms |
768 KB |
Output is correct |
17 |
Correct |
237 ms |
896 KB |
Output is correct |
18 |
Correct |
169 ms |
896 KB |
Output is correct |
19 |
Correct |
223 ms |
864 KB |
Output is correct |
20 |
Correct |
184 ms |
768 KB |
Output is correct |
21 |
Correct |
258 ms |
888 KB |
Output is correct |
22 |
Correct |
245 ms |
768 KB |
Output is correct |
23 |
Correct |
12 ms |
768 KB |
Output is correct |
24 |
Correct |
194 ms |
896 KB |
Output is correct |
25 |
Correct |
176 ms |
768 KB |
Output is correct |
26 |
Correct |
171 ms |
768 KB |
Output is correct |
27 |
Correct |
168 ms |
768 KB |
Output is correct |
28 |
Correct |
194 ms |
948 KB |
Output is correct |
29 |
Correct |
196 ms |
896 KB |
Output is correct |
30 |
Correct |
200 ms |
896 KB |
Output is correct |
31 |
Correct |
244 ms |
888 KB |
Output is correct |
32 |
Correct |
236 ms |
976 KB |
Output is correct |
33 |
Correct |
163 ms |
768 KB |
Output is correct |
34 |
Correct |
157 ms |
768 KB |
Output is correct |
35 |
Correct |
19 ms |
768 KB |
Output is correct |
36 |
Correct |
429 ms |
888 KB |
Output is correct |