QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#385594#8570. Idola-TreezhouhuanyiRE 0ms18652kbC++142.3kb2024-04-10 21:31:282024-04-10 21:31:28

Judging History

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

  • [2024-04-10 21:31:28]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:18652kb
  • [2024-04-10 21:31:28]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#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],l1,r1,l2,r2;
long long dp[N+1][3],delta[N+1],tong[N+1],dque[N+1],dque2[N+1];
bool used[N+1];
vector<int>E[N+1];
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()
{
	//freopen("idola.in","r",stdin);
	//freopen("idola.out","w",stdout);
	int x,y,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
		{
			dfs(1),dfs2(1),l1=l2=1,r1=r2=0;
			for (int i=1;i<=n;++i)
				if (E[i].size()==1)
					dque[++r1]=delta[i]+n-2;
			sort(dque+1,dque+r1+1);
			for (int i=0;i<=sC;++i)
			{
				if (i)
				{
					if (l1<=r1&&(l2==r2+1||dque[l1]<dque2[l2])) Adder(ans,dque[l1]%mod),dque2[++r2]=dque[l1]+((n-2)<<1);
					else Adder(ans,dque2[l2]),dque2[++r2]=dque[l1]+((n-2)<<1);
				}
				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: 18652kb

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

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
249984

result:

ok 4 tokens

Test #3:

score: -100
Runtime Error

input:

4
300000 50000000
216838 200677
44849 12926
125086 157230
26195 29767
241694 21336
21336 24457
105861 84565
184655 45583
175336 97945
286044 30927
295273 249694
109469 1566
193560 251229
176229 288707
206166 13532
166218 264413
299977 95587
159105 48116
57912 82606
97296 193689
115029 121192
68068 1...

output:


result: