# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1125124 | MatteoArcari | 열대 식물원 (Tropical Garden) (IOI11_garden) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
const int lg = 30;
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n, m, p;
cin >> n >> m >> p;
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
int sa = adj[a].size();
int sb = adj[b].size();
if (sa < 2) adj[a].push_back({b, sb == 0});
if (sb < 2) adj[b].push_back({a, sa == 0});
}
vector<vector<pair<int, int>>> up[2];
up[0].resize(lg, vector<pair<int, int>>(n));
up[1].resize(lg, vector<pair<int, int>>(n));
for (int i = 0; i < n; i++) {
up[1][0][i] = adj[i][0];
up[0][0][i] = adj[i][0];
if (adj[i].size() > 1) {
up[1][0][i] = adj[i][1];
}
}
for (int i = 1; i < lg; i++) {
for (int j = 0; j < n; j++) {
{
auto [to, s] = up[0][i - 1][j];
up[0][i][j] = up[s][i - 1][to];
} {
auto [to, s] = up[1][i - 1][j];
up[1][i][j] = up[s][i - 1][to];
}
}
}
auto get_up_bro = [&](int j, int k) {
int cur_state = 0;
for (int i = lg - 1; i >= 0; i--) {
if ((1 << i) <= k) {
auto [to, s] = up[cur_state][i][j];
cur_state = s;
j = to;
k -= (1 << i);
}
}
return j;
};
int q; cin >> q;
while (q--) {
int k; cin >> k;
int ans = 0;
for (int i = 0; i < n; i++) {
ans += get_up_bro(i, k) == p;
}
cout << ans << '\n';
}
}