QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#690216#8836. Highway HoaxRafi22WA 334ms52224kbC++145.1kb2024-10-30 21:03:542024-10-30 21:03:55

Judging History

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

  • [2024-10-30 21:03:55]
  • 评测
  • 测评结果:WA
  • 用时:334ms
  • 内存:52224kb
  • [2024-10-30 21:03:54]
  • 提交

answer

#include <bits/stdc++.h>
#pragma GCC target ("avx2,fma")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")


#define int long long
#define ll long long
#define ld long double
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define ROF(i,r,l) for(int i=(r);i>=(l);i--)
using namespace std;
int inf=1000000000000000007;
//int mod=1000000007;
int mod1=998244353;

#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

const long long MOD = 998244353;
const long long mod = 998244353;
const long long HALF = 499122177;
const std::vector<long long> ntt_root = {1, 998244352, 911660635, 372528824, 929031873, 452798380, 922799308, 781712469, 476477967, 166035806, 258648936, 584193783, 63912897, 350007156, 666702199, 968855178, 629671588, 24514907, 996173970, 363395222, 565042129, 733596141, 267099868, 15311432};
const std::vector<long long> ntt_iroot = {1, 998244352, 86583718, 509520358, 337190230, 87557064, 609441965, 135236158, 304459705, 685443576, 381598368, 335559352, 129292727, 358024708, 814576206, 708402881, 283043518, 3707709, 121392023, 704923114, 950391366, 428961804, 382752275, 469870224};
void ntt(std::vector<long long> &A){
  int N = A.size();
  int M = __builtin_ctz(N);
  for (int i = M - 1; i >= 0; i--){
    for (int j = 0; j < N; j += (2 << i)){
      long long z = 1;
      for (int k = 0; k < (1 << i); k++){
        long long a = A[j + k] + A[j + (1 << i) + k];
        if (a >= MOD){
          a -= MOD;
        }
        long long b = A[j + k] - A[j + (1 << i) + k];
        if (b < 0){
          b += MOD;
        }
        b *= z;
        b %= MOD;
        A[j + k] = a;
        A[j + (1 << i) + k] = b;
        z *= ntt_root[i + 1];
        z %= MOD;
      }
    }
  }
}
void ntt_inv(std::vector<long long> &A){
  int N = A.size();
  int M = __builtin_ctz(N);
  for (int i = 0; i < M; i++){
    for (int j = 0; j < N; j += (2 << i)){
      long long z = 1;
      for (int k = 0; k < (1 << i); k++){
        A[j + (1 << i) + k] *= z;
        A[j + (1 << i) + k] %= MOD;
        long long a = A[j + k] + A[j + (1 << i) + k];
        if (a >= MOD){
          a -= MOD;
        }
        long long b = A[j + k] - A[j + (1 << i) + k];
        if (b < 0){
          b += MOD;
        }
        A[j + k] = a;
        A[j + (1 << i) + k] = b;
        z *= ntt_iroot[i + 1];
        z %= MOD;
      }
    }
  }
  long long Ninv = 1;
  for (int i = 0; i < M; i++){
    Ninv *= HALF;
    Ninv %= MOD;
  }
  for (int i = 0; i < N; i++){
    A[i] *= Ninv;
    A[i] %= MOD;
  }
}
std::vector<long long> convolution(std::vector<long long> A, std::vector<long long> B){
  int sz = A.size() + B.size() - 1;
  int N = sz == 1 ? 1 : 1 << (32 - __builtin_clz(sz - 1));
  A.resize(N);
  B.resize(N);
  ntt(A);
  ntt(B);
  std::vector<long long> ans(N);
  for (int i = 0; i < N; i++){
    ans[i] = A[i] * B[i] % MOD;
  }
  ntt_inv(ans);
  ans.resize(sz);
  return ans;
}

const int N=200007;

vector<pair<int,int>>G[N];
int w[N];
int DP[N][2];

vector<vector<int>>V[2];

vector<int>rek(int p,int l,int r)
{
    if(l>r) return {1,0};
    debug(l,sz(V[p]));
    if(l==r) return V[p][l];
    int m=(l+r)/2;
    vector<int>L=rek(p,l,m);
    vector<int>R=rek(p,m+1,r);
    return convolution(L,R);
}

void dfs(int v,int o,int T)
{
    vector<vector<int>>X[2];
    for(auto [u,t]:G[v])
    {
        if(u==o) continue;
        dfs(u,v,t);
        X[t].pb({DP[u][0],DP[u][1]});
    }
    V[0]=X[0],V[1]=X[1];
    vector<int>V0=rek(0,0,sz(V[0])-1);
    vector<int>V1=rek(1,0,sz(V[1])-1);
    debug("XDD",v);
    debug(V0,sz(V[0]));
    debug(V1,sz(V[1]));
    FOR(k,0,min(sz(V0),sz(V1))-1)
    {

        DP[v][0]=(DP[v][0]+V0[k]*V1[k])%mod;
        if(w[v]==1&&k+1<sz(V0)) DP[v][0]=(DP[v][0]+V0[k+1]*V1[k]%mod)%mod;
        if(w[v]==0&&k+1<sz(V1)) DP[v][0]=(DP[v][0]+V0[k]*V1[k+1]%mod)%mod;


        if(T==0&&k+1<sz(V0)) DP[v][1]=(DP[v][1]+V0[k+1]*V1[k]%mod)%mod;
        if(T==1&&k+1<sz(V1)) DP[v][1]=(DP[v][1]+V0[k]*V1[k+1]%mod)%mod;
        if(T==w[v]) DP[v][1]=(DP[v][1]+V0[k]*V1[k]%mod)%mod;
        else
        {
            if(T==1&&k+2<sz(V0)) DP[v][1]=(DP[v][1]+V0[k+2]*V1[k]%mod)%mod;
            if(T==0&&k+2<sz(V1)) DP[v][1]=(DP[v][1]+V0[k]*V1[k+2]%mod)%mod;
        }
    }
    debug(v,DP[v][0],DP[v][1]);
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s;
    int n;
    cin>>n>>s;
    FOR(i,1,n-1)
    {
        int a,b;
        cin>>a>>b;
        G[a].pb({b,0});
        G[b].pb({a,1});
    }
    FOR(i,1,n) w[i]=(s[i-1]=='S');
    dfs(1,0,0);
    cout<<DP[1][0]<<endl;





    return 0;
}

详细

Test #1:

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

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: 2ms
memory: 11156kb

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: 10020kb

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: 2ms
memory: 11016kb

input:

2
FS
1 2

output:

1

result:

ok 1 number(s): "1"

Test #5:

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

input:

3
FFF
3 1
3 2

output:

1

result:

ok 1 number(s): "1"

Test #6:

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

input:

4
FFFF
1 2
4 2
2 3

output:

1

result:

ok 1 number(s): "1"

Test #7:

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

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: 2ms
memory: 11864kb

input:

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

output:

3

result:

ok 1 number(s): "3"

Test #9:

score: 0
Accepted
time: 334ms
memory: 50292kb

input:

199995
FSSFFFSSSFSFFSSSFSSSSSFSFSSSFSFFSSFSFSFFFSSSFSSFSFFSFFFFSSFFSSSFFSFSFFFFFFFFFFFFFSSSFSSFFFSSSFSFSSSFFFSFFSFSSSSFSFSFSFSSFFSFSFFSFFSSFSSSFSFFSSSFFSFFFSFFSFSFSFSFSSSFSSSSFFSSFFFFFSFSSSFFSSSSSSSSFFFSFSFFSSSFSFFFFFSFFFFSSSSSSSFFFSFFSFSSSFSFSFFFFSFSSFSSFFFSSFFFSSFSFFFSSSFSFSSFFSFSFFSFFSSFSFSSSSFFF...

output:

233157276

result:

ok 1 number(s): "233157276"

Test #10:

score: 0
Accepted
time: 325ms
memory: 52224kb

input:

199996
FSSFFSFSSSFFFFFFSFSSFSFFSSFFFFFFFSSSFFSFSSFSSFSFSSFSSSFFFFSFFFFFFSSSSFFFSFSSFSSFFSSFSFFSSSSSFSFFFFSFSFFSFSSFSFSSSSSFSFSSSFFSSFSSFSFFSFFSSFSFSSFSSSFSSSFFSSFFFFSFFSFSFSFFFSFSSSFFSFFSSSSSSSSFSSFSSFSSSSFSSSFSSSFSFFFFFFSFFSSSFSSFFFSFSFSFSSSFSFFSSSSFSSFSFSSFSSFSFFFSSSFFSSFSFSFFSSSFFFFSSSSSSSFSSSFFF...

output:

509139031

result:

ok 1 number(s): "509139031"

Test #11:

score: 0
Accepted
time: 321ms
memory: 50144kb

input:

199997
FFSSSFSSFSFSFSSSSFSFFSSFFFSSFFSSSSSSFSSSFFSSSFFFSFFSFSFSSSFFSSSFSFFFFFSSFFSSFFFFSSSFFFFSSSSFFFSSFFSSSFSFFSFSFSFSFFSSFSFSSSFSSFSSFSFFSSFFFSFSSFSFFSSSFSFFSFFFFSSFFSFSFFFSFSFFSFSFSFFSSFFFSSFSFFSFSSSSSFFFFFSSSSSSSSSFFFFSSFFFSSFFSFSFSSSFSFSFFSSSSFSFSFFFSSFSSFFSFFSSFSSSSSSSFSSSFSSSFSFSSSSFSFFSSSFSS...

output:

917209642

result:

ok 1 number(s): "917209642"

Test #12:

score: 0
Accepted
time: 316ms
memory: 50988kb

input:

199998
FFFSSSSSFSSSFFFFFFSSSSFSSFFFSFSSFFFSFFFFSFFFFFFFSSSFFSFSFFSFSFSFFSSFSFFSFSFSFSFFSSFFSFFFSFSFSSSSFSSSFFFSSSSFFSFFFFSFFFFFFSFSSFSFSSFFSSFSFFSFSSFSFSSFSSSSFSSFFSSFSSSSSSSSFFSFFSSSSSSSSSSSSFFSSFFFFFFSSFFSFFFFSSFFFSFFSSFFFFFFFFSFSSFSSSSFFSFSFFSSSSFSFSFFFSSFSFFFSFFSSFFSSSSSSSFSSSFFFSSFSSSSSFFFFSFSF...

output:

722227522

result:

ok 1 number(s): "722227522"

Test #13:

score: 0
Accepted
time: 315ms
memory: 50528kb

input:

199999
FSFSFSFSSFSSSFSSFFSFSFFFFFFSSFSSSFSFSSFFSSSFSSSFSSSFSFFSFSFFFFFFSFFFSFSSSSSSFFSFFSFFFFFFFSSSFFFSFFSSSFSFSSSSSFSSFSFSFFSFSFFSSFSFSSFFSFSSSSSSSSSSSFFSSSSSFSFFFSFFSFFFSFFFFFSFFFFSSSSSSSFFFFSFFSSSFSFSSFFFFSSFSFSFSSSFSSSSFSSFSFFSFFFSSFSSFFFSFSFFFSFFFFSFSFSSSSSSSFFSSFFFSSFSSSSSFSSSFFFFFSFSSSFFFSFFS...

output:

22915769

result:

ok 1 number(s): "22915769"

Test #14:

score: -100
Wrong Answer
time: 322ms
memory: 49968kb

input:

200000
SFSSSSFSFFSFFFSFSFSSSSSSSSFFSSSFFSFFSSFFFFFFFFFFSSFFFFFFFSFFSFFFFFSSSFFFSFFSFFFFFSSFFSFFFFSSSFSFFSFSSFSFFFFFFFSFFSSFSSFSSFFSSFSSSSSFSSSSFFSFFFFFSSFSFFSFFFSSSSFFSFFFFFSSSSSSFFFSFFSFFFSFFFFFFSSSFSFFFFFSFSSSFFSSSFSFSFSSSFFFFFSSFFFSFSFFFSFSSSSSFSFFSFSSFFSSFSFFFFFSSFSFSFFFSSSFSFFFFSSSSFFFSSFFSFSFS...

output:

957484376

result:

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