結果

問題 No.1813 Magical Stones
ユーザー ChanyuhChanyuh
提出日時 2022-01-27 15:02:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,682 bytes
コンパイル時間 1,839 ms
コンパイル使用メモリ 141,748 KB
実行使用メモリ 19,308 KB
最終ジャッジ日時 2023-08-26 15:16:47
合計ジャッジ時間 7,418 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 20 ms
16,696 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 20 ms
8,564 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 19 ms
16,884 KB
testcase_15 AC 109 ms
19,164 KB
testcase_16 AC 106 ms
19,080 KB
testcase_17 WA -
testcase_18 AC 107 ms
19,276 KB
testcase_19 AC 104 ms
19,308 KB
testcase_20 AC 103 ms
19,296 KB
testcase_21 AC 106 ms
19,064 KB
testcase_22 AC 106 ms
19,096 KB
testcase_23 AC 104 ms
19,156 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 13 ms
6,444 KB
testcase_26 AC 5 ms
4,380 KB
testcase_27 AC 68 ms
15,604 KB
testcase_28 AC 73 ms
12,448 KB
testcase_29 AC 80 ms
15,476 KB
testcase_30 AC 71 ms
14,504 KB
testcase_31 AC 94 ms
18,592 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 5 ms
4,376 KB
testcase_35 WA -
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 20 ms
5,544 KB
testcase_38 AC 17 ms
12,328 KB
testcase_39 AC 64 ms
16,372 KB
testcase_40 AC 3 ms
4,376 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 7 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<array>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}

struct SCC{
  vector<vector<int> > G,R,T,C;//T:強連結分解後のグラフ
  vector<int> vs,used,blg;
  SCC(int n):G(n),R(n),used(n),blg(n){}

  void add_edge(int u,int v){
    G[u].emplace_back(v);
    R[v].emplace_back(u);
  }

  int size(){
    return T.size();
  }

  void dfs(int v){
    used[v]=1;
    for(int u:G[v])
      if(!used[u]) dfs(u);
    vs.emplace_back(v);
  }

  void rdfs(int v,int k){
    used[v]=1;
    blg[v]=k;
    C[k].emplace_back(v);
    for(int u:R[v])
      if(!used[u]) rdfs(u,k);
  }

  int build(){
    int n=G.size();
    for(int v=0;v<n;v++)
      if(!used[v]) dfs(v);

    fill(used.begin(),used.end(),0);
    int k=0;
    for(int i=n-1;i>=0;i--){
      if(!used[vs[i]]){
        T.emplace_back();
        C.emplace_back();
        rdfs(vs[i],k++);
      }
    }
    for(int v=0;v<n;v++)
      for(int u:G[v])
        if(blg[v]!=blg[u])
          T[blg[v]].push_back(blg[u]);

    for(int i=0;i<k;i++){
      sort(T[i].begin(),T[i].end());
      T[i].erase(unique(T[i].begin(),T[i].end()),T[i].end());
    }
    return k;
  }
  int operator[](int k) const{return blg[k];};//頂点kの属する強連結成分
};

int n,m;

void solve(){
    cin >> n >> m;
    SCC scc(n);
    rep(i,m){
        int a,b;cin >> a >> b;a--;b--;
        scc.add_edge(a,b);
        //cout << a << " " << b << endl;
    }
    
    int K=scc.build();
    int A=0,B=0;
    vector<bool> d(K,false);
    rep(i,K){
        if(scc.T[i].size()==0) A++;
        for(int s:scc.T[i]){
            d[s]=true;
        }
    }
    rep(i,K) if(!d[i]) B++;
    cout << max(A,B) << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0