#pragma GCC optimize("Ofast,unroll-loops")
#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 |
1008 KB |
Output is correct |
4 |
Correct |
4 ms |
1492 KB |
Output is correct |
5 |
Correct |
8 ms |
4820 KB |
Output is correct |
6 |
Correct |
7 ms |
3796 KB |
Output is correct |
7 |
Correct |
9 ms |
5588 KB |
Output is correct |
8 |
Correct |
4 ms |
4076 KB |
Output is correct |
9 |
Correct |
4 ms |
4052 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
4820 KB |
Output is correct |
2 |
Correct |
16 ms |
4844 KB |
Output is correct |
3 |
Correct |
17 ms |
4824 KB |
Output is correct |
4 |
Correct |
17 ms |
4820 KB |
Output is correct |
5 |
Correct |
112 ms |
25012 KB |
Output is correct |
6 |
Correct |
110 ms |
24620 KB |
Output is correct |
7 |
Correct |
78 ms |
15880 KB |
Output is correct |
8 |
Correct |
80 ms |
15604 KB |
Output is correct |
9 |
Correct |
18 ms |
4948 KB |
Output is correct |
10 |
Correct |
14 ms |
4988 KB |
Output is correct |
11 |
Correct |
5 ms |
1524 KB |
Output is correct |
12 |
Correct |
81 ms |
21580 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 |
1008 KB |
Output is correct |
4 |
Correct |
4 ms |
1492 KB |
Output is correct |
5 |
Correct |
8 ms |
4820 KB |
Output is correct |
6 |
Correct |
7 ms |
3796 KB |
Output is correct |
7 |
Correct |
9 ms |
5588 KB |
Output is correct |
8 |
Correct |
4 ms |
4076 KB |
Output is correct |
9 |
Correct |
4 ms |
4052 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
1364 KB |
Output is correct |
13 |
Correct |
17 ms |
4820 KB |
Output is correct |
14 |
Correct |
16 ms |
4844 KB |
Output is correct |
15 |
Correct |
17 ms |
4824 KB |
Output is correct |
16 |
Correct |
17 ms |
4820 KB |
Output is correct |
17 |
Correct |
112 ms |
25012 KB |
Output is correct |
18 |
Correct |
110 ms |
24620 KB |
Output is correct |
19 |
Correct |
78 ms |
15880 KB |
Output is correct |
20 |
Correct |
80 ms |
15604 KB |
Output is correct |
21 |
Correct |
18 ms |
4948 KB |
Output is correct |
22 |
Correct |
14 ms |
4988 KB |
Output is correct |
23 |
Correct |
5 ms |
1524 KB |
Output is correct |
24 |
Correct |
81 ms |
21580 KB |
Output is correct |
25 |
Correct |
15 ms |
4812 KB |
Output is correct |
26 |
Correct |
13 ms |
4752 KB |
Output is correct |
27 |
Correct |
14 ms |
4788 KB |
Output is correct |
28 |
Correct |
15 ms |
4772 KB |
Output is correct |
29 |
Correct |
115 ms |
24648 KB |
Output is correct |
30 |
Correct |
115 ms |
24988 KB |
Output is correct |
31 |
Correct |
76 ms |
15816 KB |
Output is correct |
32 |
Correct |
70 ms |
15664 KB |
Output is correct |
33 |
Correct |
13 ms |
5108 KB |
Output is correct |
34 |
Correct |
11 ms |
5024 KB |
Output is correct |
35 |
Correct |
5 ms |
1472 KB |
Output is correct |
36 |
Correct |
114 ms |
25824 KB |
Output is correct |