QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#570705#8836. Highway HoaxDjangle162857#WA 7ms32772kbC++142.0kb2024-09-17 17:12:322024-09-17 17:12:35

Judging History

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

  • [2024-09-17 17:12:35]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:32772kb
  • [2024-09-17 17:12:32]
  • 提交

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];
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)
	{
		
		for(auto y:e2[x])
		{
			if(y==fa)continue;
			dfs(y,x);
		}
		
		if(x<=maxicnt)
		{
			f[x][0]=c[x]-1;
			f[x][1]=1;
			for(auto y:e2[x])
			{
				if(y==fa)continue;
				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;

			}

		}
		else
		{
			f[x][0]=1;
			f[x][1]=0;
			for(auto y:e2[x])
			{
				if(y==fa)continue;
				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]);
	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
			;
	}
	e.dfs(1,1);
	printf("%d\n",(f[1][0]+f[1][1])%mod);
	return 0;
}

詳細信息

Test #1:

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

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: 0ms
memory: 32488kb

input:

4
SFFF
1 2
1 3
1 4

output:

4

result:

ok 1 number(s): "4"

Test #3:

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

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: 0ms
memory: 30472kb

input:

2
FS
1 2

output:

1

result:

ok 1 number(s): "1"

Test #5:

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

input:

3
FFF
3 1
3 2

output:

1

result:

ok 1 number(s): "1"

Test #6:

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

input:

4
FFFF
1 2
4 2
2 3

output:

1

result:

ok 1 number(s): "1"

Test #7:

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

input:

5
FFFFF
4 2
2 1
3 1
3 5

output:

1

result:

ok 1 number(s): "1"

Test #8:

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

input:

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

output:

1

result:

wrong answer 1st numbers differ - expected: '3', found: '1'