# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
302801 | 2020-09-19T08:37:30 Z | BlancaHM | Regions (IOI09_regions) | C++14 | 0 ms | 0 KB |
#include <iostream> #include <fstream> #include <cfloat> #include <iomanip> #include <vector> #include <algorithm> #include <queue> #include <stack> #include <cstring> #include <cmath> #include <climits> #include <set> #include <map> #include <unordered_set> #include <unordered_map> using namespace std; typedef pair<int, int> pii; typedef long long int ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<vvvi> vvvvi; typedef vector<pii> vpii; typedef vector<vpii> vvpii; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef vector<vvvl> vvvvl; typedef vector<string> vs; #define fir first #define sec second #define pb push_back #define eb emplace_back #define ppb pop_back #define pf push_front #define ppf pop_front #define mp make_pair #define len(v) ((int)v.size()) #define all(v) v.begin(), v.end() vi country; vvi G; int N, R, e1, e2, ans; int dfs(int S) { int cur = 0; for (auto v: G[S]) { cur += dfs(v); if (country[v] == e2) cur++; } if (country[S] == e1) ans += cur; return cur; } int main() { //ios::sync_with_stdio(false); //cin.tie(NULL); int Q, cn, sup; cin >> N >> R >> Q; G.assign(N, vi()); country = vi(N); cin >> cn; country[0] = cn; map<pii, int> past; for (int i = 1; i < N; i++) { cin >> sup >> cn; G[sup-1].push_back(i); country[i] = cn; } while(Q--) { cin >> e1 >> e2; if (past.find(mp(e1, e2))) { cout << past[mp(e1, e2)] << "\n"; continue; } ans = 0; dfs(0); cout << ans << "\n"; past[mp(e1, e2)] = ans; cout.flush(); } return 0; }