QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#570633 | #8836. Highway Hoax | Djangle162857# | WA | 0ms | 3876kb | C++14 | 2.0kb | 2024-09-17 16:49:45 | 2024-09-17 16:49:48 |
Judging History
answer
#include <bits/stdc++.h>
#define debug(x) cout << #x << " :== " << x << endl
#define FINISH cout << "FINISH" << endl;
#define el '\n'
using namespace std;
using ll = long long;
const int inf = 1000000000;
const int mod = 998244353;
ll up(ll x)
{
return (x % mod + mod) % mod;
}
void solve()
{
int n;
cin >> n;
string s;
cin >> s;
s = " " + s;
vector<vector<int>> edge1(n + 1), edge(n + 1);
vector<int> siz(n + 1, 0);
vector<int> col(n + 1);
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
edge1[x].push_back(y);
}
auto bfs = [&](int st) {
int cnt = 0;
queue<int> q;
q.push(st);
// cout << "START " << st << endl;
while (!q.empty()) {
int x = q.front();
// cout << x << " ";
q.pop();
cnt++;
if (col[x]) {
edge[st].push_back(col[x]);
edge[col[x]].push_back(st);
}
else
col[x] = st;
if (s[x] == 'S' && x != st) {
continue;
}
for (auto to : edge1[x]) {
q.push(to);
}
}
/*cout << endl;
FINISH*/
return cnt;
};
for (int i = 1; i <= n; i++) {
if (s[i] == 'S') {
siz[i] = bfs(i);
}
}
/*for (int i = 1; i <= n; i++) {
for (auto j : edge[i]) {
cout << "edge " << i << " " << j << endl;
}
}
FINISH*/
vector<int> vis(n + 1, 0);
vector<array<ll, 2>> dp(n + 1);
vector<int> res;
function<void(int, int)> dfs = [&](int x, int fa) {
vis[x] = 1;
dp[x][0] = siz[x] - 1;
dp[x][1] = 1;
for (auto to : edge[x]) {
if (to == fa)
continue;
dfs(to, x);
dp[x][1] = dp[to][0] * dp[x][1] % mod + dp[x][0] * dp[to][1] % mod;
dp[x][0] = dp[to][0] * dp[x][0] % mod;
}
};
for (int i = 1; i <= n; i++) {
if (s[i] == 'S' && !vis[i]) {
dfs(i, i);
res.push_back(i);
}
}
ll ans = 1;
for (int i = 0; i < res.size(); i++) {
ans = ans * (dp[res[i]][0] + dp[res[i]][1]) % mod;
}
cout << up(ans) << el;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3568kb
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: 3672kb
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: 3828kb
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: 3860kb
input:
2 FS 1 2
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
3 FFF 3 1 3 2
output:
1
result:
ok 1 number(s): "1"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3876kb
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: 3640kb
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: 0ms
memory: 3568kb
input:
6 FSSSSF 5 3 2 5 1 2 2 6 4 2
output:
2
result:
wrong answer 1st numbers differ - expected: '3', found: '2'