#include <bits/stdc++.h>
using namespace std;
// Case 1: exact length
// Case 2: some prefix of directories + symlink + some suffix to file
// Case 3: dist to file + constant*(place symlink in a parent directory of file)
int n, m, k, s;
int dirLen[3001];
vector<int> subdirs[3001];
vector<int> dirfiles[3001];
int fileLen[3001];
bool ans[3001];
set<int> prefixLengths;
void dfsPrefixes(int u, int d) {
prefixLengths.insert(d);
for (int dir : subdirs[u]) {
dfsPrefixes(dir, d + dirLen[dir]);
}
}
vector<int> prefixLenStack;
int numCaseThreeLengths[1000001];
void dfsCaseThree(int u, int d, int v) {
if (d + s > 1000000) return;
numCaseThreeLengths[d+s] += v;
for (int sub : subdirs[u]) {
dfsCaseThree(sub, d + dirLen[sub], v);
}
}
void dfs(int u, int d) {
prefixLenStack.push_back(d);
dfsCaseThree(u, 0, 1);
for (int f : dirfiles[u]) {
int totLen = fileLen[f] + d;
// case 1
if (totLen == k) ans[f] = true;
// case 2
for (int pfx : prefixLenStack) {
int suffix = totLen - pfx + s;
int want = k - suffix;
if (prefixLengths.count(want)) ans[f] = true;
}
// case 3
int want = k - totLen;
if (want > 0) {
for (int i = 1; i*i <= want; i++) {
if (want % i == 0) {
if (numCaseThreeLengths[want/i] > 0) ans[f] = true;
if (numCaseThreeLengths[i] > 0) ans[f] = true;
}
}
}
}
for (int sub : subdirs[u]) {
dfs(sub, d + dirLen[sub]);
}
dfsCaseThree(u, 0, -1);
prefixLenStack.pop_back();
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n >> m >> k >> s; s++;
for (int i = 1; i <= n; i++) {
int p, l; cin >> p >> l;
dirLen[i] = l+1;
subdirs[p].push_back(i);
}
for (int i = 0; i < m; i++) {
int p, l; cin >> p >> l;
fileLen[i] = l+1;
dirfiles[p].push_back(i);
}
// generate prefixes
dfsPrefixes(0, 0);
dfs(0, 0);
for (int i = 0; i < m; i++) {
if (ans[i]) cout << "YES" << "\n";
else cout << "NO" << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
896 KB |
Output is correct |
4 |
Correct |
5 ms |
896 KB |
Output is correct |
5 |
Correct |
7 ms |
4224 KB |
Output is correct |
6 |
Correct |
5 ms |
2944 KB |
Output is correct |
7 |
Correct |
11 ms |
3840 KB |
Output is correct |
8 |
Correct |
4 ms |
3712 KB |
Output is correct |
9 |
Correct |
4 ms |
3840 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
4 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
4480 KB |
Output is correct |
2 |
Correct |
13 ms |
4352 KB |
Output is correct |
3 |
Correct |
13 ms |
4608 KB |
Output is correct |
4 |
Correct |
12 ms |
4480 KB |
Output is correct |
5 |
Correct |
252 ms |
4796 KB |
Output is correct |
6 |
Correct |
249 ms |
4856 KB |
Output is correct |
7 |
Correct |
134 ms |
4472 KB |
Output is correct |
8 |
Correct |
148 ms |
4856 KB |
Output is correct |
9 |
Correct |
14 ms |
4096 KB |
Output is correct |
10 |
Correct |
11 ms |
3584 KB |
Output is correct |
11 |
Correct |
10 ms |
768 KB |
Output is correct |
12 |
Correct |
207 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
896 KB |
Output is correct |
4 |
Correct |
5 ms |
896 KB |
Output is correct |
5 |
Correct |
7 ms |
4224 KB |
Output is correct |
6 |
Correct |
5 ms |
2944 KB |
Output is correct |
7 |
Correct |
11 ms |
3840 KB |
Output is correct |
8 |
Correct |
4 ms |
3712 KB |
Output is correct |
9 |
Correct |
4 ms |
3840 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
4 ms |
896 KB |
Output is correct |
13 |
Correct |
13 ms |
4480 KB |
Output is correct |
14 |
Correct |
13 ms |
4352 KB |
Output is correct |
15 |
Correct |
13 ms |
4608 KB |
Output is correct |
16 |
Correct |
12 ms |
4480 KB |
Output is correct |
17 |
Correct |
252 ms |
4796 KB |
Output is correct |
18 |
Correct |
249 ms |
4856 KB |
Output is correct |
19 |
Correct |
134 ms |
4472 KB |
Output is correct |
20 |
Correct |
148 ms |
4856 KB |
Output is correct |
21 |
Correct |
14 ms |
4096 KB |
Output is correct |
22 |
Correct |
11 ms |
3584 KB |
Output is correct |
23 |
Correct |
10 ms |
768 KB |
Output is correct |
24 |
Correct |
207 ms |
2296 KB |
Output is correct |
25 |
Correct |
11 ms |
4352 KB |
Output is correct |
26 |
Correct |
11 ms |
4608 KB |
Output is correct |
27 |
Correct |
11 ms |
4480 KB |
Output is correct |
28 |
Correct |
12 ms |
4608 KB |
Output is correct |
29 |
Correct |
263 ms |
4840 KB |
Output is correct |
30 |
Correct |
258 ms |
4856 KB |
Output is correct |
31 |
Correct |
163 ms |
4728 KB |
Output is correct |
32 |
Correct |
134 ms |
4600 KB |
Output is correct |
33 |
Correct |
11 ms |
3584 KB |
Output is correct |
34 |
Correct |
10 ms |
3584 KB |
Output is correct |
35 |
Correct |
13 ms |
896 KB |
Output is correct |
36 |
Correct |
262 ms |
1784 KB |
Output is correct |