QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#439940#6892. WO MEI KAcoippAC ✓431ms65388kbC++142.6kb2024-06-12 20:54:172024-06-12 20:54:18

Judging History

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

  • [2024-06-12 20:54:18]
  • 评测
  • 测评结果:AC
  • 用时:431ms
  • 内存:65388kb
  • [2024-06-12 20:54:17]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define mod 998244353
#define N 200005
using namespace std;
inline char nc(){
	static char buf[1000000],*p=buf,*q=buf;
	return p==q&&(q=(p=buf)+fread(buf,1,1000000,stdin),p==q)?EOF:*p++;
}
inline ll read(){
	ll res = 0;
	char c = nc();
	while(c<'0'||c>'9')c=nc();
	while(c<='9'&&c>='0')res=res*10+c-'0',c=nc();
	return res;
}
char obuf[1<<21],*p3=obuf; 
inline void pc(char c){ 
	p3-obuf<=(1<<20)?(*p3++=c):(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=c); 
} 
inline void write(ll x){ 
	if(x<0) pc('-'),x=-x; 
	if(x>9) write(x/10); 
	pc(x%10+'0'); 
}
vector<ll> op[N],sta[N],w[N];
ll n,jc[N],inv[N],i,e[N],sum,cnt[N],son[N],x,y,z,cnt2[N];
inline ll qmi(ll a,ll b,ll p){
	ll res = 1%p,t = a;
	while(b){
		if(b&1) res=res*t%p;
		t=t*t%p;
		b>>=1;
	}
	return res;
}
inline ll C(ll n,ll m){
	if(n<m) return 0;
	return jc[n]*inv[m]%mod*inv[n-m]%mod;
}
inline void init(ll x,ll fa){
	son[x] = 1;
	for(ll i=0;i<op[x].size();i++){
		if(op[x][i]==fa) continue;
		init(op[x][i],x);
		son[x] += son[op[x][i]];
	}
	cnt[x] = son[x];
}
inline void dfs(ll x,ll fa){
	for(ll i=0;i<op[x].size();i++){
		if(op[x][i]==fa) continue;
		ll pos = sta[w[x][i]][sta[w[x][i]].size()-1];
		if(pos!=1) cnt[pos] -= son[op[x][i]];
		else cnt2[w[x][i]] -= son[op[x][i]];
		sta[w[x][i]].push_back(op[x][i]);
		dfs(op[x][i],x);
		sta[w[x][i]].pop_back();
	}
}
inline void dfs2(ll x,ll fa){
	for(ll i=0;i<op[x].size();i++){
		if(op[x][i]==fa) continue;
		ll pos = sta[w[x][i]][sta[w[x][i]].size()-1];
		if(pos!=1) sum = (sum+cnt[op[x][i]]*cnt[pos])%mod;
		else sum = (sum+cnt[op[x][i]]*cnt2[w[x][i]])%mod;
		sta[w[x][i]].push_back(op[x][i]);
		dfs2(op[x][i],x);
		sta[w[x][i]].pop_back();
	}
}
inline void solve(){
	sum=0;
	n=read();
	for(ll i=1;i<n;i++){
		x=read(),y=read(),z=read();
		op[x].push_back(y),w[x].push_back(z);
		op[y].push_back(x),w[y].push_back(z);
	}
	for(ll i=1;i<=n;i++) sta[i].push_back(1),cnt2[i]=n;
	init(1,-1);
	dfs(1,-1);
	dfs2(1,-1);
	e[1]=0,e[2]=sum;
	for(ll i=3;i<=n;i++) e[i]=e[2]*C(n-2,i-2)%mod;
	for(ll i=1;i<=n;i++) e[i]=e[i]*qmi(C(n,i),mod-2,mod)%mod;
	for(ll i=1;i<=n;i++) e[i]^=e[i-1];
	write(e[n]),pc('\n');
}
inline void clear(){
	for(ll i=1;i<=n;i++) op[i].clear(),w[i].clear();
	for(ll i=1;i<=n;i++) sta[i].clear();
	for(ll i=1;i<=n;i++) cnt[i]=0,cnt2[i]=0;
}
int main(){
	jc[0]=1;
	for(i=1;i<=2e5;i++) jc[i]=jc[i-1]*i%mod;
	inv[200000]=qmi(jc[200000],mod-2,mod);
	for(i=2e5;i>=1;i--) inv[i-1]=inv[i]*i%mod;
	ll T;
	T=read();
	while(T--) solve(),clear();
	return fwrite(obuf,p3-obuf,1,stdout),0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 431ms
memory: 65388kb

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