#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int n, m, k, s, dep[6006], occ[2000006], l[6006], ans[6006];
vector<int> E[6006], cur, child[6006];
void dfs(int u)
{
if (u <= n)
{
cur.emplace_back(u);
for(auto p : cur)
child[p].emplace_back(u);
for (auto v : E[u])
{
dep[v] = dep[u] + l[v];
dfs(v);
}
cur.pop_back();
}
else
{
ans[u] = (dep[u] == k);
for (int i = 1; i * i <= k - dep[u]; i++)
if ((k - dep[u]) % i == 0)
{
int j = (k - dep[u]) / i;
ans[u] |= (occ[i] || occ[j]);
}
}
}
void dfscycle(int u)
{
if (u <= n)
{
for (auto c : child[u])
occ[dep[c] - dep[u] + s]++;
for (auto v : E[u])
dfscycle(v);
for (auto c : child[u])
occ[dep[c] - dep[u] + s]--;
}
else
{
ans[u] = (dep[u] == k);
for (int i = 1; i * i <= k - dep[u]; i++)
if ((k - dep[u]) % i == 0)
{
int j = (k - dep[u]) / i;
ans[u] |= (occ[i] || occ[j]);
}
}
}
void dfsjump(int u)
{
if (u <= n)
{
cur.emplace_back(u);
for (auto v : E[u])
dfsjump(v);
cur.pop_back();
}
else
{
for (auto p : cur)
{
int d = k - (dep[u] - dep[p] + s);
if(d >= 0)
ans[u] |= occ[d];
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m >> k >> s;
s++;
for (int i = 1; i <= n + m; i++)
{
int p;
cin >> p >> l[i];
l[i]++;
E[p].emplace_back(i);
}
dfs(0);
dfscycle(0);
for (int i = 0; i <= n; i++)
occ[dep[i]]++;
dfsjump(0);
for (int i = n + 1; i <= n + m; i++)
cout << (ans[i] ? "YES\n" : "NO\n");
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
4 |
Correct |
3 ms |
1492 KB |
Output is correct |
5 |
Correct |
7 ms |
4820 KB |
Output is correct |
6 |
Correct |
6 ms |
3796 KB |
Output is correct |
7 |
Correct |
9 ms |
5532 KB |
Output is correct |
8 |
Correct |
4 ms |
4052 KB |
Output is correct |
9 |
Correct |
4 ms |
4080 KB |
Output is correct |
10 |
Correct |
0 ms |
616 KB |
Output is correct |
11 |
Correct |
1 ms |
628 KB |
Output is correct |
12 |
Correct |
3 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4808 KB |
Output is correct |
2 |
Correct |
16 ms |
4820 KB |
Output is correct |
3 |
Correct |
15 ms |
4788 KB |
Output is correct |
4 |
Correct |
15 ms |
4820 KB |
Output is correct |
5 |
Correct |
114 ms |
24948 KB |
Output is correct |
6 |
Correct |
96 ms |
24556 KB |
Output is correct |
7 |
Correct |
71 ms |
15788 KB |
Output is correct |
8 |
Correct |
72 ms |
15568 KB |
Output is correct |
9 |
Correct |
16 ms |
4948 KB |
Output is correct |
10 |
Correct |
14 ms |
4948 KB |
Output is correct |
11 |
Correct |
5 ms |
1516 KB |
Output is correct |
12 |
Correct |
92 ms |
21556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
4 |
Correct |
3 ms |
1492 KB |
Output is correct |
5 |
Correct |
7 ms |
4820 KB |
Output is correct |
6 |
Correct |
6 ms |
3796 KB |
Output is correct |
7 |
Correct |
9 ms |
5532 KB |
Output is correct |
8 |
Correct |
4 ms |
4052 KB |
Output is correct |
9 |
Correct |
4 ms |
4080 KB |
Output is correct |
10 |
Correct |
0 ms |
616 KB |
Output is correct |
11 |
Correct |
1 ms |
628 KB |
Output is correct |
12 |
Correct |
3 ms |
1364 KB |
Output is correct |
13 |
Correct |
15 ms |
4808 KB |
Output is correct |
14 |
Correct |
16 ms |
4820 KB |
Output is correct |
15 |
Correct |
15 ms |
4788 KB |
Output is correct |
16 |
Correct |
15 ms |
4820 KB |
Output is correct |
17 |
Correct |
114 ms |
24948 KB |
Output is correct |
18 |
Correct |
96 ms |
24556 KB |
Output is correct |
19 |
Correct |
71 ms |
15788 KB |
Output is correct |
20 |
Correct |
72 ms |
15568 KB |
Output is correct |
21 |
Correct |
16 ms |
4948 KB |
Output is correct |
22 |
Correct |
14 ms |
4948 KB |
Output is correct |
23 |
Correct |
5 ms |
1516 KB |
Output is correct |
24 |
Correct |
92 ms |
21556 KB |
Output is correct |
25 |
Correct |
14 ms |
4916 KB |
Output is correct |
26 |
Correct |
13 ms |
4852 KB |
Output is correct |
27 |
Correct |
13 ms |
4820 KB |
Output is correct |
28 |
Correct |
14 ms |
4724 KB |
Output is correct |
29 |
Correct |
107 ms |
24708 KB |
Output is correct |
30 |
Correct |
112 ms |
25036 KB |
Output is correct |
31 |
Correct |
76 ms |
15920 KB |
Output is correct |
32 |
Correct |
75 ms |
15664 KB |
Output is correct |
33 |
Correct |
13 ms |
5068 KB |
Output is correct |
34 |
Correct |
12 ms |
5004 KB |
Output is correct |
35 |
Correct |
6 ms |
1364 KB |
Output is correct |
36 |
Correct |
93 ms |
25840 KB |
Output is correct |