提出 #70485357


ソースコード 拡げる

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = std::forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = std::forward<U>(y), true); }

template <class T> void mkuni(std::vector<T>& v) {
    std::ranges::sort(v);
    auto result = std::ranges::unique(v);
    v.erase(result.begin(), result.end());
}

template <class T> int lwb(const std::vector<T>& v, const T& x) {
    return std::distance(v.begin(), std::ranges::lower_bound(v, x));
}

using namespace std;

using ll = long long;

void solve() {
    int N, M;
    cin >> N >> M;
    vector<int> x(M), y(M);
    for (int i = 0; i < M; i++) {
        cin >> x[i] >> y[i];
        x[i]--, y[i]--;
    }
    vector<int> P(N);
    for (int i = 0; i < N; i++) {
        cin >> P[i];
        P[i]--;
    }

    for (int i = 0; i < M; i++) {
        x[i] = P[x[i]];
        y[i] = P[y[i]];
    }
    vector<vector<int>> G(N);
    vector<int> indeg(N, 0), pre(N, -2);
    for (int i = 0; i < M; i++) {
        G[x[i]].emplace_back(y[i]);
        chmax(pre[y[i]], x[i]);
        indeg[y[i]]++;
    }
    priority_queue<int> pq;
    for (int i = 0; i < N; i++) {
        if (indeg[i] == 0) {
            pq.emplace(i);
        }
    }
    vector<bool> used(N, false);
    int ans = 0;
    for (int i = 0; i < N; i++) {
        if (not used[i]) {
            while (int(pq.size()) > 1) {
                int u = pq.top();
                pq.pop();
                ans++;
                used[u] = true;
            }
            assert(pq.top() == i);
            pq.pop();
        }
        for (int j : G[i]) {
            if (--indeg[j] == 0) {
                pq.emplace(j);
            }
        }
    }

    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int T;
    cin >> T;
    for (; T--;) {
        solve();
    }
    return 0;
}

提出情報

提出日時
問題 A - Communicate Topological Order
ユーザ rniya
言語 C++ 23 (gcc 12.2)
得点 700
コード長 2145 Byte
結果 AC
実行時間 61 ms
メモリ 16000 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 700 / 700
結果
AC × 1
AC × 70
セット名 テストケース
Sample sample.txt
All dense_1.txt, dense_10.txt, dense_2.txt, dense_3.txt, dense_4.txt, dense_5.txt, dense_6.txt, dense_7.txt, dense_8.txt, dense_9.txt, line_1.txt, line_10.txt, line_2.txt, line_3.txt, line_4.txt, line_5.txt, line_6.txt, line_7.txt, line_8.txt, line_9.txt, random_1.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_2.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_3.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, random_37.txt, random_38.txt, random_39.txt, random_4.txt, random_40.txt, random_5.txt, random_6.txt, random_7.txt, random_8.txt, random_9.txt, sample.txt, small_1.txt, small_2.txt, small_3.txt, small_4.txt, small_5.txt, small_6.txt, small_7.txt, small_8.txt, small_9.txt
ケース名 結果 実行時間 メモリ
dense_1.txt AC 39 ms 7148 KiB
dense_10.txt AC 39 ms 8352 KiB
dense_2.txt AC 38 ms 8120 KiB
dense_3.txt AC 38 ms 6404 KiB
dense_4.txt AC 39 ms 8444 KiB
dense_5.txt AC 43 ms 11852 KiB
dense_6.txt AC 39 ms 6404 KiB
dense_7.txt AC 41 ms 10000 KiB
dense_8.txt AC 41 ms 11164 KiB
dense_9.txt AC 38 ms 6584 KiB
line_1.txt AC 41 ms 7620 KiB
line_10.txt AC 47 ms 10008 KiB
line_2.txt AC 45 ms 11604 KiB
line_3.txt AC 38 ms 9624 KiB
line_4.txt AC 39 ms 10304 KiB
line_5.txt AC 42 ms 6884 KiB
line_6.txt AC 42 ms 7724 KiB
line_7.txt AC 45 ms 15848 KiB
line_8.txt AC 42 ms 9612 KiB
line_9.txt AC 42 ms 8112 KiB
random_1.txt AC 59 ms 15860 KiB
random_10.txt AC 37 ms 13000 KiB
random_11.txt AC 51 ms 9660 KiB
random_12.txt AC 59 ms 15844 KiB
random_13.txt AC 52 ms 10900 KiB
random_14.txt AC 48 ms 11912 KiB
random_15.txt AC 58 ms 15928 KiB
random_16.txt AC 49 ms 9816 KiB
random_17.txt AC 46 ms 8892 KiB
random_18.txt AC 49 ms 14568 KiB
random_19.txt AC 44 ms 10272 KiB
random_2.txt AC 61 ms 15892 KiB
random_20.txt AC 57 ms 15852 KiB
random_21.txt AC 57 ms 15692 KiB
random_22.txt AC 60 ms 15876 KiB
random_23.txt AC 57 ms 13700 KiB
random_24.txt AC 53 ms 15164 KiB
random_25.txt AC 57 ms 14744 KiB
random_26.txt AC 37 ms 10668 KiB
random_27.txt AC 51 ms 10444 KiB
random_28.txt AC 61 ms 15856 KiB
random_29.txt AC 58 ms 15836 KiB
random_3.txt AC 39 ms 13624 KiB
random_30.txt AC 49 ms 9620 KiB
random_31.txt AC 51 ms 10764 KiB
random_32.txt AC 52 ms 11340 KiB
random_33.txt AC 43 ms 9120 KiB
random_34.txt AC 57 ms 16000 KiB
random_35.txt AC 50 ms 8188 KiB
random_36.txt AC 51 ms 9648 KiB
random_37.txt AC 58 ms 15844 KiB
random_38.txt AC 41 ms 13796 KiB
random_39.txt AC 50 ms 8020 KiB
random_4.txt AC 54 ms 13496 KiB
random_40.txt AC 34 ms 12836 KiB
random_5.txt AC 54 ms 10004 KiB
random_6.txt AC 60 ms 15872 KiB
random_7.txt AC 57 ms 15940 KiB
random_8.txt AC 49 ms 9580 KiB
random_9.txt AC 53 ms 11340 KiB
sample.txt AC 1 ms 3532 KiB
small_1.txt AC 24 ms 3480 KiB
small_2.txt AC 23 ms 3452 KiB
small_3.txt AC 23 ms 3484 KiB
small_4.txt AC 23 ms 3500 KiB
small_5.txt AC 23 ms 3396 KiB
small_6.txt AC 24 ms 3616 KiB
small_7.txt AC 23 ms 3504 KiB
small_8.txt AC 24 ms 3496 KiB
small_9.txt AC 10 ms 3492 KiB