Submission #764231

#TimeUsernameProblemLanguageResultExecution timeMemory
764231AlmaRegions (IOI09_regions)C++17
0 / 100
1694 ms131072 KiB
#include <bits/stdc++.h> using namespace std; vector<int> region; vector<bool> vis, calc; vector<vector<int>> adj, dp, distrib; void dfs(int u, int r) { vis[u] = true; if (region[u] != r) { dp[r][region[u]]++; } for (int v: adj[u]) { if (!vis[v]) dfs(v, r); } } void dfs2(int u, int r, int cnt) { vis[u] = true; if (region[u] == r) { dp[r][region[u]] += cnt; cnt++; } for (int v: adj[u]) { if (!vis[v]) dfs2(v, r, cnt); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, r, q, h, s, r1, r2; cin >> n >> r >> q; region = vector<int>(n); adj = vector<vector<int>>(n); distrib = vector<vector<int>>(n); cin >> h; h--; region[0] = h; distrib[h].push_back(0); for (int i = 1; i < n; i++) { cin >> s >> h; h--; s--; region[i] = h; distrib[h].push_back(i); adj[s].push_back(i); } calc = vector<bool> (r, false); dp = vector<vector<int>> (r, vector<int>(r, 0)); while (q--) { cin >> r1 >> r2; r1--; r2--; if (not calc[r1]) { vis = vector<bool>(n, false); for (int i: distrib[r1]) { if (!vis[i]) dfs(i, r1); } vis = vector<bool>(n, false); dfs2(0, r1, 0); calc[r1] = true; } cout << dp[r1][r2] << endl << flush; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...