이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
int n, m, k, s;
vector<int> graph[6060];
int to_root[6060], par[6060], can[2020202], cyc[2020202];
bool good[6060];
void add_cycles(int node, int root, int val) {
cyc[to_root[node] - to_root[root] + s] += val;
for (int i : graph[node]) if (i <= n) add_cycles(i, root, val);
}
void dfs(int node = 0) {
if (node > n) {
int add_dist = k - to_root[node];
if (!add_dist) good[node] = true;
for (int i = 1; i * i <= add_dist; i++)
if (add_dist % i == 0 && (cyc[i] || cyc[add_dist / i])) good[node] = true;
for (int i = par[node]; ~i; i = par[i])
if (add_dist - s + to_root[i] >= 0 && can[add_dist - s + to_root[i]]) good[node] = true;
}
add_cycles(node, node, 1);
for (int i : graph[node]) dfs(i);
add_cycles(node, node, -1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k >> s;
s++;
par[0] = -1;
can[0] = 1;
FOR(i, 1, n + m + 1) {
int l;
cin >> par[i] >> l;
graph[par[i]].push_back(i);
to_root[i] = to_root[par[i]] + l + 1;
if (i <= n) can[to_root[i]]++;
}
dfs();
FOR(i, n + 1, n + m + 1) cout << (good[i] ? "YES\n" : "NO\n");
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |