Submission #61393739
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5505, M = 3*N;
const int mod = 998244353;
int n, m;
int a[N];
vector<int> e[N], g[N];
vector<pair<int,int>> v[N];
int head[M],to[M],nxt[M],etot=1;
bool cut[M];
int dft,low[N],dfn[N];
int cnt, f[N];
int bel[N];
void add(int x,int y){
to[++etot]=y;
nxt[etot]=head[x];
head[x]=etot;
}
void tarjan(int x,int lst){
dfn[x]=low[x]=++dft;
for(int i=head[x];i;i=nxt[i]) if(i!=lst) {
int y=to[i];
if(!dfn[y]){
tarjan(y,i);
low[x]=min(low[x],low[y]);
if(dfn[x]<=low[y]){
cut[i]=cut[i^1]=1;
}
}else{
low[x]=min(low[x],dfn[y]);
}
}
}
void dfs1(int x, int beg){
bel[x] = beg;
f[x]=1;
for(int i=head[x];i;i=nxt[i]){
int y=to[i];
if(!cut[i] && !f[y]) {
dfs1(y, beg);
}
}
}
int vis2[N];
ll dp[N][N], sm[N][N];
void dfs(int now, int fa) {
vis2[now] = 1;
for (auto i : v[now]) if (i.first != fa) dfs(i.first, now);
for (int i = 1; i <= m; i++) {
dp[now][i] = 1;
for (auto tt : v[now]) {
int j = tt.first, tp = tt.second;
if (j == fa) continue;
if (tp == 0) (dp[now][i] *= sm[j][i]) %= mod;
else (dp[now][i] *= (sm[j][m] - sm[j][i-1] + mod) % mod) %= mod;
}
(sm[now][i] = sm[now][i - 1] + dp[now][i]) %= mod;
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
add(i, a[i]), add(a[i], i);
e[a[i]].push_back(i);
e[i].push_back(a[i]);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) {
tarjan(i, 0);
}
}
for(int i=1;i<=n;i++){
if(!f[i]){
dfs1(i, (++cnt));
}
}
// for (int i = 1; i <= n; i++) cout << bel[i] << ' '; cout << '\n';
for (int i = 1; i <= n; i++) {
if (bel[i] == bel[a[i]]) continue;
g[bel[a[i]]].push_back(bel[i]);
v[bel[a[i]]].push_back({bel[i], 0});
v[bel[i]].push_back({bel[a[i]], 1});
}
ll ans = 1;
for (int i = 1; i <= cnt; i++) {
if (!vis2[i]) {
dfs(i, 0);
(ans *= sm[i][m]) %= mod;
}
}
cout << ans;
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | F - Count Arrays |
| User | Nicrobott |
| Language | C++ 20 (gcc 12.2) |
| Score | 550 |
| Code Size | 2470 Byte |
| Status | AC |
| Exec Time | 97 ms |
| Memory | 84424 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 550 / 550 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt, 02_handmade_07.txt, 02_handmade_08.txt, 02_handmade_09.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_00.txt | AC | 1 ms | 3552 KiB |
| 00_sample_01.txt | AC | 1 ms | 3596 KiB |
| 00_sample_02.txt | AC | 1 ms | 3580 KiB |
| 01_random_00.txt | AC | 9 ms | 16608 KiB |
| 01_random_01.txt | AC | 28 ms | 44880 KiB |
| 01_random_02.txt | AC | 4 ms | 7792 KiB |
| 01_random_03.txt | AC | 2 ms | 4576 KiB |
| 01_random_04.txt | AC | 3 ms | 6016 KiB |
| 01_random_05.txt | AC | 1 ms | 3712 KiB |
| 01_random_06.txt | AC | 6 ms | 10560 KiB |
| 01_random_07.txt | AC | 5 ms | 8572 KiB |
| 01_random_08.txt | AC | 4 ms | 7956 KiB |
| 01_random_09.txt | AC | 3 ms | 5752 KiB |
| 01_random_10.txt | AC | 4 ms | 8160 KiB |
| 01_random_11.txt | AC | 6 ms | 11180 KiB |
| 01_random_12.txt | AC | 51 ms | 84096 KiB |
| 01_random_13.txt | AC | 51 ms | 83720 KiB |
| 01_random_14.txt | AC | 54 ms | 82672 KiB |
| 01_random_15.txt | AC | 34 ms | 53260 KiB |
| 01_random_16.txt | AC | 36 ms | 58148 KiB |
| 01_random_17.txt | AC | 30 ms | 47632 KiB |
| 01_random_18.txt | AC | 58 ms | 81740 KiB |
| 01_random_19.txt | AC | 40 ms | 41212 KiB |
| 01_random_20.txt | AC | 54 ms | 83316 KiB |
| 01_random_21.txt | AC | 29 ms | 32452 KiB |
| 01_random_22.txt | AC | 34 ms | 34036 KiB |
| 01_random_23.txt | AC | 41 ms | 56848 KiB |
| 02_handmade_00.txt | AC | 54 ms | 84424 KiB |
| 02_handmade_01.txt | AC | 97 ms | 84292 KiB |
| 02_handmade_02.txt | AC | 51 ms | 83936 KiB |
| 02_handmade_03.txt | AC | 53 ms | 84116 KiB |
| 02_handmade_04.txt | AC | 95 ms | 84004 KiB |
| 02_handmade_05.txt | AC | 54 ms | 84172 KiB |
| 02_handmade_06.txt | AC | 1 ms | 3656 KiB |
| 02_handmade_07.txt | AC | 1 ms | 3592 KiB |
| 02_handmade_08.txt | AC | 2 ms | 3912 KiB |
| 02_handmade_09.txt | AC | 2 ms | 3932 KiB |