Codeforces 246 E Blood Cousins Return: DSU on Tree

题意:

给出一个森林,其中father=0的为根,会有多个根,每个点有一个name,有Q次询问,每次询问节点v的距离v为h的所有儿子的name有多少种。

题解:

DSU on Tree using map
O ( n l o g 2 n ) O(nlog^2n) O(nlog2n)

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int inf = 0x3f3f3f3f;

#define PB(x) push_back(x)
#define rep(i,l,r) for (ll i = l,_ = r;i< _;i++)
#define REP(i,l,r) for (ll i=l,_=r;i<=_;i++)
#define leave(x) do {cout<<#x<<endl;fflush(stdout);return 0;}while (0);
#define untie do{ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);}while (0)

/************* header ******************/
const int maxn = 2e5+100;
int first[maxn],nxt[maxn],des[maxn],tot,n,m;
int sz[maxn],dep[maxn],wson[maxn];
bool big[maxn];
unordered_map<int,int> cnt[maxn];
vector<pii> Query[maxn];
int ans[maxn];
map<string,int> id;int idcnt;
int a[maxn];
inline int get_id(const string & str){
    if (id.find(str) == id.end()){
        id[str] = ++idcnt;
    }
    return id[str];
}
inline void addEdge(int x,int y){
    tot ++;
    des[tot] = y;
    nxt[tot] = first[x];
    first[x] = tot;
}
void get_sz(int node,int depth){
    sz[node] = 1;
    dep[node] = depth;
    for (int t = first[node];t;t=nxt[t]){
        int v = des[t];
        get_sz(v,depth+1);
        sz[node] += sz[v];
        if (sz[v] > sz[wson[node]]){
            wson[node] = v;
        }
    }
}
void add(int node){
    cnt[dep[node]][a[node]] ++;
    for (int t = first[node];t;t=nxt[t]){
        int v = des[t];
        if (big[v])continue;
        add(v);
    }
}
void rem(int node){
    auto it = cnt[dep[node]].find(a[node]);
    it->second --;
    if (!it->second){
        cnt[dep[node]].erase(it);
    }
    for (int t = first[node];t;t=nxt[t]){
        int v = des[t];
        if (big[v])continue;
        rem(v);
    }
}
void dfs(int node,bool keep){
    for (int t = first[node];t;t=nxt[t]){
        int v = des[t];
        if (v == wson[node])continue;
        dfs(v,0);
    }
    if (wson[node]){
        big[wson[node]] = 1;
        dfs(wson[node],1);
    }
    add(node);
    rep(i,0,Query[node].size()){
        ans[Query[node][i].second] = cnt[dep[node] + Query[node][i].first].size();
    }
    if (wson[node]) big[wson[node]] = 0;
    if (!keep)rem(node);
}
int main(){
    untie;
    cin>>n;
    REP(i,1,n){
        string name;
        int fa;
        cin>>name>>fa;
        a[i] = get_id(name);
        if (fa == 0){
            fa = n+1;
        }
        addEdge(fa,i);
    }
    cin>>m;
    REP(i,1,m){
        int v,h;
        cin>>v>>h;
        Query[v].push_back({h,i});
    }
    get_sz(n+1,1);
    dfs(n+1,0);
    REP(i,1,m){
        cout<<ans[i]<<'\n';
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值