結果

問題 No.1813 Magical Stones
ユーザー chocoruskchocorusk
提出日時 2022-01-14 23:33:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 3,267 bytes
コンパイル時間 4,015 ms
コンパイル使用メモリ 197,108 KB
実行使用メモリ 41,092 KB
最終ジャッジ日時 2024-04-30 17:57:31
合計ジャッジ時間 8,813 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 33 ms
17,388 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 5 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 44 ms
11,648 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 33 ms
17,428 KB
testcase_15 AC 267 ms
41,092 KB
testcase_16 AC 264 ms
40,804 KB
testcase_17 AC 174 ms
24,832 KB
testcase_18 AC 270 ms
40,824 KB
testcase_19 AC 268 ms
40,836 KB
testcase_20 AC 265 ms
40,856 KB
testcase_21 AC 269 ms
41,004 KB
testcase_22 AC 271 ms
41,000 KB
testcase_23 AC 272 ms
40,936 KB
testcase_24 AC 4 ms
6,944 KB
testcase_25 AC 25 ms
8,448 KB
testcase_26 AC 8 ms
6,940 KB
testcase_27 AC 169 ms
30,072 KB
testcase_28 AC 216 ms
28,544 KB
testcase_29 AC 208 ms
33,216 KB
testcase_30 AC 194 ms
31,108 KB
testcase_31 AC 249 ms
38,708 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 1 ms
6,940 KB
testcase_34 AC 8 ms
6,944 KB
testcase_35 AC 19 ms
6,940 KB
testcase_36 AC 5 ms
6,940 KB
testcase_37 AC 59 ms
8,704 KB
testcase_38 AC 30 ms
12,544 KB
testcase_39 AC 154 ms
29,608 KB
testcase_40 AC 6 ms
6,940 KB
testcase_41 AC 6 ms
6,940 KB
testcase_42 AC 20 ms
6,940 KB
testcase_43 AC 17 ms
6,940 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]){
                if(scc.cmp[j]==scc.cmp[k]) continue;
                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