QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#385545#8570. Idola-TreezhouhuanyiWA 2ms14860kbC++142.1kb2024-04-10 21:05:472024-04-10 21:05:48

Judging History

This is the latest submission verdict.

  • [2024-04-10 21:05:48]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 14860kb
  • [2024-04-10 21:05:47]
  • Submitted

answer

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#define N 300000
#define mod 998244353
using namespace std;
const int inv2=(mod+1)>>1;
int read()
{
	char c=0;
	int sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
int MD(int x)
{
	return x>=mod?x-mod:x;
}
void Adder(int &x,int d)
{
	x+=d;
	if (x>=mod) x-=mod;
	return;
}
int T,n,sC,ans,tans,fa[N+1],C[3][3];
long long dp[N+1][3],delta[N+1];
bool used[N+1];
vector<int>E[N+1];
priority_queue<long long>q;
void add(int x,int y)
{
	E[x].push_back(y),E[y].push_back(x);
	return;
}
void adder(int x,int y,int d)
{
	for (int i=0;i<=2;++i)
		for (int j=0;j<=i;++j)
			dp[x][i]+=dp[y][j]*C[i][j]*d;
	return;
}
void dfs(int x)
{
	used[x]=dp[x][0]=1;
	for (int i=0;i<E[x].size();++i)
		if (!used[E[x][i]])
			fa[E[x][i]]=x,dfs(E[x][i]),adder(x,E[x][i],1);
	return;
}
void dfs2(int x)
{
	Adder(ans,1ll*(dp[x][2]%mod)*inv2%mod);
	if (E[x].size()==1) delta[x]=2*dp[x][1];
	for (int i=0;i<E[x].size();++i)
		if (fa[E[x][i]]==x)
			adder(x,E[x][i],-1),adder(E[x][i],x,1),dfs2(E[x][i]),adder(E[x][i],x,-1),adder(x,E[x][i],1);
	return;
}
int main()
{
	int x,y,d,rst;
	for (int i=0;i<=2;++i) C[i][0]=1;
	for (int i=1;i<=2;++i)
		for (int j=1;j<=i;++j)
			C[i][j]=C[i-1][j-1]+C[i-1][j];
	T=read();
	while (T--)
	{
		n=read(),sC=read()-(n-1),ans=tans=0;
		for (int i=1;i<=n;++i)
		{
			E[i].clear(),used[i]=fa[i]=0;
			for (int j=0;j<=2;++j) dp[i][j]=0;
		}
		for (int i=1;i<=n-1;++i) x=read(),y=read(),add(x,y);
		if (n==2)
		{
			Adder(ans,1);
			for (int i=0;i<=sC;++i)
			{
				if (i) Adder(ans,1);
				Adder(tans,1ll*ans*ans%mod*ans%mod);
			}
			printf("%d\n",tans);
		}
		else
		{
			while (!q.empty()) q.pop();
			dfs(1),dfs2(1);
			for (int i=1;i<=n;++i)
				if (E[i].size()==1)
					q.push(delta[i]+n-2);
			for (int i=0;i<=sC;++i)
			{
				if (i) d=q.top(),Adder(ans,d%mod),q.pop(),q.push(d+n-2);
				rst=MD(ans+1ll*i*i%mod),Adder(tans,1ll*rst*rst%mod*rst%mod);
			}
			printf("%d\n",tans);
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
4 3
1 4
1 3
2 1
4 4
1 4
1 3
2 1

output:

3375
25327

result:

ok 2 tokens

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 13432kb

input:

4
4 3
1 4
1 3
2 1
4 4
1 4
1 3
2 1
5 4
1 4
1 3
1 2
5 4
5 5
1 4
1 3
1 2
5 4

output:

3375
25327
54872
270872

result:

wrong answer 4th words differ - expected: '249984', found: '270872'