CF 914E Palindromes in a Tree

题目描述

http://codeforces.com/problemset/problem/914/E

Solution

注意到字母只有二十个,可以状压

在计算答案的时候枚举是哪个 $1$ 即可

时间复杂度为 $O(20n\log n)$

细节比较多,首先统计答案要在两个端点都进行操作,所以我们可以先把所有点的贡献记下来然后在统计每个端点的答案的时候再把这个子树的贡献减掉

根节点的操作也有一些细节

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define maxn 200010
#define maxm 20
#define INF 1000000000
#define ll long long
#define pb push_back
using namespace std;

int n, m;

char s[maxn]; int w[maxn];

struct Edge {
int to, next, w;
} e[maxn * 2]; int c1, head[maxn];
inline void add_edge(int u, int v) {
e[c1].to = v; e[c1].next = head[u]; head[u] = c1++;
}

bool vis[maxn];
struct Calc_sz {
int sz[maxn], f[maxn], sum, rt;
void init() { f[rt = 0] = INF; }

void dfs_sz(int u, int fa) {
sz[u] = 1;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa || vis[v]) continue;
dfs_sz(v, u); sz[u] += sz[v];
}
}

void dfs(int u, int fa) {
f[u] = 0;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa || vis[v]) continue;
dfs(v, u); f[u] = max(f[u], sz[v]);
} f[u] = max(f[u], sum - sz[u]);
if (f[u] < f[rt]) rt = u;
}

inline int get_rt(int u) {
rt = 0; dfs_sz(u, 0); sum = sz[u]; dfs(u, 0); return rt;
}
} _;

int a[maxn], c2, dis[1 << maxm];
void Dfs(int u, int fa, int S) {
a[++c2] = S;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa || vis[v]) continue;
Dfs(v, u, S ^ w[v]);
}
}

ll ans[maxn];
ll dfs(int u, int fa, int S) {
ll sum = dis[S];
for (int i = 0; i < 20; ++i) sum += dis[S ^ 1 << i];
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa || vis[v]) continue;
sum += dfs(v, u, S ^ w[v]);
} ans[u] += sum;
return sum;
}

vector<int> I;
ll calc(int u, int fa, bool F) {
c2 = 0; Dfs(u, 0, w[u]); ll sum = 0;
if (F) {
for (int i = 1; i <= c2; ++i) ++dis[a[i]], I.pb(a[i]);
return 0;
}
for (int i = 1; i <= c2; ++i) --dis[a[i]];
sum = dfs(u, 0, w[u] ^ w[fa]);
for (int i = 1; i <= c2; ++i) ++dis[a[i]];
return sum;
}


void divide(int u) {
vis[u] = 1; dis[0] = 1;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (vis[v]) continue;
calc(v, u, 1);
} ll sum = dis[w[u]];
for (int i = 0; i < 20; ++i) sum += dis[w[u] ^ 1 << i];
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (vis[v]) continue;
sum += calc(v, u, 0);
} ans[u] += sum / 2; for (auto u : I) dis[u] = 0; I.clear();
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (vis[v]) continue;
divide(_.get_rt(v));
}
}

int main() { memset(head, -1, sizeof head);
cin >> n;
for (int i = 1; i < n; ++i) {
int x, y; scanf("%d%d", &x, &y);
add_edge(x, y); add_edge(y, x);
} scanf("%s", s + 1);
for (int i = 1; i <= n; ++i) w[i] = 1 << s[i] - 'a';
_.init(); divide(_.get_rt(1));
for (int i = 1; i <= n; ++i) printf("%I64d ", ans[i] + 1);
return 0;
}