#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)) != past.end()) {
cout << past[mp(e1, e2)] << "\n";
cout.flush();
continue;
}
ans = 0;
dfs(0);
cout << ans << "\n";
past[mp(e1, e2)] = ans;
cout.flush();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
396 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
11 ms |
384 KB |
Output is correct |
6 |
Correct |
32 ms |
512 KB |
Output is correct |
7 |
Correct |
95 ms |
632 KB |
Output is correct |
8 |
Correct |
126 ms |
632 KB |
Output is correct |
9 |
Correct |
295 ms |
1176 KB |
Output is correct |
10 |
Correct |
1886 ms |
1564 KB |
Output is correct |
11 |
Correct |
3565 ms |
2044 KB |
Output is correct |
12 |
Correct |
3659 ms |
2784 KB |
Output is correct |
13 |
Execution timed out |
8036 ms |
2400 KB |
Time limit exceeded |
14 |
Execution timed out |
8074 ms |
2828 KB |
Time limit exceeded |
15 |
Correct |
6223 ms |
5924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
8035 ms |
5948 KB |
Time limit exceeded |
2 |
Execution timed out |
8061 ms |
4628 KB |
Time limit exceeded |
3 |
Execution timed out |
8045 ms |
8204 KB |
Time limit exceeded |
4 |
Execution timed out |
8013 ms |
2956 KB |
Time limit exceeded |
5 |
Execution timed out |
8095 ms |
5728 KB |
Time limit exceeded |
6 |
Execution timed out |
8058 ms |
4148 KB |
Time limit exceeded |
7 |
Execution timed out |
8048 ms |
4492 KB |
Time limit exceeded |
8 |
Execution timed out |
8020 ms |
9780 KB |
Time limit exceeded |
9 |
Execution timed out |
8038 ms |
9744 KB |
Time limit exceeded |
10 |
Execution timed out |
8074 ms |
14892 KB |
Time limit exceeded |
11 |
Execution timed out |
8029 ms |
9740 KB |
Time limit exceeded |
12 |
Execution timed out |
8029 ms |
11752 KB |
Time limit exceeded |
13 |
Execution timed out |
8058 ms |
11852 KB |
Time limit exceeded |
14 |
Execution timed out |
8044 ms |
11388 KB |
Time limit exceeded |
15 |
Execution timed out |
8099 ms |
15508 KB |
Time limit exceeded |
16 |
Execution timed out |
8067 ms |
21284 KB |
Time limit exceeded |
17 |
Execution timed out |
8060 ms |
20404 KB |
Time limit exceeded |