|
| 1 | +/** |
| 2 | + * @file 2178F.cpp |
| 3 | + * @author Macesuted ([email protected]) |
| 4 | + * @date 2025-12-28 |
| 5 | + * |
| 6 | + * @copyright Copyright (c) 2025 |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#include <bits/stdc++.h> |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +#ifndef LOCAL |
| 14 | +#define endl '\n' |
| 15 | +#endif |
| 16 | + |
| 17 | +bool mem1; |
| 18 | + |
| 19 | +#define maxn 200005 |
| 20 | +#define mod 998244353 |
| 21 | + |
| 22 | +vector<vector<int>> graph; |
| 23 | +int siz[maxn]; |
| 24 | + |
| 25 | +int64_t qpow(int64_t a, int64_t x) { |
| 26 | + int64_t ans = 1; |
| 27 | + while (x) { |
| 28 | + if (x & 1) ans = ans * a % mod; |
| 29 | + a = a * a % mod, x >>= 1; |
| 30 | + } |
| 31 | + return ans; |
| 32 | +} |
| 33 | +int64_t inv(int64_t a) { return qpow(a, mod - 2); } |
| 34 | + |
| 35 | +void dfs(int p, int pre = -1) { |
| 36 | + siz[p] = 1; |
| 37 | + for (auto q : graph[p]) |
| 38 | + if (q != pre) dfs(q, p), siz[p] += (siz[q] & 1) ? siz[q] : 0; |
| 39 | + return; |
| 40 | +} |
| 41 | + |
| 42 | +void solve(void) { |
| 43 | + int n; |
| 44 | + cin >> n; |
| 45 | + graph.clear(), graph.resize(n + 1); |
| 46 | + for (int i = 1, x, y; i < n; i++) cin >> x >> y, graph[x].push_back(y), graph[y].push_back(x); |
| 47 | + |
| 48 | + dfs(1); |
| 49 | + |
| 50 | + vector<int> S; |
| 51 | + for (int i = 2; i <= n; i++) |
| 52 | + if (!(siz[i] & 1)) S.push_back(siz[i]); |
| 53 | + |
| 54 | + if (S.empty()) return cout << 1 << endl, void(); |
| 55 | + |
| 56 | + int64_t ans = 0, mul = 1; |
| 57 | + for (auto s : S) mul = mul * s % mod; |
| 58 | + mul = mul * mul % mod * siz[1] % mod; |
| 59 | + |
| 60 | + for (auto s : S) ans = (ans + mul * inv(s)) % mod; |
| 61 | + |
| 62 | + for (int i = 1; i < (int)S.size(); i++) ans = ans * i % mod; |
| 63 | + |
| 64 | + cout << ans << endl; |
| 65 | + |
| 66 | + return; |
| 67 | +} |
| 68 | + |
| 69 | +bool mem2; |
| 70 | + |
| 71 | +int main() { |
| 72 | + ios::sync_with_stdio(false), cin.tie(nullptr); |
| 73 | +#ifdef LOCAL |
| 74 | + cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl; |
| 75 | +#endif |
| 76 | + |
| 77 | + int _ = 1; |
| 78 | + cin >> _; |
| 79 | + while (_--) solve(); |
| 80 | + |
| 81 | +#ifdef LOCAL |
| 82 | + cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl; |
| 83 | +#endif |
| 84 | + return 0; |
| 85 | +} |
0 commit comments