結果

問題 No.2780 The Bottle Imp
ユーザー GOTKAKOGOTKAKO
提出日時 2024-06-07 21:50:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 2,160 bytes
コンパイル時間 2,863 ms
コンパイル使用メモリ 230,572 KB
実行使用メモリ 36,116 KB
最終ジャッジ日時 2024-06-08 10:29:15
合計ジャッジ時間 4,802 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 27 ms
11,736 KB
testcase_08 AC 26 ms
11,888 KB
testcase_09 AC 25 ms
11,772 KB
testcase_10 AC 25 ms
11,748 KB
testcase_11 AC 25 ms
11,760 KB
testcase_12 AC 44 ms
18,652 KB
testcase_13 AC 45 ms
18,524 KB
testcase_14 AC 17 ms
6,912 KB
testcase_15 AC 17 ms
7,040 KB
testcase_16 AC 14 ms
6,272 KB
testcase_17 AC 13 ms
6,144 KB
testcase_18 AC 16 ms
6,912 KB
testcase_19 AC 18 ms
6,912 KB
testcase_20 AC 16 ms
6,912 KB
testcase_21 AC 13 ms
6,016 KB
testcase_22 AC 10 ms
6,272 KB
testcase_23 AC 13 ms
6,272 KB
testcase_24 AC 22 ms
9,672 KB
testcase_25 AC 34 ms
14,320 KB
testcase_26 AC 19 ms
8,832 KB
testcase_27 AC 26 ms
12,288 KB
testcase_28 AC 24 ms
12,288 KB
testcase_29 AC 18 ms
11,712 KB
testcase_30 AC 16 ms
10,728 KB
testcase_31 AC 34 ms
14,976 KB
testcase_32 AC 8 ms
5,376 KB
testcase_33 AC 69 ms
36,116 KB
testcase_34 AC 52 ms
31,644 KB
testcase_35 AC 7 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 7 ms
5,376 KB
testcase_39 AC 45 ms
21,600 KB
testcase_40 AC 44 ms
21,724 KB
testcase_41 AC 27 ms
16,128 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class SCC{
    public:
    int siz = 0,name = 0,nowc = 0;
    vector<bool> already;
    vector<int> ord,low,belong,topo;
    vector<vector<int>> Graph,cycle;
    stack<int> St;
 
    void dfs(int pos){
        low.at(pos) = (ord.at(pos) = name++); St.push(pos);
        for(auto to : Graph.at(pos)){
            if(ord.at(to) == -1){
                dfs(to);
                low.at(pos) = min(low.at(pos),low.at(to));
            }
            else if(!already.at(to)) low.at(pos) = min(low.at(pos),ord.at(to));
        }
        if(low.at(pos) == ord.at(pos)){
            cycle.push_back({});
            while(true){
                int s = St.top(); St.pop();
                cycle.back().push_back(s);
                belong.at(s) = nowc; already.at(s) = true;
                if(s == pos) break;
            }
            nowc++;
        }
    }
 
    void make(vector<vector<int>> &G){
        Graph = G; siz = G.size();
        ord.resize(siz,-1); low.resize(siz,1001001001);
        already.resize(siz); belong.resize(siz);
 
        for(int i=0; i<siz; i++) if(ord.at(i) == -1) dfs(i);
        for(int i=0; i<siz; i++) belong.at(i) = (nowc-1)-belong.at(i);
        reverse(cycle.begin(),cycle.end());
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N; cin >> N;
    vector<vector<int>> Graph(N);
    for(int i=0; i<N; i++){
        int K; cin >> K;
        for(int k=0; k<K; k++){
            int to; cin >> to; to--;
            Graph.at(i).push_back(to);
        }
    }

    SCC Z; Z.make(Graph);
    if(Z.belong.at(0) != 0){cout << "No" << endl; return 0;}

    reverse(Z.cycle.begin(),Z.cycle.end());
    set<int> S;
    bool end = true;
    for(auto &Vs : Z.cycle){
        set<int> S2;
        for(auto pos : Vs) S2.insert(pos);
        if(end){end = false; S = S2; continue;}

        bool ok = false;
        for(auto pos : Vs){
            for(auto to : Graph.at(pos)) if(S.count(to)){ok = true; break;}
            if(ok) break;
        }
        if(!ok){cout << "No" << endl; return 0;}
        S = S2; continue;
    }
    cout << "Yes" << endl;
}
0