QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#707437 | #9530. A Game On Tree | leafmaple# | WA | 4ms | 7824kb | C++20 | 1.6kb | 2024-11-03 15:58:49 | 2024-11-03 15:58:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6+5;
const int mod = 998244353;
vector<int>g[N];
int qpow(int a, int b){
a %= mod;
int ans = 1;
while(b){
if(b&1)ans= ans * a%mod;
b>>=1;
a = a*a%mod;
}
return ans;
}
int tot = 0;
int siz[N], fs[N];
int n;
void dfs1(int u, int fa){
siz[u] = 1;
for(auto v: g[u]) if(v != fa){
dfs1(v, u);
siz[u] = (siz[u] + siz[v])%mod;
fs[u] = (fs[u] + siz[v]*siz[v]%mod)%mod;
}
fs[u] = (fs[u] + siz[u]*siz[u]%mod)%mod;
int x = siz[u] * (n - siz[u]);
tot = (tot + x*x%mod)%mod;
}
int tot2 = 0;
void dfs2(int u, int fa){
int fk = 0;
int ans = 0;
for(auto v: g[u])if(v!=fa){
dfs2(v, u);
//case1
int x = (n-siz[v])*(n-siz[v])%mod;
int y = (fs[v] - siz[v]*siz[v]%mod)%mod;
x = (x + mod)%mod;
y = (y + mod)%mod;
ans = (ans + 2*x%mod*y%mod)%mod;
//case2
ans = (ans + 2*fk%mod*fs[v]%mod)%mod;
fk = (fk + fs[v])%mod;
}
// tot2 += ans;
tot2 = (tot2 + ans)%mod;
// cout << u << ' ' << ans << endl;
}
void solve()
{
cin >> n;
tot = tot2 = 0;
for(int i=1; i<=n; i++){
g[i].clear();
siz[i] = fs[i] = 0;
}
for(int i=1; i<n; i++){
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs1(1, 0);
dfs2(1, 0);
// cout << tot << ' ' << tot2 << endl;
tot = (tot + tot2)%mod;
int base = n*(n-1)/2;
base = base*base%mod;
tot = (tot + mod)%mod;
cout << tot*qpow(base, mod-2)%mod << endl;
//
}
signed main (){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--)
solve();
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 7824kb
input:
2 3 1 2 2 3 5 1 2 1 5 3 2 4 2
output:
443664158 918384806
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 4ms
memory: 7708kb
input:
1000 7 3 6 4 3 5 3 2 6 1 4 7 1 12 5 7 10 7 2 10 11 2 1 7 8 1 4 2 9 11 6 9 12 11 3 5 6 2 5 1 2 4 5 6 4 3 6 5 2 5 1 5 4 5 3 2 8 1 8 2 8 4 2 6 1 5 6 7 6 3 8 8 3 8 7 3 4 8 6 4 2 7 5 2 1 4 4 3 1 4 3 2 1 6 5 1 6 1 2 5 3 5 4 2 12 8 11 5 11 12 8 3 12 6 12 2 3 4 6 10 11 1 5 9 5 7 5 9 6 1 7 6 4 7 8 7 5 4 9 6 ...
output:
934863761 939119690 381551177 259543533 495302366 760142705 776412276 292818345 628600613 203346074 791817282 243399088 247498602 616913179 298240908 198662952 507601297 584006902 38943856 154050056 359102138 109501295 816465290 98592036 758665710 166649059 961765302 589524409 310564911 431340154 81...
result:
wrong answer 1st lines differ - expected: '948445317', found: '934863761'