• CF208E Blood Cousins [dsu on tree 树上启发式合并]


    倍增,然后数一下有几个深度为 (dep_u)

    // powered by c++11
    // by Isaunoya
    #include <bits/stdc++.h>
    #define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
    #define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
    using namespace std;
    using db = double;
    using ll = long long;
    using uint = unsigned int;
    #define Tp template
    using pii = pair<int, int>;
    #define fir first
    #define sec second
    Tp<class T> void cmax(T& x, const T& y) {if (x < y) x = y;} Tp<class T> void cmin(T& x, const T& y) {if (x > y) x = y;}
    #define all(v) v.begin(), v.end()
    #define sz(v) ((int)v.size())
    #define pb emplace_back
    Tp<class T> void sort(vector<T>& v) { sort(all(v)); } Tp<class T> void reverse(vector<T>& v) { reverse(all(v)); }
    Tp<class T> void unique(vector<T>& v) { sort(all(v)), v.erase(unique(all(v)), v.end()); }
    const int SZ = 1 << 23 | 233;
    struct FILEIN { char qwq[SZ], *S = qwq, *T = qwq, ch;
    #ifdef __WIN64
    #define GETC getchar
    #else
      char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
    #endif
      FILEIN& operator>>(char& c) {while (isspace(c = GETC()));return *this;}
      FILEIN& operator>>(string& s) {while (isspace(ch = GETC())); s = ch;while (!isspace(ch = GETC())) s += ch;return *this;}
      Tp<class T> void read(T& x) { bool sign = 0;while ((ch = GETC()) < 48) sign ^= (ch == 45); x = (ch ^ 48);
        while ((ch = GETC()) > 47) x = (x << 1) + (x << 3) + (ch ^ 48); x = sign ? -x : x;
      }FILEIN& operator>>(int& x) { return read(x), *this; } FILEIN& operator>>(ll& x) { return read(x), *this; }
    } in;
    struct FILEOUT {const static int LIMIT = 1 << 22 ;char quq[SZ], ST[233];int sz, O;
      ~FILEOUT() { flush() ; }void flush() {fwrite(quq, 1, O, stdout); fflush(stdout);O = 0;}
      FILEOUT& operator<<(char c) {return quq[O++] = c, *this;}
      FILEOUT& operator<<(string str) {if (O > LIMIT) flush();for (char c : str) quq[O++] = c;return *this;}
      Tp<class T> void write(T x) {if (O > LIMIT) flush();if (x < 0) {quq[O++] = 45;x = -x;}
    		do {ST[++sz] = x % 10 ^ 48;x /= 10;} while (x);while (sz) quq[O++] = ST[sz--];
      }FILEOUT& operator<<(int x) { return write(x), *this; } FILEOUT& operator<<(ll x) { return write(x), *this; }
    } out;
    #define int long long
    int n , q ;
    const int maxn = 1e5 + 51 ;
    vector < int > g[maxn] ;
    int fa[maxn] , ans[maxn] ;
    int f[maxn][22] ;
    vector < pair < int , int > > qr[maxn] ;
    int sz[maxn] , son[maxn] , dep[maxn] ;
    void dfs(int u) {
    	sz[u] = 1 ;
    	for(int v : g[u]) {
    		f[v][0] = u , dep[v] = dep[u] + 1 ;
    		dfs(v) ;
    		sz[u] += sz[v] ;
    		if(sz[v] > sz[son[u]])
    			son[u] = v ;
    	}
    }
    
    int vis[maxn] , sum[maxn] ;
    
    void upd(int dep , int qwq) {
    	sum[dep] += qwq ;
    }
    void add(int u , int qwq) {
    	upd(dep[u] , qwq) ;
    	for(int v : g[u]) 
    		if(! vis[v] && v ^ fa[u])
    			add(v , qwq) ;
    }
    void dfs(int u , int kep) {
    	for(int v : g[u])
    		if(v ^ son[u])
    			dfs(v , 0) ;
    	if(son[u]) {
    		dfs(son[u] , 1) ;
    		vis[son[u]] = 1 ;
    	}
    	add(u , 1) ;
    	vis[son[u]] = 0 ;
    	for(auto x : qr[u])
    		ans[x.second] = sum[x.first] - 1 ;
    	if(!kep) add(u , -1) ;
    }
    
    int kth(int x , int k) {
    	for(int i = 20 ; ~ i ; i --)
    		if(k & (1 << i))
    			x = f[x][i] ;
    	return x ;
    }
    signed main() {
      // code begin.
    	in >> n ;
    	rep(i , 1 , n) {
    		in >> fa[i] ;
    		g[fa[i]].pb(i) ;
    	}
    	rep(i , 1 , n) {
    		if(! fa[i])
    			dfs(i) ;
    	}
    	rep(j , 1 , 20)
    		rep(i , 1 , n) 
    			f[i][j] = f[f[i][j - 1]][j - 1] ;
    	in >> q ;
    	rep(i , 1 , q) {
    		int v , p ;
    		in >> v >> p ;
    		int u = kth(v , p) ;
    		qr[u].pb(dep[v] , i) ;
    	}
    	rep(i , 1 , n)
    		if(! fa[i]) dfs(i , 0) ;
    	rep(i , 1 , q) 
    		out << ans[i] << ' ' ;
    	out << '
    ' ;
    	return 0;
      // code end.
    }
    
  • 相关阅读:
    Redis学习笔记
    sysctl命令修改内核参数
    Spring Boot学习笔记
    抽象和接口
    FTP主动模式与被动模式
    JVM参数调优
    JVM性能调优工具
    性能调优工具
    Feign Hystrix Tomcat参数配置
    Zuul学习笔记
  • 原文地址:https://www.cnblogs.com/Isaunoya/p/12456754.html
Copyright © 2020-2023  润新知