QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#786226#9530. A Game On TreeSZH#WA 109ms20708kbC++142.4kb2024-11-26 20:48:032024-11-26 20:48:08

Judging History

This is the latest submission verdict.

  • [2024-11-26 20:48:08]
  • Judged
  • Verdict: WA
  • Time: 109ms
  • Memory: 20708kb
  • [2024-11-26 20:48:03]
  • Submitted

answer

#include<bits/stdc++.h>
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;

inline ll read()
{
	ll f=1,sum=0;char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-1;c=getchar();}
	while (isdigit(c)) {sum=sum*10+c-'0';c=getchar();}
	return sum*f;
}

const int maxn=222222;
const int mod=998244353;
int N,fir[maxn],nxt[maxn],to[maxn],tot;
ll siz[maxn],f1[maxn],f2[maxn],f3[maxn],ANS;

void add_edge(int x,int y)
{
	nxt[++tot]=fir[x],to[fir[x]=tot]=y;
}

void dfs(int x,int y)
{
	ll tmp1=0,tmp2=0,tmp3=0,tmp4=0,tmp5=0,tmp6=0;
	ll tot1=0,tot4=0,tot6=0;
	ll sizb2=0;
	siz[x]=1;
	for(int i=fir[x],v=to[i];i;i=nxt[i],v=to[i])if(v!=y)
	{
		dfs(v,x);
		sizb2+=siz[v]*siz[v];
		sizb2%=mod;
		siz[x]+=siz[v];
		f3[x]-=siz[v]*siz[v];
		f3[x]%=mod;
		tot1+=f3[v];
		tot1%=mod;
		tot4+=f1[v]+2*f2[v]+f3[v];
		tot4%=mod;
		tot6+=f2[v]+f3[v];
		tot6%=mod;
	}
	f1[x]=tot4;
	f2[x]=tot6;
	f3[x]+=tot1+2*siz[x]-1+(siz[x]-1)*(siz[x]-1);
	f3[x]%=mod;
	sizb2+=(N-siz[x])*(N-siz[x]);
	sizb2%=mod;
	for(int i=fir[x],v=to[i];i;i=nxt[i],v=to[i])if(v!=y)
	{
		tmp1=(tot1-f3[v])%mod;
		tmp2=(f1[v]+2*f2[v]+f3[v])%mod;
		tmp3=f3[v];
		tmp4=(tot4-(f1[v]+2*f2[v]+f3[v]))%mod;
		tmp5=(f2[v]+f3[v])%mod;
		tmp6=(tot6-(f2[v]+f3[v]))%mod;
		tot1=tmp1,tot4=tmp4,tot6=tmp6;
		ll tmp=N-siz[v];
		ANS+=(tmp*2-1+(tmp-1)*(N-1-siz[v])%mod-sizb2+siz[v]*siz[v]%mod)%mod*tmp2%mod;
		// cout<<"ANS"<<x<<':'<<ANS<<endl;
		ANS+=tmp1*tmp2%mod+tmp3*tmp4%mod+2*tmp5*tmp6%mod;
		// cout<<"ANS"<<x<<':'<<ANS<<endl;
		ANS%=mod;
	}
	// cout<<"ANS"<<x<<':'<<ANS<<endl;
	// cout<<"f"<<x<<':'<<f1[x]<<' '<<f2[x]<<' '<<f3[x]<<' '<<endl;
}

ll mul(ll x,int k)
{
	ll ans=1,mid=x;
	while(k)
	{
		if(k&1) ans=ans*mid%mod;
		mid=mid*mid%mod;
		k>>=1;
	}
	return ans;
}

int main()
{
	int T=read();
	while(T--)
	{
		N=read();
		tot=ANS=0;
		for(int i=0;i<=N+5;++i) fir[i]=siz[i]=f1[i]=f2[i]=f3[i]=0;
		for(int i=1;i<N;++i) 
		{
			int a=read(),b=read();
			add_edge(a,b),add_edge(b,a);
		}
		dfs(1,0);
		ANS=(ANS%mod+mod)%mod;
		// cout<<ANS<<endl;
		// for(ll i=1;i<=200;++i)if(i*4%mod*mul(N*N%mod*(N-1)%mod*(N-1)%mod,mod-2)%mod==918384806) cout<<i<<endl;
		cout<<ANS*4%mod*mul(N*N%mod*(N-1)%mod*(N-1)%mod,mod-2)%mod<<'\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 11792kb

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: 0
Accepted
time: 2ms
memory: 11760kb

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:

948445317
468414020
550143557
918384806
711758412
487662742
776412276
869581749
240852807
765628773
211048577
887328316
890334966
940494682
760637552
908032643
592850815
584006902
908525604
221832080
433351719
56023919
867301808
183319566
698771049
366957926
449579681
599710576
310564911
286902823
3...

result:

ok 1000 lines

Test #3:

score: 0
Accepted
time: 4ms
memory: 11816kb

input:

1000
94
59 1
33 59
73 1
6 33
83 59
4 59
20 59
61 6
39 1
76 73
71 6
44 39
9 71
24 4
87 9
57 83
2 9
81 71
82 20
90 2
85 39
12 9
30 83
66 30
53 9
47 9
36 44
43 53
29 12
31 53
64 81
38 31
84 82
77 38
23 71
93 84
78 83
58 31
68 90
42 1
55 64
13 78
70 78
62 24
19 55
92 87
14 57
10 84
65 81
63 6
75 36
91 1...

output:

508107725
996793960
201633249
335988372
842755864
460619380
342223697
207523414
429241811
391691799
542977964
786416604
454278948
685531402
25914978
440729774
228518323
679471537
82764520
554190841
432505337
143444089
189106586
337234245
61954935
905141094
532919674
703954588
185671863
942858630
692...

result:

ok 1000 lines

Test #4:

score: -100
Wrong Answer
time: 109ms
memory: 20708kb

input:

10000
8
1 4
3 1
5 1
7 3
8 4
6 8
2 7
8
2 6
4 6
5 6
8 5
7 6
3 5
1 7
8
8 5
6 5
2 5
7 2
1 6
3 1
4 8
9
8 6
9 8
3 6
1 8
5 9
2 8
4 3
7 9
8
8 6
3 6
5 8
1 6
4 3
7 6
2 6
9
9 5
7 5
2 7
8 7
4 9
3 7
6 3
1 4
8
1 4
5 1
6 5
3 4
8 4
7 8
2 5
9
1 8
6 1
2 1
3 8
5 3
9 8
7 8
4 8
9
4 9
2 9
1 2
3 4
5 2
6 9
8 3
7 2
8
1 2
8 ...

output:

49657566
56023919
387074343
97051536
701572244
211048577
711758412
308100110
761007271
711758412
178698065
285212675
80216065
43380497
267677376
818005792
53239701
765628773
970145625
387074343
436731906
422725927
479157293
977872021
436731906
925779210
487662742
705549251
267677376
711758412
526851...

result:

wrong answer 9992nd lines differ - expected: '391800050', found: '906559799'