#include <bits/stdc++.h>
using namespace std;
const int nx=6e3+5, kx=1e6+5;
int n, m, k, sz, pa[nx], dist[nx], w, res[nx], cnt[kx], vs[kx];
vector<pair<int, int>> d[nx];
vector<int> fact[kx];
void dfs(int u)
{
for (auto [v, w]:d[u]) dist[v]=dist[u]+w, dfs(v);
}
void dfs3(int u, int vl, int rt)
{
if (dist[u]-dist[rt]+sz+1<=k) cnt[dist[u]-dist[rt]+sz+1]+=vl;
for (auto [v, w]:d[u]) if (v<=n) dfs3(v, vl, rt);
}
void dfs2(int u)
{
if (u<=n) dfs3(u, 1, u);
if (u>n) for (auto x:fact[k-dist[u]]) if (cnt[x]>0) res[u]=1;
for (auto [v, w]:d[u]) dfs2(v);
if (u<=n) dfs3(u, -1, u);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m>>k>>sz;
for (int i=1; i<=n; i++) cin>>pa[i]>>w, d[pa[i]].push_back({i, w+1});
for (int i=n+1; i<=n+m; i++) cin>>pa[i]>>w, d[pa[i]].push_back({i, w+1});
dfs(0);
for (int i=n+1; i<=n+m; i++) vs[k-dist[i]]=1;
for (int i=2; i<=k; i++) for (int j=i; j<=k; j+=i) if (vs[j]) fact[j].push_back(i);
dfs2(0);
for (int i=n+1; i<=n+m; i++)
{
if (dist[i]==k) res[i]=1;
set<int> s;
int tmp=i;
while (tmp!=0) s.insert(dist[i]-dist[pa[tmp]]), tmp=pa[tmp];
for (int j=0; j<=n; j++) if (s.find(k-dist[j]-sz-1)!=s.end()) res[i]=1;
if (res[i]) cout<<"YES\n";
else cout<<"NO\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
27228 KB |
Output is correct |
2 |
Correct |
7 ms |
27560 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
10 ms |
27228 KB |
Output is correct |
5 |
Correct |
19 ms |
32020 KB |
Output is correct |
6 |
Correct |
18 ms |
31836 KB |
Output is correct |
7 |
Correct |
24 ms |
31832 KB |
Output is correct |
8 |
Correct |
19 ms |
30556 KB |
Output is correct |
9 |
Correct |
19 ms |
31744 KB |
Output is correct |
10 |
Correct |
4 ms |
27308 KB |
Output is correct |
11 |
Correct |
16 ms |
25316 KB |
Output is correct |
12 |
Correct |
19 ms |
25576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
32092 KB |
Output is correct |
2 |
Correct |
58 ms |
32088 KB |
Output is correct |
3 |
Correct |
57 ms |
32092 KB |
Output is correct |
4 |
Correct |
57 ms |
32240 KB |
Output is correct |
5 |
Correct |
347 ms |
32224 KB |
Output is correct |
6 |
Correct |
341 ms |
32296 KB |
Output is correct |
7 |
Correct |
232 ms |
32300 KB |
Output is correct |
8 |
Correct |
231 ms |
32500 KB |
Output is correct |
9 |
Correct |
51 ms |
32088 KB |
Output is correct |
10 |
Correct |
39 ms |
30384 KB |
Output is correct |
11 |
Correct |
71 ms |
25468 KB |
Output is correct |
12 |
Correct |
371 ms |
26960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
27228 KB |
Output is correct |
2 |
Correct |
7 ms |
27560 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
10 ms |
27228 KB |
Output is correct |
5 |
Correct |
19 ms |
32020 KB |
Output is correct |
6 |
Correct |
18 ms |
31836 KB |
Output is correct |
7 |
Correct |
24 ms |
31832 KB |
Output is correct |
8 |
Correct |
19 ms |
30556 KB |
Output is correct |
9 |
Correct |
19 ms |
31744 KB |
Output is correct |
10 |
Correct |
4 ms |
27308 KB |
Output is correct |
11 |
Correct |
16 ms |
25316 KB |
Output is correct |
12 |
Correct |
19 ms |
25576 KB |
Output is correct |
13 |
Correct |
56 ms |
32092 KB |
Output is correct |
14 |
Correct |
58 ms |
32088 KB |
Output is correct |
15 |
Correct |
57 ms |
32092 KB |
Output is correct |
16 |
Correct |
57 ms |
32240 KB |
Output is correct |
17 |
Correct |
347 ms |
32224 KB |
Output is correct |
18 |
Correct |
341 ms |
32296 KB |
Output is correct |
19 |
Correct |
232 ms |
32300 KB |
Output is correct |
20 |
Correct |
231 ms |
32500 KB |
Output is correct |
21 |
Correct |
51 ms |
32088 KB |
Output is correct |
22 |
Correct |
39 ms |
30384 KB |
Output is correct |
23 |
Correct |
71 ms |
25468 KB |
Output is correct |
24 |
Correct |
371 ms |
26960 KB |
Output is correct |
25 |
Correct |
57 ms |
32092 KB |
Output is correct |
26 |
Correct |
56 ms |
32092 KB |
Output is correct |
27 |
Correct |
59 ms |
32092 KB |
Output is correct |
28 |
Correct |
59 ms |
32088 KB |
Output is correct |
29 |
Correct |
363 ms |
32364 KB |
Output is correct |
30 |
Correct |
364 ms |
32600 KB |
Output is correct |
31 |
Correct |
232 ms |
32336 KB |
Output is correct |
32 |
Correct |
217 ms |
32340 KB |
Output is correct |
33 |
Correct |
42 ms |
32236 KB |
Output is correct |
34 |
Correct |
39 ms |
32084 KB |
Output is correct |
35 |
Correct |
63 ms |
25516 KB |
Output is correct |
36 |
Correct |
471 ms |
26216 KB |
Output is correct |