Problem:
Similarity of Subtrees
Define the depth of a node in a rooted tree by applying the following rules recursively:
The depth of a root node is 0.
The depths of child nodes whose parents are with depth $d$ are $d + 1$.
Let $S(T, d)$ be the number of nodes of $T$ with depth $d$. Two rooted trees $T$ and $T'$ are similar if and only if $S(T, d)$ equals $S(T', d)$ for all non-negative integer $d$.
You are given a rooted tree $T$ with $N$ nodes. The nodes of $T$ are numbered from 1 to $N$. Node 1 is the root node of $T$. Let $T_i$ be the rooted subtree of $T$ whose root is node $i$. Your task is to write a program which calculates the number of pairs $(i, j)$ such that $T_i$ and $T_j$ are similar and $i < j$.
Input
The input consists of a single test case.
$N$
$a_1$ $b_1$
$a_2$ $b_2$
...
$a_{N-1}$ $b_{N-1}$
The first line contains an integer $N$ ($1 \leq N \leq 100,000$), which is the number of nodes in a tree. The following $N -1$ lines give information of branches: the $i$-th line of them contains $a_i$ and $b_i$, which indicates that a node $a_i$ is a parent of a node $b_i$. ($1 \leq a_i, b_i \leq N, a_i \ne b_i$) The root node is numbered by 1. It is guaranteed that a given graph is a rooted tree, i.e. there is exactly one parent for each node except the node 1, and the graph is connected.
Output
Print the number of the pairs $(x, y)$ of the nodes such that the subtree with the root $x$ and the subtree with the root $y$ are similar and $x < y$.
Sample Input 1
5
1 2
1 3
1 4
1 5
Output for the Sample Input 1
6
Sample Input 2
6
1 2
2 3
3 4
1 5
5 6
Output for the Sample Input 2
2
Sample Input 3
13
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
6 10
7 11
8 12
11 13
Output for the Sample Input 3
14

Submitted solutions:

code_snippet: #include <bits/stdtr1c++.h>

#define MAX 100100
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)
#define dbg(x) cout << #x << " = " << x << endl

using namespace std;

const unsigned long long base = 666666667;

int n, depth[MAX];
vector <int> adj[MAX];
unsigned long long dp[MAX], power[MAX];
tr1::unordered_map <unsigned long long, int> mp;

void dfs(int i, int p){
    depth[i] = 1;
    int j, k, d, x, len = adj[i].size();

    for (j = 0; j < len; j++){
        x = adj[i][j];
        if (x != p){
            dfs(x, i);
            depth[i] = max(depth[i], depth[x] + 1);
        }
    }

    d = depth[i] - 1;
    unsigned long long h = 0;
    for (j = 0; j < len; j++){
        x = adj[i][j];
        if (x != p){
            unsigned long long r = dp[x] * power[d - depth[x]];
            h += r;
        }
    }

    h += power[depth[i]];
    dp[i] = h;
    mp[h]++;
}

int main(){
    int i, j, k, l, a, b;

    power[0] = 1;
    for (i = 1; i < MAX; i++) power[i] = power[i - 1] * base;

    while (scanf("%d", &n) != EOF){
        mp.clear();
        for (i = 0; i < MAX; i++) adj[i].clear();

        for (i = 1; i < n; i++){
            scanf("%d %d", &a, &b);
            adj[a].push_back(b);
            adj[b].push_back(a);
        }

        dfs(1, 1);
        long long res = 0;
        for (auto it: mp){
            long long x = it.second;
            res += ((x * (x - 1)) >> 1);
        }

        printf("%lld\n", res);
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 24036
final_rank: 3

code_snippet: #include <iostream>
#include <random>
#include <vector>
#include <algorithm>
struct Mint {
    using Value = unsigned long long;
    static constexpr Value MOD{(1uLL << 61) - 1};
    static constexpr Value MASK30{(1ull << 30) - 1};
    static constexpr Value MASK31{(1ull << 31) - 1};
    constexpr Mint() {}
    static constexpr Value Mod() noexcept {
        return MOD;
    }
    static constexpr Value Modulo(Value v) noexcept {
        Value res{(v >> 61) + (v & MOD)};
        res = (res >= MOD ? res - MOD : res);
        return res;
    }
    static constexpr Value UnsafeMul(Value a, Value b) noexcept {
        Value fa{a >> 31}, fb{b >> 31};
        Value ba{a & MASK31}, bb{b & MASK31};
        Value mid{fa * bb + fb * ba};
        return Value{2} * fa * fb + (mid >> 30) + ((mid & MASK30) << 31) + ba * bb;
    }
    static constexpr Value Mul(Value a, Value b) noexcept {
        return Modulo(UnsafeMul(a, b));
    }
};
const unsigned long long B{std::mt19937{std::random_device{}()}() % Mint::Mod()};
int N;
std::vector<int> G[100010];
long long solve() {
    std::vector<long long> hash(N);
    auto rec{[&](auto rec, int v, int p) -> long long {
        long long h{};
        for (auto x : G[v]) if (x != p) {
            h = Mint::Modulo(h + rec(rec, x, v));
        }
        h = Mint::Mul(h, B);
        h = Mint::Modulo(h + 1);
        return hash[v] = h;
    }};
    rec(rec, 0, -1);
    //for (auto h : hash) std::cout << h << ' ';
    //std::cout << std::endl;
    std::sort(hash.begin(), hash.end());
    long long ans{};
    for (int i{}, j{} ; i < N ; i = j) {
        while (j < N and hash[i] == hash[j]) j++;
        ans += (long long)(j - i) * (j - i - 1) / 2;
    }
    return ans;
}
int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    std::cin >> N;
    for (int i{} ; i < N - 1 ; i++) {
        int a, b;
        std::cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    std::cout << solve() << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 18660
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

namespace Random {
mt19937_64 randgen(chrono::steady_clock::now().time_since_epoch().count());
using u64 = unsigned long long;
u64 get() {
    return randgen();
}
template <typename T> T get(T L) { // [0,L]

    return get() % (L + 1);
}
template <typename T> T get(T L, T R) { // [L,R]

    return get(R - L) + L;
}
double uniform() {
    return double(get(1000000000)) / 1000000000;
}
string str(int n) {
    string ret;
    rep(i, 0, n) ret += get('a', 'z');
    return ret;
}
template <typename Iter> void shuffle(Iter first, Iter last) {
    if (first == last)
        return;
    int len = 1;
    for (auto it = first + 1; it != last; it++) {
        len++;
        int j = get(0, len - 1);
        if (j != len - 1)
            iter_swap(it, first + j);
    }
}
template <typename T> vector<T> select(int n, T L, T R) { // [L,R]

    if (n * 2 >= R - L + 1) {
        vector<T> ret(R - L + 1);
        iota(ALL(ret), L);
        shuffle(ALL(ret));
        ret.resize(n);
        return ret;
    } else {
        unordered_set<T> used;
        vector<T> ret;
        while (SZ(used) < n) {
            T x = get(L, R);
            if (!used.count(x)) {
                used.insert(x);
                ret.push_back(x);
            }
        }
        return ret;
    }
}

void relabel(int n, vector<pair<int, int>> &es) {
    shuffle(ALL(es));
    vector<int> ord(n);
    iota(ALL(ord), 0);
    shuffle(ALL(ord));
    for (auto &[u, v] : es)
        u = ord[u], v = ord[v];
}
template <bool directed, bool simple> vector<pair<int, int>> genGraph(int n) {
    vector<pair<int, int>> cand, es;
    rep(u, 0, n) rep(v, 0, n) {
        if (simple and u == v)
            continue;
        if (!directed and u > v)
            continue;
        cand.push_back({u, v});
    }
    int m = get(SZ(cand));
    vector<int> ord;
    if (simple)
        ord = select(m, 0, SZ(cand) - 1);
    else {
        rep(_, 0, m) ord.push_back(get(SZ(cand) - 1));
    }
    for (auto &i : ord)
        es.push_back(cand[i]);
    relabel(n, es);
    return es;
}
vector<pair<int, int>> genTree(int n) {
    vector<pair<int, int>> es;
    rep(i, 1, n) es.push_back({get(i - 1), i});
    relabel(n, es);
    return es;
}
}; // namespace Random


/**
 * @brief Random
 */

int main() {
    int N;
    cin >> N;
    vector<vector<int>> G(N);
    rep(i,0,N-1) {
        int A, B;
        cin >> A >> B;
        A--, B--;
        G[A].push_back(B);
        G[B].push_back(A);
    }
    vector<ll> Hash(N);
    ll base = Random::get((ull)1e8,(ull)1e9);
    ll MOD = 998244353;
    auto DFS = [&](auto self, int V, int P) -> ll {
        ll Ret = 0;
        for(int NV : G[V]) {
            if (NV == P) continue;
            Ret += self(self,NV,V);
            Ret %= MOD;
        }
        Ret *= base;
        Ret++;
        return Hash[V] = Ret % MOD;
    };
    DFS(DFS,0,-1);
    map<ll,int> mp;
    ll ANS = 0;
    rep(i,0,N) {
        ANS += mp[Hash[i]];
        mp[Hash[i]]++;
    }
    cout << ANS << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 26584
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> pi;
typedef pair<ll,pi> ppi;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
int main(){
    ll N;cin>>N;
    vvi E(N);
    REP(i,N-1){
        int a,b;cin>>a>>b;a--;b--;
        E[a].emplace_back(b);
        E[b].emplace_back(a);
    }
    srand(time(NULL));
    ll base=rand()%rand();
    const vi mod={998244353,1000000007,1000000009};
    vvi pow(3,vi(2e6,1));
    FOR(i,1,2e6){
        REP(j,3)pow[j][i]=pow[j][i-1]*(base%mod[j])%mod[j];
    }
    vi S(N,1),D(N),max_depth(N),P(N);
    function<void(ll)>dfs=[&](ll v){
        for(auto u:E[v])if(u!=P[v]){
            P[u]=v;
            D[u]=D[v]+1;
            dfs(u);
            S[v]+=S[u];
            max_depth[v]=max(max_depth[u]+1,max_depth[v]);
        }
    };
    map<tuple<ll,ll,ll,ll,ll>,ll>memo;
    dfs(0);
    vvi hash(3,vi(N));
    function<void(ll)>dfs2=[&](ll v){
        REP(j,3)hash[j][v]=1;
        for(auto u:E[v])if(u!=P[v]){
            dfs2(u);
            REP(j,3)hash[j][v]+=hash[j][u]*(base%mod[j])%mod[j];
            REP(j,3)hash[j][v]%=mod[j];
        }
        memo[make_tuple(S[v],max_depth[v],hash[0][v],hash[1][v],hash[2][v])]++;
    };
    dfs2(0);
    ll ans=0;
    for(auto[k,v]:memo)ans+=v*(v-1)/2;
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 81172
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;

int main(){

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N;
    cin>>N;
    vector<vector<ll>> G(N);
    for(int i=0;i<N-1;i++){
        int u,v;
        cin>>u>>v;
        u--;v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    ll VN=10;
    vector<ll> BS(VN),MD(VN);
    for(int i=0;i<VN;i++)BS[i]=rand()%1000+1000;
    for(int i=0;i<VN;i++)MD[i]=rand()%100000+1e9;
    vector<vector<ll>> C(N,vector<ll>(VN,1));
    map<vector<ll>,ll> MP;
    ll an=0;
    auto dfs = [&](auto dfs, int n,int p) -> void {
        for (int v : G[n]) {
            if(v!=p){
                dfs(dfs,v,n);
                for(int i=0;i<VN;i++){
                    C[n][i]+=(C[v][i]*BS[i])%MD[i];
                    C[n][i]%=MD[i];
                }
            }
        }
        an+=MP[C[n]];
        MP[C[n]]++;
    };
    dfs(dfs,0,-1);

    cout<<an<<endl;    
    
}
accuracy: 1
time_ms: 90
memory_kb: 53024
final_rank: 14

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

int main() {
    int N = in();
    vector<vector<int>> tree(N);
    for(int i : rep(N - 1)) {
        int u = in(), v = in(); u--, v--;
        tree[u].push_back(v);
        tree[v].push_back(u);
    }

    const int M = 2;
    using hash = array<i64, M>;
    const hash mod = {int(1e9) + 7, int(1e9) + 9};
    const i64 b = 10007;
    map<hash, int> mp;
    auto dfs = [&](auto self, int v, int p) -> hash {
        hash sum = {};
        for(int to : tree[v]) if(to != p) {
            auto ch = self(self, to, v);
            for(int i : rep(M)) sum[i] = (sum[i] + ch[i]) % mod[i];
        }
        for(int i : rep(M)) sum[i] = (sum[i] * b + 1) % mod[i];
        mp[sum] += 1;
        return sum;
    };
    dfs(dfs, 0, -1);

    i64 ans = 0;
    for(auto [key, value] : mp) ans += i64(value) * (value - 1) / 2;
    print(ans);
}
accuracy: 1
time_ms: 40
memory_kb: 28964
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define foa(s, v) for(auto &s : v)
#define all(v) v.begin(), v.end()
#define REPname(a,b,c,d,...) d
#define rep(...) REPname(__VA_ARGS__, REP2, REP1, REP0)(__VA_ARGS__)
#define REP2(i,l,r) for(int i = l; i < r; i++)
#define REP1(i, x) REP2(i,0,x)
#define REP0(x) REP1(SPJ, x)
#define sz(x) int(x.size())

template <class T>
using V=vector<T>;

template <class T>
using VV=vector<V<T>>;

template<class T>
using pqmin = priority_queue<T, V<T>, greater<T>>;
using ll = long long ;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = V<vll>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
template<class T>
inline bool chmax(T &a, T b){
	return a < b ? a=b, 1 : 0;
}
template <class T>
inline bool chmin(T &a, T b) {
	return a > b ? a = b, 1 : 0;
}

template <class T>
void view(T x) {
	cerr << x;
}

template <class T>
void view(V<T> v) {
	cerr << "{ ";
	foa(t, v) {view(t) ; cerr << ", ";}
	cerr << "}";
	cerr << endl;
}


template <class T>
void view(VV<T> v) {
	cerr << "{ ";
	foa(t, v) {view(t) ; cerr << ",\n";}
	cerr << "}";
	cerr << endl;
}


// template <c0lass T>
void view(int x) {
	cerr << x;
}

#ifdef LOCAL
void debug_out() {}
template <class T>
void debug_out(T x) {
	view(x);
}
template <class H, class... T>
void debug_out(H h, T... t) {
	view(h);
	cerr << ", ";
	debug_out(t...);
}
#define debug(...)                                           \
	do {                                                     \
		cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                              \
		cerr << "]\n";                                       \
	} while(0)
#else
#define debug(...) (void(0))
#endif
using vvi = V<vi>;

struct uf{
	vector<int> dat;
	uf(int n) : dat(n, -1) {}
	int root(int x) 
	{
		int& p = dat[x];
		if(p < 0) return x;
		return p = root(p);
	}
	bool merge(int x, int y) {
		x = root(x);
		y = root(y);
		if(x == y) return false;
		if(-dat[x] < -dat[y]) swap(x, y);
		dat[x] += dat[y];
		dat[y]  = x;
		return true;
	}
};

ll modpow(ll x, ll n, ll md){
	ll ret = 1 % md;
	while(n > 0){
		if(n & 1) {ret *= x; ret %= md;}
		n >>= 1;
		x *= x;
		x %= md;
	}
	if(ret < 0) ret += abs(md);
	return ret;
}

using bint = __int128_t;

ll solve(int n){
	vector<vi> g(n);
	rep(n-1) {
		int a, b; cin >> a >> b;
		a--;
		b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}


	vector<bint> hs(n, 0);
	const bint base = 10231437;
	const bint mod = (1LL << 61) - 1;

	auto dfs = [&](int now, int par, auto self) -> void {
		bint& tmp = hs[now];
		foa(nxt, g[now]) {
			if(nxt == par) continue;
			self(nxt, now, self);
			tmp += hs[nxt];
		}
		tmp *= base;
		tmp += 1;
		tmp %= mod;
		return;
	};

	dfs(0, -1, dfs);

	map<bint, ll> m;
	rep(i, n) m[hs[i]] ++ ;
	ll ans = 0;
	foa(t, m) {
		ans += (t.second - 1) * t.second / 2;
	}

	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(15);
	srand((unsigned)time(NULL));
	int n;
	while(cin >> n && n) cout << solve(n) << '\n';
}
accuracy: 1
time_ms: 40
memory_kb: 27224
final_rank: 7

code_snippet: #include <stdio.h>
#include <vector>
#include <map>
#define M1 1030000001
#define M2 1012345693
#define X1 103001
#define X2 123457
using namespace std;

vector<int> ed[103000];
map<pair<int, int>, int> mp;

pair<long long, long long> makehash(int x, int p) {
    pair<long long, long long> pp, t;
    pp.first = 1;
    pp.second = 1;
    for (int i : ed[x]) {
        if (i == p) continue;
        t = makehash(i, x);
        pp.first = (pp.first + t.first * X1) % M1;
        pp.second = (pp.second + t.second * X2) % M2;
    }
    if (mp.find(pp) != mp.end()) mp[pp]++;
    else mp.insert({ pp, 1 });
    return pp;
}

int main(void) {
    int n, a, b;
    long long r = 0;
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
        scanf("%d %d", &a, &b);
        ed[a].push_back(b);
        ed[b].push_back(a);
    }
    makehash(1, -1);
    for (auto& i : mp) {
        r += i.second * (i.second - 1LL) / 2;
    }
    printf("%lld\n", r);
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 22564
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

constexpr int mod = 998244353,mod2 = 1000000007;

map<pair<int,int>,int>mp;

pair<int,int> dfs(int n,int p,vector<vector<int>>&ki) {
    int sum = 0,sum2 = 0;
    for(int i:ki[n]) {
        if(i == p) {
            continue;
        }
        pair<int,int> a = dfs(i,n,ki);
        sum += 1ll*a.first*1000003%mod;
        sum2 += 1ll*a.second*1000003%mod2;
        if(sum >= mod) sum -= mod;
        if(sum2 >= mod2) sum2 -= mod2;
    }
    sum += 1;
    if(sum >= mod) sum -= mod;
    sum2 += 1;
    if(sum2 >= mod) sum2 -= mod;
    mp[{sum,sum2}]++;
    return {sum,sum2};
}

int main() {
    int N;
    cin >> N;
    vector<vector<int>>ki(N);
    for(int i = 0; i < N-1; i++) {
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        ki[a].push_back(b);
        ki[b].push_back(a);
    }
    dfs(0,-1,ki);
    long long ans = 0;
    for(auto i:mp) {
        ans += 1ll*i.second*(i.second-1)/2;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 22504
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

constexpr int mod = 998244353,mod2 = 1000000007;

map<pair<int,int>,int>mp;

pair<int,int> dfs(int n,int p,vector<vector<int>>&ki) {
    int sum = 0,sum2 = 0;
    for(int i:ki[n]) {
        if(i == p) {
            continue;
        }
        pair<int,int> a = dfs(i,n,ki);
        sum += 1ll*a.first*10%mod;
        sum2 += 1ll*a.second*10%mod2;
        if(sum >= mod) sum -= mod;
        if(sum2 >= mod2) sum2 -= mod2;
    }
    sum += 1;
    if(sum >= mod) sum -= mod;
    sum2 += 1;
    if(sum2 >= mod) sum2 -= mod;
    mp[{sum,sum2}]++;
    return {sum,sum2};
}

int main() {
    int N;
    cin >> N;
    vector<vector<int>>ki(N);
    for(int i = 0; i < N-1; i++) {
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        ki[a].push_back(b);
        ki[b].push_back(a);
    }
    dfs(0,-1,ki);
    long long ans = 0;
    for(auto i:mp) {
        ans += 1ll*i.second*(i.second-1)/2;
    }
    cout << ans << endl;
}
accuracy: 0.12
time_ms: 10
memory_kb: 5128
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

constexpr int mod = 998244353;

map<int,int>mp;

int dfs(int n,int p,vector<vector<int>>&ki) {
    int sum = 0;
    for(int i:ki[n]) {
        if(i == p) {
            continue;
        }
        sum += 1ll*dfs(i,n,ki)*10%mod;
        if(sum >= mod) sum -= mod;
    }
    sum += 1;
    if(sum >= mod) sum -= mod;
    mp[sum]++;
    return sum;
}

int main() {
    int N;
    cin >> N;
    vector<vector<int>>ki(N);
    for(int i = 0; i < N-1; i++) {
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        ki[a].push_back(b);
        ki[b].push_back(a);
    }
    dfs(0,-1,ki);
    long long ans = 0;
    for(auto i:mp) {
        ans += 1ll*i.second*(i.second-1)/2;
    }
    cout << ans << endl;
}
accuracy: 0.12
time_ms: 10
memory_kb: 5132
final_rank: 18

code_snippet: #include <cstdio>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

const int m0 = 1e9 + 7;
const int m1 = 1e9 + 9;

struct hv {
  int v0, v1;
  hv operator + (const hv& other) const {
    return hv{(v0 + other.v0)%m0, (v1 + other.v1)%m1};
  }
  hv operator * (const hv& other) const {
    return hv{(int)(v0 * 1ll * other.v0%m0), (int)(v1 * 1ll * other.v1%m1)};
  }
  bool operator == (const hv& other) const {
    return v0 == other.v0 && v1 == other.v1; 
  }
};

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<int>> g(n + 1);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
  }
  vector<hv> pw(n + 1);
  pw[0] = hv{1, 1};
  hv c = {171, 171};
  for (int i = 1; i <= n; i++) pw[i] = pw[i - 1] * c;
  vector<hv> val(n + 1);
  
  function<void(int)> dfs = [&](int u) {
    int find = 0;
    val[u] = hv{1, 1} * pw[0];
    for (int v: g[u]) {
      dfs(v);
      auto add = val[v] * c;
      val[u] = val[u] + add;
    }
    //printf("val[%d] = %d, %d\n", u, val[u].v0, val[u].v1);
  };
  
  dfs(1);
  vector<hv> all;
  for (int i = 1; i <= n; i++) {
    all.push_back(val[i]); 
  }
  sort(all.begin(), all.end(), [](auto u, auto v) {
    if (u.v0 != v.v0) return u.v0 < v.v0;
    return u.v1 < v.v1; 
  });
  long long ans = 0;
  int p = 0;
  while (p < n) {
    int np = p;
    while (np + 1 < n && all[np + 1] == all[p]) np++;
    int cnt = np - p + 1;
    ans += cnt * 1ll * (cnt - 1) / 2;
    p = np + 1;
  } 
  printf("%lld\n", ans);
  return 0; 
}
accuracy: 1
time_ms: 20
memory_kb: 19968
final_rank: 2

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;

    void print();
    template <class t> void print(const vector<t> &);
    template <class t, class u> void print(const pair<t, u> &);
    template <class t> void print(const t&);
    template <class Head, class... Tail> void print(const Head&, const Tail&...);

    template <class t> void println(const vector<vector<t>>&);
    template <class t> void println(const vector<t>&);
    template <class t> void println(const t&);
    template <class Head, class... Tail> void println(const Head&, const Tail&...);
    void println();
    void newline();

    void print(){
        return;
    }

    template <class t>
    void print(const vector<t>&x){
        for(const t&i:x)print(i);
    }
    template <class t, class u>
    void print(const pair<t,u>&p){
        print(p.first);
        print(p.second);
    }
    template <class t>
    void print(const t&x){
        if(was_output)cout<<" ";
        cout<<x;
        was_output = true;
    }
    template <class Head, class... Tail>
    void print(const Head&head,const Tail&...tail){
        print(head);
        print(tail...);
    }

    template<class t>
    void println(const vector<vector<t>>&x){
        for(vector<t> i:x)println(i);
    }
    template<class t>
    void println(const vector<t>&x){
        for(const t&i:x)print(i);
        println();
    }
    template <class t>
    void println(const t&x){
        print(x);
        println();
    }
    void println(){
        if(was_output){
            cout << endl;
            was_output = false;
        }
    }
    template <class Head, class... Tail>
    void println(const Head&head,const Tail&...tail){
        print(head);
        println(tail...);
    }

    void newline(){
        was_output = true;
        println();
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

    template<class t>
    ostream& operator<<(ostream&os, vector<t> &v){
        os << "{";
        for(t &i:v){
            os << i << ", ";
        }
        os << "}";
        return os;
    }

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

    long long lcm(long long a,long long b){
        return a / gcd(a,b) * b;
    }

    class output_initializer{
    public:
        output_initializer(){
            ios::sync_with_stdio(false);
            cin.tie(nullptr);
            cout << setprecision(20);
        }
    };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();
}

using namespace templates;

template<long long mod>
class modint{
	public:
		long long x;
		modint(long long a){x=a%mod;if(x<0)x+=mod;}
		modint(){x=0;}

		modint pow(long long a){
			modint res(1), b(x);
			while(a){
				if(a&1)res*=b;
				b*=b;
				a>>=1;
			}
			return res;
		}

		modint inv(){return pow(mod-2);}

		modint& operator+=(modint a){x=(x+a.x)%mod;return *this;}
		modint& operator-=(modint a){x=x-a.x;if(x<0)x+=mod;return *this;}
		modint& operator*=(modint a){x=x*a.x%mod;return *this;}
		modint& operator/=(modint a){x=x*a.inv().x%mod;return *this;}

		modint operator+(modint a){return modint(x)+=a;}
		modint operator-(modint a){return modint(x)-=a;}
		modint operator*(modint a){return modint(x)*=a;}
		modint operator/(modint a){return modint(x)/=a;}

		modint operator-(){return modint(x);}

		bool operator==(const modint a){return x == a.x;}
		bool operator<(const modint a){return x < a.x;}
		bool operator>(const modint a){return x > a.x;}
};

template<long long mod>
ostream& operator<<(ostream& os, const modint<mod>& a){
	os << a.x;
	return os;
}

using M1 = modint<1000000007>;
using M2 = modint<1000000009>;

class ModHash{
public:
    M1 v1;
    M2 v2;
    ModHash():ModHash(0){}
    ModHash(long long v):v1(v),v2(v){}
    ModHash(M1 v1,M2 v2):v1(v1),v2(v2){}
    ModHash(const ModHash &that):v1(that.v1),v2(that.v2){}
    ModHash operator+(ModHash that){
        return ModHash(v1+that.v1,v2+that.v2);
    }
    ModHash operator-(ModHash that){
        return ModHash(v1-that.v1,v2-that.v2);
    }
    ModHash operator*(ModHash that){
        return ModHash(v1*that.v1,v2*that.v2);
    }
};

bool operator<(ModHash x,ModHash y){
    return x.v1 < y.v1 or
        (x.v1 == y.v1 and
         x.v2 < y.v2);
}

ll func(){
    int n = in();
    vvector<pii> edges(n);
    rep(_,n-1){
        int a = in()-1;
        int b = in()-1;
        edges[a].emplace_back(b,edges[b].size());
        edges[b].emplace_back(a,edges[a].size()-1);
    }
    vvector<ModHash> dp(n);
    vvector<int> used(n);
    ModHash B = ModHash(11,13);
    rep(i,n)dp[i].resize(edges[i].size()+1);
    rep(i,n)used[i].resize(edges[i].size()+1,false);
    method(rec,ModHash,int p,int last){
        int itr = last < 0 ? dp[p].size()-1 : last;
        if(used[p][itr])return dp[p][itr];
        used[p][itr] = true;
        ModHash &it = dp[p][itr];
        if(last < 0){
            ModHash sum = 1;
            foreach(e,edges[p]){
                sum = sum + rec(e.first,e.second) * 11;
            }
            rep(i,edges[p].size()){
                dp[p][i] = sum - rec(edges[p][i].first,edges[p][i].second) * B;
            }
            it = sum;
        }else{
            it = 1;
            rep(i,edges[p].size()){
                if(i==last)continue;
                it = it + rec(edges[p][i].first,edges[p][i].second) * B;
            }
        }
        return it;
    };
    map<ModHash,int> m;
    ll res = 0;
    method(solve,ll,int p,int last){
        ll res = 0;
        ModHash v = rec(p,last);
        res += m[v];
        ++m[v];
        rep(i,edges[p].size()){
            if(i==last)continue;
            res += solve(edges[p][i].first,edges[p][i].second);
        }
        return res;
    };
    return solve(0,-1);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    println(func());

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 42876
final_rank: 13

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;

    void print();
    template <class t> void print(const vector<t> &);
    template <class t, class u> void print(const pair<t, u> &);
    template <class t> void print(const t&);
    template <class Head, class... Tail> void print(const Head&, const Tail&...);

    template <class t> void println(const vector<vector<t>>&);
    template <class t> void println(const vector<t>&);
    template <class t> void println(const t&);
    template <class Head, class... Tail> void println(const Head&, const Tail&...);
    void println();
    void newline();

    void print(){
        return;
    }

    template <class t>
    void print(const vector<t>&x){
        for(const t&i:x)print(i);
    }
    template <class t, class u>
    void print(const pair<t,u>&p){
        print(p.first);
        print(p.second);
    }
    template <class t>
    void print(const t&x){
        if(was_output)cout<<" ";
        cout<<x;
        was_output = true;
    }
    template <class Head, class... Tail>
    void print(const Head&head,const Tail&...tail){
        print(head);
        print(tail...);
    }

    template<class t>
    void println(const vector<vector<t>>&x){
        for(vector<t> i:x)println(i);
    }
    template<class t>
    void println(const vector<t>&x){
        for(const t&i:x)print(i);
        println();
    }
    template <class t>
    void println(const t&x){
        print(x);
        println();
    }
    void println(){
        if(was_output){
            cout << endl;
            was_output = false;
        }
    }
    template <class Head, class... Tail>
    void println(const Head&head,const Tail&...tail){
        print(head);
        println(tail...);
    }

    void newline(){
        was_output = true;
        println();
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

    template<class t>
    ostream& operator<<(ostream&os, vector<t> &v){
        os << "{";
        for(t &i:v){
            os << i << ", ";
        }
        os << "}";
        return os;
    }

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

    long long lcm(long long a,long long b){
        return a / gcd(a,b) * b;
    }

    class output_initializer{
    public:
        output_initializer(){
            ios::sync_with_stdio(false);
            cin.tie(nullptr);
            cout << setprecision(20);
        }
    };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();
}

using namespace templates;

template<long long mod>
class modint{
	public:
		long long x;
		modint(long long a){x=a%mod;if(x<0)x+=mod;}
		modint(){x=0;}

		modint pow(long long a){
			modint res(1), b(x);
			while(a){
				if(a&1)res*=b;
				b*=b;
				a>>=1;
			}
			return res;
		}

		modint inv(){return pow(mod-2);}

		modint& operator+=(modint a){x=(x+a.x)%mod;return *this;}
		modint& operator-=(modint a){x=x-a.x;if(x<0)x+=mod;return *this;}
		modint& operator*=(modint a){x=x*a.x%mod;return *this;}
		modint& operator/=(modint a){x=x*a.inv().x%mod;return *this;}

		modint operator+(modint a){return modint(x)+=a;}
		modint operator-(modint a){return modint(x)-=a;}
		modint operator*(modint a){return modint(x)*=a;}
		modint operator/(modint a){return modint(x)/=a;}

		modint operator-(){return modint(x);}

		bool operator==(const modint a){return x == a.x;}
		bool operator<(const modint a){return x < a.x;}
		bool operator>(const modint a){return x > a.x;}
};

template<long long mod>
ostream& operator<<(ostream& os, const modint<mod>& a){
	os << a.x;
	return os;
}

using M1 = modint<1000000007>;
using M2 = modint<1000000009>;

class ModHash{
public:
    M1 v1;
    M2 v2;
    ModHash():ModHash(0){}
    ModHash(long long v):v1(v),v2(v){}
    ModHash(M1 v1,M2 v2):v1(v1),v2(v2){}
    ModHash(const ModHash &that):v1(that.v1),v2(that.v2){}
    ModHash operator+(ModHash that){
        return ModHash(v1+that.v1,v2+that.v2);
    }
    ModHash operator-(ModHash that){
        return ModHash(v1-that.v1,v2-that.v2);
    }
    ModHash operator*(ModHash that){
        return ModHash(v1*that.v1,v2*that.v2);
    }
};

bool operator<(ModHash x,ModHash y){
    return x.v1 < y.v1 or
        (x.v1 == y.v1 and
         x.v2 < y.v2);
}

ll func(){
    int n = in();
    vvector<pii> edges(n);
    rep(_,n-1){
        int a = in()-1;
        int b = in()-1;
        edges[a].emplace_back(b,edges[b].size());
        edges[b].emplace_back(a,edges[a].size()-1);
    }
    vvector<ModHash> dp(n);
    vvector<int> used(n);
    rep(i,n)dp[i].resize(edges[i].size()+1);
    rep(i,n)used[i].resize(edges[i].size()+1,false);
    method(rec,ModHash,int p,int last){
        int itr = last < 0 ? dp[p].size()-1 : last;
        if(used[p][itr])return dp[p][itr];
        used[p][itr] = true;
        ModHash &it = dp[p][itr];
        if(last < 0){
            ModHash sum = 1;
            foreach(e,edges[p]){
                sum = sum + rec(e.first,e.second) * 11;
            }
            rep(i,edges[p].size()){
                dp[p][i] = sum - rec(edges[p][i].first,edges[p][i].second) * 11;
            }
            it = sum;
        }else{
            it = 1;
            rep(i,edges[p].size()){
                if(i==last)continue;
                it = it + rec(edges[p][i].first,edges[p][i].second) * 11;
            }
        }
        return it;
    };
    map<ModHash,int> m;
    ll res = 0;
    method(solve,ll,int p,int last){
        ll res = 0;
        ModHash v = rec(p,last);
        res += m[v];
        ++m[v];
        rep(i,edges[p].size()){
            if(i==last)continue;
            res += solve(edges[p][i].first,edges[p][i].second);
        }
        return res;
    };
    return solve(0,-1);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    println(func());

    return 0;
}
accuracy: 0.14
time_ms: 30
memory_kb: 17604
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (int i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"YES\n"; else cout<<"NO\n";}


void solve();

//  rainy ~ 雨に打たれて ~
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	solve();
}

void solve(){
	int N;
	cin>>N;
	vector<vector<int>> G(N);
	rep(i,N-1){
		int a,b;
		cin>>a>>b;
		a--,b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	vector<vector<ll>> dp(N,vector<ll>(3));
	vector<ll> M={(1ll<<61)-1,998244353,924924167};
	vector<ll> q={2,3,5};
	vector<int> order={0},seen(N,-2);
	rep(i,N){
		int a=order[i];
		for(auto x:G[a]){
			if(seen[a]==x) continue;
			seen[x]=a;
			order.push_back(x);
		}
	}
	rep(m,3){
		for(int i=N-1;i>=0;i--){
			int a=order[i];
			for(auto x:G[a]){
				if(seen[a]==x) continue;
				dp[a][m]+=dp[x][m];
				dp[a][m]%=M[m];
			}
			dp[a][m]*=q[m];
			dp[a][m]++;
			dp[a][m]%=M[m];
		}
	}
	ll ans=0;
	map<vector<ll>,ll> table;
	rep(i,N){
		ans+=table[dp[i]];
		table[dp[i]]++;
	}
	cout<<ans<<"\n";
}
accuracy: 1
time_ms: 60
memory_kb: 25660
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (int i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"YES\n"; else cout<<"NO\n";}


void solve();

//  rainy ~ 雨に打たれて ~
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	solve();
}

void solve(){
	int N;
	cin>>N;
	vector<vector<int>> G(N);
	rep(i,N-1){
		int a,b;
		cin>>a>>b;
		a--,b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	vector<vector<ll>> dp(N,vector<ll>(3));
	vector<ll> M={(1ll<<61)-1,998244353,924924167};
	vector<int> order={0},seen(N,-2);
	rep(i,N){
		int a=order[i];
		for(auto x:G[a]){
			if(seen[a]==x) continue;
			seen[x]=a;
			order.push_back(x);
		}
	}
	rep(m,3){
		for(int i=N-1;i>=0;i--){
			int a=order[i];
			for(auto x:G[a]){
				if(seen[a]==x) continue;
				dp[a][m]+=dp[x][m];
				dp[a][m]%=M[m];
			}
			dp[a][m]*=2ll;
			dp[a][m]++;
			dp[a][m]%=M[m];
		}
	}
	ll ans=0;
	map<vector<ll>,ll> table;
	rep(i,N){
		ans+=table[dp[i]];
		table[dp[i]]++;
	}
	cout<<ans<<"\n";
}
accuracy: 0.12
time_ms: 10
memory_kb: 7236
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (int i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"YES\n"; else cout<<"NO\n";}


void solve();

//  rainy ~ 雨に打たれて ~
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	solve();
}

void solve(){
	int N;
	cin>>N;
	vector<vector<int>> G(N);
	rep(i,N-1){
		int a,b;
		cin>>a>>b;
		a--,b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	vector<vector<int>> dp(N,vector<int>(3));
	vector<int> M={1000000007,998244353,924924167};
	vector<int> order={0},seen(N,-2);
	rep(i,N){
		int a=order[i];
		for(auto x:G[a]){
			if(seen[a]==x) continue;
			seen[x]=a;
			order.push_back(x);
		}
	}
	rep(m,3){
		for(int i=N-1;i>=0;i--){
			int a=order[i];
			for(auto x:G[a]){
				if(seen[a]==x) continue;
				dp[a][m]+=dp[x][m];
				dp[a][m]%=M[m];
			}
			dp[a][m]*=2ll;
			dp[a][m]++;
			dp[a][m]%=M[m];
		}
	}
	ll ans=0;
	map<vector<int>,ll> table;
	rep(i,N){
		ans+=table[dp[i]];
		table[dp[i]]++;
	}
	cout<<ans<<"\n";
}
accuracy: 0.12
time_ms: 10
memory_kb: 7232
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using P = pair<ll, ll>;
using P3 = pair<int, P>;
using PP = pair<P, P>;
constexpr int INF32 = 1 << 30;
constexpr ll INF64 = 1LL << 60;
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr double EPS = 1e-10;
const double PI = acos(-1);

#define ALL(v) (v).begin(),(v).end()
#define REP(i,n) for(int i=0,i_len=n; i<i_len; ++i)

template<typename T1,typename T2> bool chmax(T1 &a, T2 b) { if (a<b) { a=b; return 1; } return 0; }
template<typename T1,typename T2> bool chmin(T1 &a, T2 b) { if (b<a) { a=b; return 1; } return 0; }

struct Hash {
    static constexpr int M = 2;
    static constexpr ll MODS[M] = {999999937, 1000000007};
    static constexpr ll BASE = 100007;

    int n;
    ll hash[M];
    void init(){
        n = 0;
        REP(k,M){
            hash[k] = 0;
        }
    }
    void push(int d){
        n++;
        REP(k,M){
            hash[k] = (hash[k]*BASE + d) % MODS[k];
        }
    }
    ll get(int k) {
        return hash[k];
    }
};

bool match(Hash& a, Hash& b) {
    bool res = true;
    for (int k = 0; k < Hash::M; k++) {
        res &= a.get(k) == b.get(k);
    }
    return res;
}

Hash add(Hash a, Hash b){
    Hash res;
    res.init();
    REP(i, Hash::M){
        res.hash[i] = (a.get(i) + b.get(i)) % Hash::MODS[i];
    }
    res.n = max(a.n, b.n);
    return res;
}

vector<vector<int> > g;
vector<Hash> hs;

Hash dfs(int now){
    Hash res;
    res.init();
    for(auto nxt : g[now]){
        res = add(res, dfs(nxt));
    }
    res.push(1);
    return hs[now] = res;
}

int solve(){
    int n;
    cin >> n;
    g.resize(n);
    for(int i=0;i<n-1;i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
    }
    hs.resize(n);
    dfs(0);
    ll ans = 0;
    map<P, ll> cnt;
    REP(i,n){
        cnt[P(hs[i].get(0), hs[i].get(1))]++;
        // cout << hs[i].get(0) << " " << hs[i].get(1) << endl;
    }
    for(auto& node : cnt){
        ll x = node.second;
        // cout << x << endl;
        ans += x*(x-1)/2;
    }
    cout <<ans << endl;
    return 0;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
    // int T; cin >> T; REP(t,T) solve();
    // while(!solve());
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 28124
final_rank: 6

code_snippet: #include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};
long double eps = 1e-9;
long double pi = acos(-1);

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
    for(T &in : v) is >> in;
    return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
    for(int i = 0; i < (int) v.size(); i++) {
        os << v[i] << (i + 1 != (int) v.size() ? " " : "");
    }
    return os;
}

vector<vector<int>> v(200200);
const ll mul[] = {1007,1009,1013,1001};
const ll mod[] = {1000000007,1000000023,1000000087,1000000093};
const int hash_size = 3;

map<vector<ll>, ll> mp;
ll ans = 0;
vector<ll> dfs(int p, int pre=-1){
    vector<ll> ret(hash_size, 1);
    for(auto i:v[p]){
        if(i == pre) continue;
        auto g = dfs(i, p);
        for(int i=0;i<hash_size;i++){
            ret[i] += g[i] * mul[i] % mod[i];
            if(ret[i] >= mod[i]) ret[i] -= mod[i];
        }
    }
    ans += mp[ret];
    mp[ret]++;
    return ret;
}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    int n;
    cin>>n;
    for(int i=1;i<n;i++){
        int a,b;
        cin>>a>>b;
        a--,b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(0, -1);
    cout << ans << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 31256
final_rank: 12

code_snippet: #pragma region Macros
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < (n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

void Case(int i) { cout << "Case #" << i << ": "; }
int popcount(int x) { return __builtin_popcount(x); }
ll popcount(ll x) { return __builtin_popcountll(x); }
#pragma endregion Macros

using ull = unsigned long long;
using ui128 = __uint128_t;
constexpr ull mod = (1ULL << 61) - 1;

inline ull add(ull a, ull b) {
    if((a += b) >= mod) {
        a -= mod;
    }
    return a;
}
inline ull mul(ull a, ull b) {
    ui128 t = (ui128)a * b;
    ull na = t >> 61;
    ull nb = t & mod;
    if((na += nb) >= mod) {
        na -= mod;
    }
    return na;
}

int N;
vector<int> G[100001];
ull base, dp[100001];

ull dfs(int u, int p, int d) {
    ull res = base;
    for(int v : G[u]) {
        if(v == p) continue;
        res = add(res, mul(dfs(v, u, d+1), base));
    }
    return (dp[u] = res);
}

int main() {
    random_device seed_gen;
    mt19937_64 engine(seed_gen());
    uniform_int_distribution<ull> rand(2, mod-1);
    base = rand(engine);
    
    cin >> N;
    REP(i, N-1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    dfs(0, -1, 0);
    map<ull, int> cnt;
    REP(i, N) cnt[dp[i]] += 1;
    ll ans = 0;
    for(auto [h, sz] : cnt) {
        ans += (ll)sz * (sz-1) / 2;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 25260
final_rank: 4