QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#694006 | #9530. A Game On Tree | szy10010 | WA | 2ms | 11848kb | C++23 | 2.1kb | 2024-10-31 17:06:41 | 2024-10-31 17:06:47 |
Judging History
answer
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define u1 (u<<1)
#define u2 (u<<1|1)
#define pb emplace_back
#define pp pop_back()
#define int long long
#define laile cout<<"laile"<<endl
#define lowbit(x) ((x)&(-x))
#define double long double
#define sf(x) scanf("%lld",&x)
#define sff(x,y) scanf("%lld %lld",&x,&y)
#define sd(x) scanf("%Lf",&x)
#define sdd(x,y) scanf("%Lf %Lf",&x,&y)
#define _for(i,n) for(int i=0;i<(n);++i)
#define _rep(i,a,b) for(int i=(a);i<=(b);++i)
#define _pre(i,a,b) for(int i=(a);i>=(b);--i)
#define all(x) (x).begin(), (x).end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef unsigned long long ULL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
const int N=1e6+10,INF=4e18,P=998244353;
int n,m;
int e[N],ne[N],h[N],idx;
int sz[N],sum[N];
int res;
void add(int a,int b)
{
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
void dfs(int u,int fa)
{
sz[u]=1;
sum[u]=0;
int s=0;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==fa)continue;
dfs(j,u);
sz[u]+=sz[j];
sum[u]+=sz[j]*sz[j]%P;
sum[u]%=P;
res+=2*(n-sz[j])*(n-sz[j])%P*(((sum[j]-sz[j]*sz[j]%P)%P+P)%P)%P;
res%=P;
res+=sz[j]*(n-sz[j])%P*sz[j]%P*(n-sz[j])%P;
res%=P;
s=(s+sum[j])%P;
}
sum[u]+=sz[u]*sz[u]%P;
sum[u]%=P;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==fa)continue;
res+=(sum[j]*(((s-sum[j])%P+P)%P)%P)%P;
res%=P;
}
return;
}
int qmi(int a,int b)
{
int res=1;
while(b)
{
if(b&1)res=res*a%P;
a=a*a%P;
b>>=1;
}
return res;
}
void solve()
{
cin>>n;
memset(h,-1,(n+1)*8);
idx=res=0;
_rep(i,2,n)
{
int a,b;
cin>>a>>b;
add(a,b);
add(b,a);
}
dfs(1,0);
int Cn2=n*(n-1)/2;
Cn2%=P;
Cn2=Cn2*Cn2%P;
// cout<<"res="<<res<<'\n';
cout<<res*qmi(Cn2,P-2)%P<<'\n';
return;
}
signed main()
{
IOS;
int T=1;
cin>>T;
while(T--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11848kb
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: 2ms
memory: 11840kb
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'