이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))
template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }
using pii = pair<int, int>;
using vi = vector<int>;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int nbNode, nbSpot, nbReq;
cin >> nbNode >> nbSpot >> nbReq;
vector<vi> adj(nbNode);
rep(iEdge, 0, nbNode-1) {
int u, v; cin >> u >> v;
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> spots(nbSpot);
vector<vi> ps(nbNode, vi(nbSpot+1, 0));
rep(iSpot, 0, nbSpot) {
cin >> spots[iSpot];
--spots[iSpot];
ps[spots[iSpot]][iSpot+1]++;
}
rep(node, 0, nbNode) rep(iSpot, 0, nbSpot) {
ps[node][iSpot+1] += ps[node][iSpot];
}
auto in = [&] (int node, int L, int R) {
return ps[node][R+1] - ps[node][L] > 0;
};
rep(iReq, 0, nbReq) {
int L, R;
cin >> L >> R;
--L, --R;
int ans = 0;
auto Dfs = [&] (auto dfs, int node, int parent) -> bool {
bool saw = in(node, L, R);
for (int child : adj[node]) if (child != parent) {
saw |= dfs(dfs, child, node);
}
if (saw) ++ans;
return saw;
};
Dfs(Dfs, spots[L], -1);
cout << ans << '\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |