提出 #61398295


ソースコード 拡げる

#include <bits/stdc++.h>
using namespace std;

#define int long long

vector<bool> visited; // keeps track of which vertices are already visited

// runs depth first search starting at vertex v.
// each visited vertex is appended to the output vector when dfs leaves it.
void dfs(int v, vector<vector<int>> const& adj, vector<int> &output) {
    visited[v] = true;
    for (auto u : adj[v])
        if (!visited[u])
            dfs(u, adj, output);
    output.push_back(v);
}

// input: adj -- adjacency list of G
// output: components -- the strongy connected components in G
// output: adj_cond -- adjacency list of G^SCC (by root vertices)
void strongly_connected_components(vector<vector<int>> const& adj,
                                  vector<vector<int>> &components,
                                  vector<vector<int>> &adj_cond) {
    int n = adj.size();
    components.clear(), adj_cond.clear();

    vector<int> order; // will be a sorted list of G's vertices by exit time

    visited.assign(n, false);

    // first series of depth first searches
    for (int i = 0; i < n; i++)
        if (!visited[i])
            dfs(i, adj, order);

    // create adjacency list of G^T
    vector<vector<int>> adj_rev(n);
    for (int v = 0; v < n; v++)
        for (int u : adj[v])
            adj_rev[u].push_back(v);

    visited.assign(n, false);
    reverse(order.begin(), order.end());

    vector<int> roots(n, 0); // gives the root vertex of a vertex's SCC

    // second series of depth first searches
    for (auto v : order)
        if (!visited[v]) {
            std::vector<int> component;
            dfs(v, adj_rev, component);
            components.push_back(component);
            int root = *min_element(begin(component), end(component));
            for (auto u : component)
                roots[u] = root;
        }

    // add edges to condensation graph
    adj_cond.assign(n, {});
    for (int v = 0; v < n; v++)
        for (auto u : adj[v])
            if (roots[v] != roots[u])
                adj_cond[roots[v]].push_back(roots[u]);
}

int dp[2050][2050];
vector<vector<int>> components, adj;
const int MOD = 998244353;
bool vis[2051];
int recur(int x, int val){
	if (val <= 0) return 0;
	if (dp[x][val] != -1) return dp[x][val];
	vis[x] = 1;
	int ans = 1;
	
	for (auto y : adj[x]){
		int k = recur(y, val);
		ans *= k;
		ans %= MOD;
	}
	
	ans += recur(x, val-1);
	ans %= MOD;
	
	return dp[x][val] = ans;
}

main(){
	int n, m; cin >> n >> m;
	int arr[n]; for (int x = 0; x < n; x++) cin >> arr[x];
	vector<vector<int>> adj_init(n);
	for (int x = 0; x < n; x++){
		if (x != arr[x] - 1) adj_init[arr[x] - 1].push_back(x);
	}
	strongly_connected_components(adj_init, components, adj);
	
	n = components.size();
	memset(dp, -1, sizeof(dp));
	int ans = 1;
	for (int x = 0; x < n; x++){
		if (!vis[x]){
			ans *= recur(x, m);
			ans %= MOD;
		}
	}
	
	cout << ans;
}

提出情報

提出日時
問題 F - Count Arrays
ユーザ shoryu386
言語 C++ 20 (gcc 12.2)
得点 0
コード長 3002 Byte
結果 WA
実行時間 104 ms
メモリ 37016 KiB

コンパイルエラー

Main.cpp:88:1: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
   88 | main(){
      | ^~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 550
結果
AC × 1
WA × 2
AC × 6
WA × 31
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
00_sample_00.txt WA 16 ms 36380 KiB
00_sample_01.txt AC 15 ms 36328 KiB
00_sample_02.txt WA 15 ms 36312 KiB
01_random_00.txt WA 22 ms 36588 KiB
01_random_01.txt WA 46 ms 36720 KiB
01_random_02.txt WA 17 ms 36448 KiB
01_random_03.txt WA 16 ms 36436 KiB
01_random_04.txt WA 16 ms 36424 KiB
01_random_05.txt WA 15 ms 36348 KiB
01_random_06.txt WA 21 ms 36532 KiB
01_random_07.txt WA 17 ms 36432 KiB
01_random_08.txt WA 19 ms 36444 KiB
01_random_09.txt WA 16 ms 36544 KiB
01_random_10.txt WA 18 ms 36448 KiB
01_random_11.txt WA 18 ms 36672 KiB
01_random_12.txt WA 65 ms 36756 KiB
01_random_13.txt WA 66 ms 36832 KiB
01_random_14.txt WA 71 ms 37016 KiB
01_random_15.txt WA 54 ms 36884 KiB
01_random_16.txt WA 56 ms 36828 KiB
01_random_17.txt WA 54 ms 36840 KiB
01_random_18.txt WA 74 ms 36880 KiB
01_random_19.txt WA 65 ms 36944 KiB
01_random_20.txt WA 71 ms 36896 KiB
01_random_21.txt WA 52 ms 36736 KiB
01_random_22.txt WA 59 ms 36812 KiB
01_random_23.txt WA 66 ms 36864 KiB
02_handmade_00.txt WA 69 ms 36980 KiB
02_handmade_01.txt WA 100 ms 36908 KiB
02_handmade_02.txt AC 65 ms 36740 KiB
02_handmade_03.txt WA 69 ms 36920 KiB
02_handmade_04.txt WA 104 ms 36856 KiB
02_handmade_05.txt WA 71 ms 36924 KiB
02_handmade_06.txt AC 15 ms 36444 KiB
02_handmade_07.txt AC 15 ms 36492 KiB
02_handmade_08.txt AC 15 ms 36804 KiB
02_handmade_09.txt AC 15 ms 36816 KiB