QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#694017 | #9530. A Game On Tree | xuxuxuxuxu# | WA | 2ms | 6384kb | C++14 | 2.1kb | 2024-10-31 17:07:47 | 2024-10-31 17:08:01 |
Judging History
answer
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
using std::cin;
using std::cout;
using i64 = long long;
using pii = std::pair<int, int>;
const int nx = 1e5;
namespace mo {
const int mod = 998244353;
int add(int x, int y) {return x += y, (x >= mod) && (x -= mod), x;}
int dec(int x, int y) {return x -= y, (x < 0) && (x += mod), x;}
void pls(int &x, int y) {x += y, (x >= mod) && (x -= mod);}
void mns(int &x, int y) {x -= y, (x < 0) && (x += mod);}
int mul(int x, int y) {return 1ll * x * y % mod;}
int pow(int x, int k = mod - 2) {
int res = 1;
for (; k; k >>= 1, x = mul(x, x)) if (k & 1) res = mul(res, x);
return res;
}
int p2(int x) {
return pow(x, 2);
}
}using namespace mo;
int tc;
int n;
int siz[nx + 5], ssiz[nx + 5];
std::vector<int> e[nx + 5];
int f[nx + 5];
int ans;
void dfs0(int p, int fp = 0) {
ssiz[p] = 0;
siz[p] = 1;
for (int q : e[p]) if (q != fp) {
dfs0(q, p);
siz[p] += siz[q];
pls(ssiz[p], ssiz[q]);
}
pls(ssiz[p], p2(siz[p]));
}
void dfs1(int p, int fp = 0) {
if (fp) {
f[p] = dec(add(f[fp], p2(n - siz[p])), p2(siz[p]));
pls(ans, mul(dec(add(f[fp], p2(n-siz[p])), add(ssiz[p], p2(n))), p2(siz[p])));
pls(ans, mul(p2(n - siz[p]), dec(ssiz[p], p2(siz[p]))));
}
for (int q : e[p]) if (q != fp) dfs1(q, p);
}
int main() {
freopen("in", "r", stdin);
std::ios::sync_with_stdio(false);
cin >> tc;
while (tc--) {
cin >> n;
for (int i = 1; i <= n; ++i) e[i].clear();
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
e[u].emplace_back(v);
e[v].emplace_back(u);
}
ans = 0;
dfs0(1);
f[1] = ssiz[1];
dfs1(1);
int cnt = mul(mul(n, n - 1), pow(2));
ans = mul(ans, pow(pow(cnt, 2)));
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 6384kb
input:
2 3 1 2 2 3 5 1 2 1 5 3 2 4 2
output:
result:
wrong answer 1st lines differ - expected: '443664158', found: ''