兴义市建设局网站首页,做微信的微网站费用,巩义做网站优化,汕头seo网站管理cf246E. Blood Cousins Return
题意#xff1a;
给你一个森林#xff0c;每个点都有自己的种类#xff0c;问以v为根节点的子树中#xff0c;与v距离为k的节点有多少种
题解#xff1a;
和cf208E. Blood Cousins这个题差不多#xff0c;就是多了一个种类#xff0c;…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 pairint, 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 LOCALstartTime clock();freopen(in.txt, r, stdin);
#endif
}
void Time_test()
{
#ifdef LOCALendTime clock();printf(\nRun Time:%lfs\n, (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn 1e5 9;
int n, m;
vectorint vec[maxn];
vectorPII q[maxn];
unordered_mapstring, int mp;
unordered_mapint, 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;
}
mappairint, 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]];// coutnamena[u] ididendl;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();
}