Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Sum of Degree Sum

#include <atcoder/modint>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;

using mint = modint998244353;

class Tree {
  public:
    int n;
    vector<vector<int>> adj;
    vector<vector<mint>> dp, cnt;
    // dp[i][d] represents the sum of cost of configurations of the i-th subtree
    // in which the i-th node has degree d.

    // cnt[i][d] represents the number of configurations of the i-th
    // subtree in which the i-th node has degree d.
    Tree(int n) {
        this->n = n;
        adj.resize(n);
        dp.resize(n, vector<mint>(n + 1, 0));
        cnt.resize(n, vector<mint>(n + 1, 0));
    }

    void dfs(int src, int par) {
        dp[src][0] = 0;
        cnt[src][0] = 1;
        for (auto child : adj[src]) {
            if (child == par) {
                continue;
            }
            dfs(child, src);
            vector<mint> ndp(n + 1), ncnt(n + 1);
            for (int d = 0; d < adj[src].size(); d++) {
                for (int now = 0; now < adj[child].size(); now++) {
                    mint L = cnt[src][d], R = cnt[child][now];
                    mint left_sum = dp[src][d], right_sum = dp[child][now];

                    ndp[d] += L * right_sum + R * left_sum;
                    ndp[d + 1] += L * (right_sum + R) + R * (left_sum + L);

                    ncnt[d] += cnt[src][d] * cnt[child][now];
                    ncnt[d + 1] += cnt[src][d] * cnt[child][now];
                }
            }
            swap(dp[src], ndp);
            swap(cnt[src], ncnt);
        }
    }
};

void solve() {
    int n;
    cin >> n;
    Tree t(n);

    int m = n - 1;
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        t.adj[x].push_back(y);
        t.adj[y].push_back(x);
    }

    t.dfs(0, -1);
    mint ans = 0;
    for (int d = 0; d < n; d++) {
        ans += t.dp[0][d];
    }
    cout << ans.val() << endl;
}

int main() {
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        solve();
    }

    return 0;
}
#include <atcoder/modint>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;

using mint = modint998244353;

class Tree {
  public:
    int n;
    vector<vector<int>> adj;
    vector<int> deg;
    vector<bool> visited;
    Tree(int n) {
        this->n = n;
        adj.resize(n);
        deg.resize(n);
        visited.resize(n);
    }

    void dfs(int src, int par) {
        if (visited[src]) {
            return;
        }

        deg[src] = adj[src].size();
        for (auto child : adj[src]) {
            if (child != par) {
                dfs(child, src);
            }
        }
    }
};

void solve() {
    int n;
    cin >> n;

    vector<pair<int, int>> edges;
    int m = n - 1;
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        edges.push_back({x, y});
    }

    mint ans = 0;

    int full_mask = 1 << m;
    for (int msk = 0; msk < full_mask; msk++) {
        Tree t(n);
        for (int i = 0; i < m; i++) {
            if ((1 << i) & msk) {
                int x = edges[i].first;
                int y = edges[i].second;
                t.adj[x].push_back(y);
                t.adj[y].push_back(x);
            }
        }
        mint now = 0;
        for (int i = 0; i < n; i++) {
            t.dfs(i, -1);
            now += t.deg[i];
        }
        ans += now;
    }

    cout << ans.val() << endl;
}

int main() {
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        solve();
    }
    return 0;
}