QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#570739#8836. Highway HoaxDjangle162857TL 7ms34612kbC++141.8kb2024-09-17 17:29:462024-09-17 17:29:47

Judging History

This is the latest submission verdict.

  • [2024-09-17 17:29:47]
  • Judged
  • Verdict: TL
  • Time: 7ms
  • Memory: 34612kb
  • [2024-09-17 17:29:46]
  • Submitted

answer

#define _CRT_SECURE_NO_WARNINGS
#include<set>
#include<queue>
#include<cmath>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;

const int N=400010;
const int mod=998244353;
int n,cnt,maxicnt,c[N],f[N][2],nouid[N],vis[N];
vector<int>id[N],e2[N];
char s[N];

struct Graph{
	int tot,head[N],suiv[N],ver[N];

	inline void lnk(int x,int y)
	{
		ver[++tot]=y;
		suiv[tot]=head[x];
		head[x]=tot;
	}

	inline void bfs(int st,int posid)
	{
		queue<int>q;
		q.push(st);
		while(q.size())
		{
			int x=q.front();q.pop();
			id[x].push_back(posid);
			c[posid]++;
			for(int i=head[x];i;i=suiv[i])
			{
				int y=ver[i];
				if(s[y]!='S')continue;
				if(id[y].size()&&!nouid[y])
					nouid[y]=++cnt;
				q.push(y);
			}
		}
	}

	inline void dfs(int x,int fa)
	{
		vis[x]=1;
		if(x<=maxicnt)
			f[x][0]=c[x]-1,f[x][1]=1;
		else f[x][0]=1,f[x][1]=0;
		for(auto y:e2[x])
		{
			if(y==fa)continue;
			dfs(y,x);
			int tem0=f[x][0],tem1=f[x][1];
			f[x][0]=1ll*tem0*f[y][0]%mod;
			f[x][1]=(1ll*tem0*f[y][1]%mod+1ll*tem1*f[y][0]%mod)%mod;
		}
		//cout<<"dfs "<<x<<" "<<f[x][0]<<" "<<f[x][1]<<endl;
	}

}e;

int main()
{
	scanf("%d",&n);
	scanf("%s",s+1);
	for(int i=1,x,y;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		e.lnk(y,x);
	}
	for(int i=1;i<=n;i++)
		if(s[i]=='F')nouid[i]=++cnt;
	maxicnt=cnt;
	for(int i=1;i<=n;i++)
		if(s[i]=='F')e.bfs(i,nouid[i]);
	//cout<<maxicnt<<" "<<cnt<<endl;
	for(int i=1;i<=n;i++)
	{
		if(nouid[i]<=maxicnt)continue;
		for(auto x:id[i])
			e2[x].push_back(nouid[i]),
			e2[nouid[i]].push_back(x)
			//,cout<<"lnk "<<x<<" "<<nouid[i]<<endl
			;
	}
	int ans=1;
	for(int i=1;i<=maxicnt;i++)
		if(!vis[i])
		{
			e.dfs(i,i);
			ans=ans*((f[i][0]+f[i][1])%mod)%mod;
		}
	printf("%d\n",ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 32496kb

input:

5
SFSFS
2 1
2 3
4 3
4 5

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

4
SFFF
1 2
1 3
1 4

output:

4

result:

ok 1 number(s): "4"

Test #3:

score: 0
Accepted
time: 2ms
memory: 34612kb

input:

7
SSSSFFF
1 2
3 2
4 3
4 5
4 6
2 7

output:

13

result:

ok 1 number(s): "13"

Test #4:

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

input:

2
FS
1 2

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 0ms
memory: 32792kb

input:

3
FFF
3 1
3 2

output:

1

result:

ok 1 number(s): "1"

Test #6:

score: 0
Accepted
time: 3ms
memory: 32488kb

input:

4
FFFF
1 2
4 2
2 3

output:

1

result:

ok 1 number(s): "1"

Test #7:

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

input:

5
FFFFF
4 2
2 1
3 1
3 5

output:

1

result:

ok 1 number(s): "1"

Test #8:

score: 0
Accepted
time: 7ms
memory: 32496kb

input:

6
FSSSSF
5 3
2 5
1 2
2 6
4 2

output:

3

result:

ok 1 number(s): "3"

Test #9:

score: -100
Time Limit Exceeded

input:

199995
FSSFFFSSSFSFFSSSFSSSSSFSFSSSFSFFSSFSFSFFFSSSFSSFSFFSFFFFSSFFSSSFFSFSFFFFFFFFFFFFFSSSFSSFFFSSSFSFSSSFFFSFFSFSSSSFSFSFSFSSFFSFSFFSFFSSFSSSFSFFSSSFFSFFFSFFSFSFSFSFSSSFSSSSFFSSFFFFFSFSSSFFSSSSSSSSFFFSFSFFSSSFSFFFFFSFFFFSSSSSSSFFFSFFSFSSSFSFSFFFFSFSSFSSFFFSSFFFSSFSFFFSSSFSFSSFFSFSFFSFFSSFSFSSSSFFF...

output:


result: