File size: 2,134 Bytes
f7ba5f2 |
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 |
#include <iostream>
#include <vector>
using namespace std;
const int ALPHA_SIZE = 26;
struct node {
int count;
vector<node *> child;
node(int _count = 0) : count(_count), child(ALPHA_SIZE, nullptr) {}
void merge(node *n) {
count++;
for (int i = 0; i < ALPHA_SIZE; i++) {
if (n->child[i] != nullptr) {
if (child[i] == nullptr) {
child[i] = new node();
}
child[i]->merge(n->child[i]);
}
}
}
};
class trie {
node *root;
template <typename Func>
static void walk(node *n, Func f) {
f(n->count);
for (int i = 0; i < ALPHA_SIZE; i++) {
if (n->child[i] != nullptr) {
walk(n->child[i], f);
}
}
}
static void clean_up(node *n) {
for (int i = 0; i < ALPHA_SIZE; i++) {
if (n->child[i] != nullptr) {
clean_up(n->child[i]);
}
}
delete n;
}
public:
trie() : root(new node()) {}
~trie() { clean_up(root); }
void merge(node *n) { root->merge(n); }
template <typename Func>
void walk(Func f) const {
walk(root, f);
}
};
long long solve() {
int N, p;
char c;
cin >> N;
vector<vector<node>> tries(N);
for (int i = 0, M; i < N; i++) {
cin >> M;
auto &T = tries[i];
T.resize(M);
for (int j = 1; j < M; j++) {
cin >> p >> c;
T[--p].child[c - 'a'] = &T[j];
}
}
trie merged;
for (auto &T : tries) {
merged.merge(&T[0]);
}
auto n_choose_3 = [](long long n) {
return n < 3 ? 0 : n * (n - 1) * (n - 2) / 6;
};
long long ans = 0;
merged.walk([&](int k) {
// This node is in k tries. For how many out of all N choose 3 possible
// triples (T1, T2, T3) of tries is this node in at least one of the tries?
// The answer is N choose 3 minus the number of triples that doesn't have
// this node in any of the three tries, equal to (N - k) choose 3.
ans += n_choose_3(N) - n_choose_3(N - k);
});
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
cout << "Case #" << t << ": " << solve() << endl;
}
return 0;
}
|