#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")
using namespace std;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using vl = vector<long long>;
#define mp make_pair
#define pb push_back
#define pp pop_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin() , (x).end()
const int N = 2*100005;
const long long MOD = 1e9+7;
const long double EPS = 0.000000001;
const double PI = 3.14159265358979323846;
const int nx[4]={1, -1, 0, 0}, ny[4]={0, 0, 1, -1};
long long gcd(int a, int b) { return (b==0?a:gcd(b, a%b)); }
long long lcm(int a, int b) { return a*(b/gcd(a, b)); }
long long fact(int a) { return (a==1?1:a*fact(a-1)); }
int n, r, q, timer=0, arr[200005], tin[200005], out[200005]; int cnt[25005];
vector<int>adj[200005], se[25005]; vector<int>home[25005]; map<ii, int>dp; vector<ii>trav[25005];
void dfs(int v, int p) {
cnt[arr[v]]++; tin[v]=++timer;
se[arr[v]].pb(timer); trav[arr[v]].pb({tin[v], cnt[arr[v]]});
for(auto u:adj[v]) {
if(u==p) continue;
dfs(u, v);
}
out[v]=++timer; cnt[arr[v]]--;
/// trav[arr[v]].pb({out[v], cnt[arr[v]]});
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
scanf("%d%d%d", &n, &r, &q);
scanf("%d", &arr[1]); home[arr[1]].pb(1);
for(int l=2;l<=n;l++) {
int a, h; scanf("%d%d", &a, &h); home[h].pb(l);
adj[a].pb(l); adj[l].pb(a); arr[l]=h;
}
dfs(1, 1);
while(q--) {
int a, b; scanf("%d%d", &a, &b); int res=0; bool swp=false;
if(dp.find({a, b})!=dp.end()) { cout<<dp[{a, b}]<<"\n"<<flush; continue; }
if(se[a].size()>se[b].size()) swap(a, b), swp=true;
for(auto u:home[a]) {
int l=tin[u], r=out[u];
if(swp) {
auto it=lb(all(trav[b]), mp(l, INT_MAX));
if(it!=trav[b].begin()) {
it--; res+=it->second;
}
}
else res+=lb(all(se[b]), r)-lb(all(se[b]), l);
}
if(swp) swap(a, b);
dp[{a, b}]=res; cout<<res<<"\n"<<flush;
}
return 0;
}
Compilation message
regions.cpp: In function 'int32_t main()':
regions.cpp:48:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%d%d%d", &n, &r, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
regions.cpp:49:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d", &arr[1]); home[arr[1]].pb(1);
| ~~~~~^~~~~~~~~~~~~~~
regions.cpp:51:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | int a, h; scanf("%d%d", &a, &h); home[h].pb(l);
| ~~~~~^~~~~~~~~~~~~~~~
regions.cpp:56:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | int a, b; scanf("%d%d", &a, &b); int res=0; bool swp=false;
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
6728 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
6728 KB |
Output isn't correct |
3 |
Incorrect |
7 ms |
6728 KB |
Output isn't correct |
4 |
Incorrect |
9 ms |
6728 KB |
Output isn't correct |
5 |
Incorrect |
15 ms |
6856 KB |
Output isn't correct |
6 |
Correct |
24 ms |
6984 KB |
Output is correct |
7 |
Incorrect |
35 ms |
6952 KB |
Output isn't correct |
8 |
Incorrect |
35 ms |
7144 KB |
Output isn't correct |
9 |
Correct |
56 ms |
7880 KB |
Output is correct |
10 |
Incorrect |
109 ms |
7872 KB |
Output isn't correct |
11 |
Incorrect |
177 ms |
8324 KB |
Output isn't correct |
12 |
Incorrect |
163 ms |
9224 KB |
Output isn't correct |
13 |
Incorrect |
255 ms |
9264 KB |
Output isn't correct |
14 |
Incorrect |
315 ms |
9664 KB |
Output isn't correct |
15 |
Correct |
355 ms |
14248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1243 ms |
14700 KB |
Output isn't correct |
2 |
Incorrect |
1494 ms |
14084 KB |
Output isn't correct |
3 |
Incorrect |
2219 ms |
19708 KB |
Output isn't correct |
4 |
Incorrect |
343 ms |
10968 KB |
Output isn't correct |
5 |
Incorrect |
374 ms |
13716 KB |
Output isn't correct |
6 |
Incorrect |
736 ms |
12884 KB |
Output isn't correct |
7 |
Incorrect |
983 ms |
13632 KB |
Output isn't correct |
8 |
Incorrect |
1357 ms |
24188 KB |
Output isn't correct |
9 |
Incorrect |
2158 ms |
27976 KB |
Output isn't correct |
10 |
Incorrect |
3895 ms |
37784 KB |
Output isn't correct |
11 |
Incorrect |
5032 ms |
33568 KB |
Output isn't correct |
12 |
Incorrect |
1613 ms |
24912 KB |
Output isn't correct |
13 |
Incorrect |
2314 ms |
28448 KB |
Output isn't correct |
14 |
Incorrect |
2794 ms |
29828 KB |
Output isn't correct |
15 |
Incorrect |
3685 ms |
38896 KB |
Output isn't correct |
16 |
Incorrect |
3675 ms |
50344 KB |
Output isn't correct |
17 |
Incorrect |
3940 ms |
47588 KB |
Output isn't correct |