• 「CF741D」Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths


    传送门
    Luogu

    解题思路

    考虑把22个字符状压下来,易知合法情况就是状态中之多有一个1,这个可以暴力一点判断23次。
    然后后就是 dsu on the tree 了。

    细节注意事项

    • 咕咕咕

    参考代码

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cctype>
    #include <cmath>
    #include <ctime>
    #define rg register
    using namespace std;
    template < typename T > inline void read(T& s) {
     	s = 0; int f = 0; char c = getchar();
     	while (!isdigit(c)) f |= (c == '-'), c = getchar();
     	while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
     	s = f ? -s : s;
    }
    
    const int _ = 500010;
    
    int tot, head[_], nxt[_ << 1], ver[_ << 1], w[_ << 1];
    inline void Add_edge(int u, int v, int d)
    { nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; }
    
    int n, dis[_], tg[1 << 22 | 2];
    int dep[_], siz[_], son[_], ans[_];
    int num, L[_], R[_], id[_];
    
    inline void dfs(int u, int f) {
    	siz[u] = 1, dep[u] = dep[f] + 1;
    	L[u] = ++num, id[num] = u;
    	for (rg int i = head[u]; i; i = nxt[i]) {
    		int v = ver[i]; if (v == f) continue;
    		dis[v] = dis[u] ^ w[i];
    		dfs(v, u), siz[u] += siz[v];
    		if (siz[v] > siz[son[u]]) son[u] = v;
    	}
    	R[u] = num;
    }
    
    inline void dfss(int u, int f, int keep) {
    	for (rg int i = head[u]; i; i = nxt[i]) {
    		int v = ver[i]; if (v == f || v == son[u]) continue;
    		dfss(v, u, 0), ans[u] = max(ans[u], ans[v]);
    	}
    	if (son[u]) dfss(son[u], u, 1), ans[u] = max(ans[u], ans[son[u]]);
    	if (tg[dis[u]]) ans[u] = max(ans[u], tg[dis[u]] - dep[u]);
    	for (rg int k = 0; k < 22; ++k)
    		if (tg[dis[u] ^ (1 << k)])
    			ans[u] = max(ans[u], tg[dis[u] ^ (1 << k)] - dep[u]);
    	tg[dis[u]] = max(tg[dis[u]], dep[u]);
    	for (rg int i = head[u]; i; i = nxt[i]) {
    		int v = ver[i]; if (v == son[u] || v == f) continue;
    		for (rg int x = L[v]; x <= R[v]; ++x) {
    			int p = id[x];
    			if (tg[dis[p]]) ans[u] = max(ans[u], tg[dis[p]] + dep[p] - 2 * dep[u]);
    			for (rg int k = 0; k < 22; ++k)
    				if (tg[dis[p] ^ (1 << k)]) ans[u] = max(ans[u], tg[dis[p] ^ (1 << k)] + dep[p] - 2 * dep[u]);
    		}
    		for (rg int x = L[v]; x <= R[v]; ++x)
    			tg[dis[id[x]]] = max(tg[dis[id[x]]], dep[id[x]]);
    	}
    	if (!keep) for (rg int x = L[u]; x <= R[u]; ++x) tg[dis[id[x]]] = 0;
    }
    
    int main() {
    #ifndef ONLINE_JUDGE
    	freopen("in.in", "r", stdin);
    #endif
    	read(n);
    	for (rg int p, i = 2; i <= n; ++i) {
    		read(p); char c = getchar();
    		while (!isalpha(c)) c = getchar();
    		Add_edge(p, i, 1 << (c - 'a'));
    		Add_edge(i, p, 1 << (c - 'a'));
    	}
    	dfs(1, 0), dfss(1, 0, 1);
    	for (rg int i = 1; i <= n; ++i)
    		printf("%d%c", ans[i], " 
    "[i == n]);
    	return 0;
    }
    

    完结撒花 (qwq)

  • 相关阅读:
    CF1119H
    oracle 第06章 数据字典
    oracle col命令
    oracle 第05章 SQL语句
    oracle 第04章 字符集
    oracle 第03章 体系结构
    oracle 第02章 基本操作
    oracle 第01章 安装
    oracle启动、关闭、重启脚本
    第十一周-学习进度条
  • 原文地址:https://www.cnblogs.com/zsbzsb/p/11746532.html
Copyright © 2020-2023  润新知