結果

問題 No.1813 Magical Stones
ユーザー chocoruskchocorusk
提出日時 2022-01-14 23:11:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,152 bytes
コンパイル時間 3,356 ms
コンパイル使用メモリ 196,248 KB
実行使用メモリ 41,004 KB
最終ジャッジ日時 2024-04-30 17:04:56
合計ジャッジ時間 8,793 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 32 ms
17,516 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 40 ms
11,776 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 34 ms
17,392 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 211 ms
24,704 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 274 ms
40,844 KB
testcase_21 AC 282 ms
40,932 KB
testcase_22 AC 274 ms
40,876 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 25 ms
8,448 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 9 ms
6,944 KB
testcase_35 AC 19 ms
6,944 KB
testcase_36 AC 5 ms
6,940 KB
testcase_37 AC 62 ms
8,832 KB
testcase_38 AC 32 ms
12,672 KB
testcase_39 AC 164 ms
29,480 KB
testcase_40 AC 6 ms
6,940 KB
testcase_41 AC 7 ms
6,944 KB
testcase_42 AC 20 ms
6,944 KB
testcase_43 AC 18 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
struct SCC{
    vector<vector<int>> g, gr;
    int n, k;
    vector<int> cmp, vs;
    vector<bool> used;
    void dfs(int x){
        used[x]=1;
        for(auto y:g[x]){
            if(!used[y]) dfs(y);
        }
        vs.push_back(x);
    }
    void rdfs(int v, int k){
        used[v]=1;
        cmp[v]=k;
        for(auto y:gr[v]){
            if(!used[y]) rdfs(y, k);
        }
    }
    SCC(const vector<vector<int>> &g):g(g), n(g.size()), cmp(n), used(n){
        gr.resize(n);
        for(int x=0; x<n; x++) for(auto y:g[x]) gr[y].push_back(x);
        for(int i=0; i<n; i++){
            if(!used[i]) dfs(i);
        }
        fill(used.begin(), used.end(), 0);
        k=0;
        for(int i=vs.size()-1; i>=0; i--){
            if(!used[vs[i]]) rdfs(vs[i], k++);
        }
    }
};
struct unionfind{
    vector<int> par, sz;
    unionfind() {}
    unionfind(int n):par(n), sz(n, 1){
        for(int i=0; i<n; i++) par[i]=i;
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }
    void unite(int x, int y){
        x=find(x); y=find(y);
        if(x==y) return;
        if(sz[x]>sz[y]) swap(x, y);
        par[x]=y;
        sz[y]+=sz[x];
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};
int n, m;
vector<vector<int>> g;
int main()
{
    cin>>n>>m;
    g.resize(n);
    unionfind uf(n);
    for(int i=0; i<m; i++){
        int a, b;cin>>a>>b;a--;b--;
        g[a].push_back(b);
        uf.unite(a, b);
    }
    SCC scc(g);
    if(scc.k==1){
        cout<<0<<endl;
        return 0;
    }
    vector<vector<int>> v(n);
    for(int i=0; i<n; i++){
        v[uf.find(i)].push_back(i);
    }
    int c1=0, c2=0;
    for(int i=0; i<n; i++){
        if(v[i].empty()) continue;
        int n1=v[i].size();
        vector<vector<int>> g1(n1);
        for(int j=0; j<n1; j++){
            int x=v[i][j];
            for(auto y:g[x]){
                int k=lower_bound(v[i].begin(), v[i].end(), y)-v[i].begin();
                g1[j].push_back(k);
            }
        }
        SCC scc(g1);
        int n2=scc.k;
        if(n2==1){
            c1++;c2++;continue;
        }
        vector<int> d(n2), e(n2);
        for(int j=0; j<n1; j++){
            for(auto k:g1[j]){
                d[scc.cmp[j]]++;
                e[scc.cmp[k]]++;
            }
        }
        for(int j=0; j<n2; j++){
            if(d[j]==0) c1++;
            if(e[j]==0) c2++;
        }
    }
    cout<<max(c1, c2)<<endl;
    return 0;
}
0