This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
bool is = false;
for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
return o << '}';
}
#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif
using ll = long long;
bool possible[1000005];
int len[6005];
vector<int> adj[6005];
bool leaf[6005];
int cycle[1000005];
int ans[6005];
vector<int> active;
int n, k, m, s;
void updcycle(int root, int node, int state) {
if (leaf[node]) return;
int cycledist = len[node] - len[root] + s;
if (cycledist > k) return;
cycle[cycledist] += state;
for (auto nxt : adj[node]) updcycle(root, nxt, state);
}
void dfs(int node) {
if (leaf[node]) {
// terminate
int req = k - len[node];
if (req == 0) {
ans[node - n] = 1;
return;
}
for (int i = 1; i * i <= req; i++) {
if (req % i == 0) {
if (cycle[i] || cycle[req / i]) {
ans[node - n] = 1;
return;
}
}
}
req -= s;
// bridge
for (int i = active.size() - 1; i >= 0; i--) {
// bridge ending at i
int dist = k - (len[node] - len[active[i]] + s);
if (dist < 0) continue;
if (possible[dist]) {
ans[node - n] = 1;
return;
}
}
return;
}
// update cycles from this directory
updcycle(node, node, 1);
active.push_back(node);
for (auto nxt : adj[node]) dfs(nxt);
active.pop_back();
updcycle(node, node, -1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("", "r", stdin);
// freopen("", "w", stdout);
cin >> n >> m >> k >> s;
s++;
possible[0] = true;
for (int i = 1; i <= n; i++) {
// folder
int p, l; cin >> p >> l;
l++;
adj[p].push_back(i);
len[i] = len[p] + l;
possible[len[i]] = true;
}
for (int i = n + 1; i <= n + m; i++) {
int p, l; cin >> p >> l;
leaf[i] = true;
l++;
len[i] = len[p] + l;
adj[p].push_back(i);
}
dfs(0);
for (int i = 1; i <= m; i++) {
if (ans[i]) {
cout << "YES" << "\n";
} else cout << "NO\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |