結果

問題 No.1194 Replace
ユーザー ktr216ktr216
提出日時 2020-08-22 17:58:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 966 bytes
コンパイル時間 1,950 ms
コンパイル使用メモリ 166,580 KB
実行使用メモリ 814,580 KB
最終ジャッジ日時 2024-04-23 11:52:26
合計ジャッジ時間 6,596 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 MLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

int N, M;
vector<vector<int>> edge(200001);
vector<int> f(200001, -1);

int solve(int x) {
    //cout << "solve(" << x << ")\n";
    if (f[x] != -1) return f[x];
    if (x == N) {
        f[x] = x;
        return x;
    }
    int ret = x;
    rep(i, edge[x].size()) {
        ret = max(ret, solve(edge[x][i]));
        //cout << ret << "!\n";
    }
    f[x] = ret;
    return ret;
}

int main() {
    int B, C;
    cin >> N >> M;
    rep(i, M) {
        cin >> B >> C;
        edge[B].push_back(C);
    }
    /*
    rep(i, N + 1) {
        cout << i << " " << edge[i].size() << "\n";
        rep(j, edge[i].size()) {
            cout << edge[i][j] << " ";
        }
        cout << "\n";
    }
    */
    ll ans = 0;
    rep(i, N) {
        ans += solve(i + 1);
        //cout << i + 1 << " " << solve(i + 1) << "\n";
    }
    cout << ans << "\n";
}
0