QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#469241#6892. WO MEI Kgrass8cowAC ✓321ms54072kbC++171.7kb2024-07-09 16:39:342024-07-09 16:39:34

Judging History

你现在查看的是最新测评结果

  • [2024-07-09 16:39:34]
  • 评测
  • 测评结果:AC
  • 用时:321ms
  • 内存:54072kb
  • [2024-07-09 16:39:34]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int n;
#define pb push_back
#define pi pair<int,int>
#define fi first
#define se second
const int mod=998244353;
vector<pi>g[210000];
vector<int>co[201000],G[200100];
int fa[201000],wh[201000],sz[201000],nc[201000],tc[201000],sk[200100];
vector<int>V;
void dfs(int x){
	V.pb(x);
    sz[x]=1;
    for(pi t:g[x])if(t.fi!=fa[x]){
        int v=t.fi,w=t.se;fa[v]=x,nc[v]=w;
        if(co[w].empty())wh[v]=1;
        else wh[v]=co[w].back();
        G[wh[v]].push_back(v);
        co[w].pb(v);
        dfs(v);
        sz[x]+=sz[v];
        co[w].pop_back();
    }
}
int qpow(int a,int b){
    int c=1;
    for(;b;b>>=1){
        if(b&1)c=1ll*a*c%mod;
        a=1ll*a*a%mod;
    }
    return c;
}
int jc[200100],ij[200100];
int C(int a,int b){
    return 1ll*jc[a]*ij[b]%mod*ij[a-b]%mod;
}
void sol(){
    scanf("%d",&n);
    jc[0]=1;
    for(int i=1;i<=n;i++)
    g[i].clear(),fa[i]=nc[i]=0,G[i].clear(),tc[i]=n,jc[i]=1ll*i*jc[i-1]%mod,co[i].clear();
    ij[n]=qpow(jc[n],mod-2);
    for(int i=n;i;i--)ij[i-1]=1ll*i*ij[i]%mod;
    for(int i=1,u,v,w;i<n;i++)scanf("%d%d%d",&u,&v,&w),g[u].pb({v,w}),g[v].pb({u,w});
    V.clear(),dfs(1);
    reverse(V.begin(),V.end());
    int ans=0;
    for(int i:V)if(i>=2){
        sk[i]=sz[i];
        for(int v:G[i])sk[i]-=sz[v];
        for(int v:G[i])(ans+=1ll*sk[i]*sk[v]%mod)%=mod;
    }
    for(int v:G[1])tc[nc[v]]-=sz[v];
    for(int v:G[1])(ans+=1ll*tc[nc[v]]*sk[v]%mod)%=mod;
    int all=0;
    for(int i=2;i<=n;i++){
        int nt=1ll*ans*C(n-2,i-2)%mod*ij[n]%mod*jc[n-i]%mod*jc[i]%mod;
        all^=nt;
    }
    printf("%d\n",all);
}
int main(){
    int T;scanf("%d",&T);while(T--)sol();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 321ms
memory: 54072kb

input:

106
200000
2 1 70358
3 1 94059
4 3 194779
5 3 147526
6 1 128796
7 4 141976
8 4 69430
9 3 132781
10 6 82526
11 5 93708
12 8 10824
13 8 159324
14 10 95645
15 9 83437
16 10 61897
17 13 119054
18 14 116823
19 18 149469
20 10 72020
21 2 95763
22 1 194299
23 1 84812
24 1 20933
25 7 71421
26 9 111015
27 16...

output:

479799996
559987406
173574248
414521015
435350101
700635296
260375015
89074409
377135544
408258434
551176217
27254026
306297792
254632387
447509594
817903044
963868466
827085336
1067266388
422475867
31622164
843934379
105738540
870679243
513141034
752944662
798135577
305400376
650171300
371654894
81...

result:

ok 106 lines