cf246E. Blood Cousins Return
Posted Jozky86
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf246E. Blood Cousins Return相关的知识,希望对你有一定的参考价值。
题意:
给你一个森林,每个点都有自己的种类,问以v为根节点的子树中,与v距离为k的节点有多少种
题解:
和cf208E. Blood Cousins这个题差不多,就是多了一个种类,用一个unordered_map对名字进行编号,用map对每一层的名字进行标记,(能用unordered_map的就不要用map,不然后超时)
详细看代码
代码:
// Problem: E. Blood Cousins Return
// Contest: Codeforces - Codeforces Round #151 (Div. 2)
// URL: https://codeforces.com/contest/246/problem/E
// Memory Limit: 256 MB
// Time Limit: 3000 ms
// Data:2021-09-02 17:37:18
// By Jozky
#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
x= 0;
char c= getchar();
bool flag= 0;
while (c < '0' || c > '9')
flag|= (c == '-'), c= getchar();
while (c >= '0' && c <= '9')
x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
if (flag)
x= -x;
read(Ar...);
}
template <typename T> inline void write(T x)
{
if (x < 0) {
x= ~(x - 1);
putchar('-');
}
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
startTime= clock();
freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
endTime= clock();
printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn= 1e5 + 9;
int n, m;
vector<int> vec[maxn];
vector<PII> q[maxn];
unordered_map<string, int> mp;
unordered_map<int, string> na;
int f[maxn][30];
int son[maxn];
int Son;
int dep[maxn], siz[maxn];
void dfs1(int u, int fa)
{
dep[u]= dep[fa] + 1;
siz[u]= 1;
f[u][0]= fa;
for (int i= 1; i <= 20; i++)
f[u][i]= f[f[u][i - 1]][i - 1];
for (auto v : vec[u]) {
if (v == fa)
continue;
dfs1(v, u);
siz[u]+= siz[v];
if (siz[v] > siz[son[u]])
son[u]= v;
}
}
int find_f(int u, int k)
{
for (int i= 0; i <= 20; i++) {
if ((1 << i) & k)
u= f[u][i];
}
return u;
}
map<pair<int, int>, int> iff;
// int iff[maxn][200];
int ans[maxn];
int num[maxn];
void add(int u, int fa, int val)
{
int id= mp[na[u]];
// cout<<"name="<<na[u]<<" id="<<id<<endl;
if (val == 1) {
iff[{id, dep[u]}]++;
if (iff[{id, dep[u]}] == 1)
num[dep[u]]+= val;
}
else if (val == -1) {
iff[{id, dep[u]}]--;
if (iff[{id, dep[u]}] == 0)
num[dep[u]]+= val;
}
for (auto v : vec[u]) {
if (v == fa || v == Son)
continue;
add(v, u, val);
}
}
void dfs2(int u, int fa, int keep)
{
for (auto v : vec[u]) {
if (v == fa || v == son[u])
continue;
dfs2(v, u, 0);
}
if (son[u]) {
dfs2(son[u], u, 1);
Son= son[u];
}
add(u, fa, 1);
for (auto it : q[u]) {
int deep= it.first + dep[u];
int id= it.second;
ans[id]= max(0, num[deep]);
}
Son= 0;
if (!keep) {
add(u, fa, -1);
}
}
int main()
{
//rd_test();
read(n);
for (int i= 1; i <= n; i++) {
string name;
int x;
cin >> name >> x;
// if(mp[name]!=0)
na[i]= name;
mp[name]= i;
vec[x].push_back(i);
}
dfs1(0, 0);
read(m);
for (int i= 1; i <= m; i++) {
int v, k;
read(v, k);
// int f= find_f(v, k);
q[v].push_back({k, i});
}
dfs2(0, 0, 0);
for (int i= 1; i <= m; i++)
printf("%d\\n", ans[i]);
//Time_test();
}
以上是关于cf246E. Blood Cousins Return的主要内容,如果未能解决你的问题,请参考以下文章
scss AutomatischanpassendeSchriftgröße