#include "bits/stdc++.h"
using namespace std;
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
bool is = false;
for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
return o << '}';
}
#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif
using ll = long long;
bool possible[1000005];
int len[6005];
vector<int> adj[6005];
bool leaf[6005];
int cycle[1000005];
int ans[6005];
vector<int> active;
int n, k, m, s;
void updcycle(int root, int node, int state) {
if (leaf[node]) return;
int cycledist = len[node] - len[root] + s;
if (cycledist > k) return;
cycle[cycledist] += state;
for (auto nxt : adj[node]) updcycle(root, nxt, state);
}
void dfs(int node) {
if (leaf[node]) {
// terminate
int req = k - len[node];
if (req == 0) {
ans[node - n] = 1;
return;
}
for (int i = 1; i * i <= req; i++) {
if (req % i == 0) {
if (cycle[i] || cycle[req / i]) {
ans[node - n] = 1;
return;
}
}
}
req -= s;
// bridge
for (int i = active.size() - 1; i >= 0; i--) {
// bridge ending at i
int dist = k - (len[node] - len[active[i]] + s);
if (dist < 0) continue;
if (possible[dist]) {
ans[node - n] = 1;
return;
}
}
return;
}
// update cycles from this directory
updcycle(node, node, 1);
active.push_back(node);
for (auto nxt : adj[node]) dfs(nxt);
active.pop_back();
updcycle(node, node, -1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("", "r", stdin);
// freopen("", "w", stdout);
cin >> n >> m >> k >> s;
s++;
possible[0] = true;
for (int i = 1; i <= n; i++) {
// folder
int p, l; cin >> p >> l;
l++;
adj[p].push_back(i);
len[i] = len[p] + l;
possible[len[i]] = true;
}
for (int i = n + 1; i <= n + m; i++) {
int p, l; cin >> p >> l;
leaf[i] = true;
l++;
len[i] = len[p] + l;
adj[p].push_back(i);
}
dfs(0);
for (int i = 1; i <= m; i++) {
if (ans[i]) {
cout << "YES" << "\n";
} else cout << "NO\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
4 |
Correct |
4 ms |
996 KB |
Output is correct |
5 |
Correct |
4 ms |
4584 KB |
Output is correct |
6 |
Correct |
3 ms |
3404 KB |
Output is correct |
7 |
Correct |
6 ms |
4564 KB |
Output is correct |
8 |
Correct |
3 ms |
4308 KB |
Output is correct |
9 |
Correct |
3 ms |
4436 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5204 KB |
Output is correct |
2 |
Correct |
10 ms |
5096 KB |
Output is correct |
3 |
Correct |
9 ms |
5352 KB |
Output is correct |
4 |
Correct |
9 ms |
5304 KB |
Output is correct |
5 |
Correct |
127 ms |
5404 KB |
Output is correct |
6 |
Correct |
131 ms |
5596 KB |
Output is correct |
7 |
Correct |
77 ms |
5188 KB |
Output is correct |
8 |
Correct |
79 ms |
5484 KB |
Output is correct |
9 |
Correct |
10 ms |
4836 KB |
Output is correct |
10 |
Correct |
9 ms |
4296 KB |
Output is correct |
11 |
Correct |
5 ms |
612 KB |
Output is correct |
12 |
Correct |
116 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
4 |
Correct |
4 ms |
996 KB |
Output is correct |
5 |
Correct |
4 ms |
4584 KB |
Output is correct |
6 |
Correct |
3 ms |
3404 KB |
Output is correct |
7 |
Correct |
6 ms |
4564 KB |
Output is correct |
8 |
Correct |
3 ms |
4308 KB |
Output is correct |
9 |
Correct |
3 ms |
4436 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
9 ms |
5204 KB |
Output is correct |
14 |
Correct |
10 ms |
5096 KB |
Output is correct |
15 |
Correct |
9 ms |
5352 KB |
Output is correct |
16 |
Correct |
9 ms |
5304 KB |
Output is correct |
17 |
Correct |
127 ms |
5404 KB |
Output is correct |
18 |
Correct |
131 ms |
5596 KB |
Output is correct |
19 |
Correct |
77 ms |
5188 KB |
Output is correct |
20 |
Correct |
79 ms |
5484 KB |
Output is correct |
21 |
Correct |
10 ms |
4836 KB |
Output is correct |
22 |
Correct |
9 ms |
4296 KB |
Output is correct |
23 |
Correct |
5 ms |
612 KB |
Output is correct |
24 |
Correct |
116 ms |
2396 KB |
Output is correct |
25 |
Correct |
8 ms |
4980 KB |
Output is correct |
26 |
Correct |
7 ms |
5076 KB |
Output is correct |
27 |
Correct |
9 ms |
5096 KB |
Output is correct |
28 |
Correct |
8 ms |
5348 KB |
Output is correct |
29 |
Correct |
120 ms |
5508 KB |
Output is correct |
30 |
Correct |
124 ms |
5224 KB |
Output is correct |
31 |
Correct |
74 ms |
5288 KB |
Output is correct |
32 |
Correct |
81 ms |
5232 KB |
Output is correct |
33 |
Correct |
9 ms |
4180 KB |
Output is correct |
34 |
Correct |
7 ms |
4308 KB |
Output is correct |
35 |
Correct |
5 ms |
724 KB |
Output is correct |
36 |
Correct |
146 ms |
1492 KB |
Output is correct |