Editorial for 2194: Настя і серіали
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
#include <iostream> #include <vector> using namespace std; const int N = 6e5; vector<int> g[N]; bool used[N]; void dfs(int u) { if (used[u]) return; used[u] = 1; for (auto to : g[u]) { dfs(to); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; for (int i = 1; i < n; i++) { int p; cin >> p; --p; g[i].push_back(p); } for (int i = 0; i < k; i++) { int x; cin >> x; --x; dfs(x); } int ans = 0; for (int i = 0; i < n; i++) { ans += used[i]; } cout << ans; return 0; }
Коментарі