結果

問題 No.679 不思議マーケット
ユーザー PachicobuePachicobue
提出日時 2018-04-27 23:03:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,470 bytes
コンパイル時間 2,397 ms
コンパイル使用メモリ 211,252 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 06:42:06
合計ジャッジ時間 3,398 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 5 ms
4,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,380 KB
testcase_08 WA -
testcase_09 AC 1 ms
4,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 2 ms
4,380 KB
testcase_16 WA -
testcase_17 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
using ll = long long;
using ld = long double;
constexpr ll MOD = 1000000007LL;
template <typename T>
constexpr T INF = numeric_limits<T>::max() / 10;
#define rep(i, n) for (int i = 0; i < n; i++)
class SCC
{
public:
    vector<vector<int>> G, rG, graph;
    vector<int> vs, cmp;
    vector<bool> used;
    int V, cnt;
    SCC(int node_size)
    {
        V = node_size;
        G.resize(V), rG.resize(V);
        used.resize(V), cmp.resize(V);
    }
    void add_edge(int from, int to)
    {
        G[from].push_back(to);
        rG[to].push_back(from);
    }
    void dfs(int u)
    {
        used[u] = true;
        for (int v : G[u]) {
            if (!used[v]) {
                dfs(v);
            }
        }
        vs.push_back(u);
    }
    void dfs(int u, const int k)
    {
        used[u] = true;
        cmp[u] = k;
        for (int v : rG[u]) {
            if (!used[v]) {
                dfs(v, k);
            }
        }
    }
    int solve()
    {  //強連結成分の数を返す
        fill(used.begin(), used.end(), false);
        rep(i, V)
        {
            if (!used[i]) {
                dfs(i);
            }
        }
        fill(used.begin(), used.end(), false);
        cnt = 0;
        for (int i = V - 1; i >= 0; i--) {
            if (!used[vs[i]]) {
                dfs(vs[i], cnt++);
            }
        }
        return cnt;
    }
    void make_graph()
    {
        graph.resize(cnt);
        rep(i, V)
        {
            for (int v : G[i]) {
                if (cmp[i] != cmp[v]) {
                    graph[cmp[i]].push_back(cmp[v]);
                }
            }
        }
    }
};
template <typename T, typename A>
inline ostream& operator<<(ostream& os, const vector<T, A>& v)
{
    os << "[";
    for (const auto& p : v) { os << p << ","; }
    return (os << "]" << endl);
}

int main()
{
    int N, M;
    cin >> N >> M;
    SCC G(N);
    for (int i = 0; i < M; i++) {
        int g, r;
        cin >> g >> r;
        g--;
        for (int j = 0; j < r; j++) {
            int h;
            cin >> h;
            h--;
            G.add_edge(h, g);
        }
    }
    const int sz = G.solve();
    vector<int> v(sz, 0);
    for (int i = 0; i < N; i++) {
        v[G.cmp[i]]++;
    }
    int ans = 0;
    for (int i = 0; i < sz; i++) {
        if (v[i] == 1) { ans++; }
    }
    cout << ans << endl;
    return 0;
}
0