結果

問題 No.1605 Matrix Shape
ユーザー nawawannawawan
提出日時 2021-07-16 22:35:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,354 bytes
コンパイル時間 2,349 ms
コンパイル使用メモリ 185,508 KB
実行使用メモリ 67,572 KB
最終ジャッジ日時 2023-09-20 14:54:59
合計ジャッジ時間 9,655 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 47 ms
41,388 KB
testcase_02 AC 49 ms
41,352 KB
testcase_03 AC 47 ms
41,336 KB
testcase_04 AC 49 ms
41,544 KB
testcase_05 AC 48 ms
41,412 KB
testcase_06 AC 48 ms
41,392 KB
testcase_07 AC 49 ms
41,360 KB
testcase_08 AC 48 ms
41,340 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 49 ms
41,532 KB
testcase_12 AC 47 ms
41,336 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 49 ms
41,560 KB
testcase_17 AC 49 ms
41,432 KB
testcase_18 AC 48 ms
41,432 KB
testcase_19 AC 320 ms
43,628 KB
testcase_20 AC 280 ms
44,432 KB
testcase_21 WA -
testcase_22 AC 378 ms
37,772 KB
testcase_23 AC 319 ms
40,928 KB
testcase_24 WA -
testcase_25 AC 136 ms
46,332 KB
testcase_26 AC 134 ms
46,264 KB
testcase_27 WA -
testcase_28 AC 135 ms
46,456 KB
testcase_29 WA -
testcase_30 AC 266 ms
37,964 KB
testcase_31 WA -
testcase_32 AC 247 ms
43,856 KB
testcase_33 AC 239 ms
43,704 KB
testcase_34 AC 154 ms
45,820 KB
testcase_35 AC 427 ms
67,572 KB
testcase_36 AC 167 ms
42,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct SCC{
    int N;
    vector<int> order;//分解後のトポロジカル順序(サイズ = 分解後のノード数)
    vector<vector<int>> cnt;
    SCC(int V){
        G.resize(V);
        rG.resize(V);
        N = V;
        order.resize(V);
    }
    void add(int s, int t){
        G[s].push_back(t);
        rG[t].push_back(s);
    }
    vector<vector<int>> build(){//分解後の頂点を返す
        used.resize(N, false);
        for(int i = 0; i < N; i++){
            if(!used[i]) dfs(i);
        }
        used.assign(N, false);  
        int k = 0;
        for(int i = (int)temp.size() - 1; i >= 0; i--){
            if(!used[temp[i]]) {
                rdfs(temp[i], k);
                k++;
            }
        }
        cnt.resize(k);
        for(int i = 0; i < N; i++){
            cnt[order[i]].push_back(i);
        }
        return cnt;
    }
private:
    vector<vector<int>> G, rG;
    vector<bool> used;
    vector<int> temp;
    void dfs(int s){
        used[s] = true;
        for(int j: G[s]){
            if(!used[j]) dfs(j);
        }
        temp.push_back(s);
    }
    void rdfs(int s, int k){
        used[s] = true;
        order[s] = k;
        for(int j: rG[s]){
            if(!used[j]) rdfs(j, k);
        }
    }
};
int dfs(int now, vector<vector<int>>&G, vector<int>&d){
    if(d[now] != -1) return d[now];
    int temp = 0;
    for(int t: G[now]){
        temp = max(temp, dfs(t, G, d));
    }
    temp+= 1;
    return d[now] = temp;
}
int main(){
    int N;
    cin >> N;
    vector<int> H(N), W(N);
    SCC scc(200010);
    for(int i = 0; i < N; i++){
        cin >> H[i] >> W[i];
        H[i]--;
        W[i]--;
        scc.add(H[i], W[i]);
    }
    vector<vector<int>> cnt = scc.build();
    vector<vector<int>> G(cnt.size());
    vector<int> d(cnt.size());
    set<int> s;
    for(int i = 0; i < N; i++){
        s.insert(scc.order[W[i]]);
        s.insert(scc.order[H[i]]);
        G[scc.order[H[i]]].push_back(scc.order[W[i]]);
    }
    if(s.size() == 1){
        cout << cnt[scc.order[H[0]]].size() << endl;
    }
    else{
        for(int i = 0; i < cnt.size(); i++){
            dfs(i, G, d);
        }
        int temp = *max_element(d.begin(), d.end());
        if(temp == (int)s.size()) cout << 1 << endl;
        else cout << 0 << endl;
    }
}
0