結果

問題 No.679 不思議マーケット
ユーザー どららどらら
提出日時 2018-04-28 03:06:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 2,019 bytes
コンパイル時間 3,061 ms
コンパイル使用メモリ 187,480 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-10 07:43:03
合計ジャッジ時間 3,608 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,388 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

template<int MAX_V>
class SCC {
  vector<int> G[MAX_V];
  vector<int> rG[MAX_V];
  vector<int> vs;
  bool used[MAX_V];

  int V;
  vector<int> cmp;

  void dfs(int v){
    used[v] = true;
    for(int i=0; i<G[v].size(); i++)
      if(!used[G[v][i]]) dfs(G[v][i]);
    vs.push_back(v);
  }
  
  void rdfs(int v, int k){
    used[v] = true;
    cmp[v] = k;
    for(int i=0; i<rG[v].size(); i++)
      if(!used[rG[v][i]]) rdfs(rG[v][i], k);
  }
public:
  SCC(int V):V(V),cmp(V){}

  int size(){ return V; }

  vector<int> get_edge(int from){ return G[from]; }
  vector<int> get_cmp(){ return cmp; }
  
  void add_edge(int from, int to){
    G[from].push_back(to);
    rG[to].push_back(from);
  }
  
  int calc(){
    for(int i=0; i<MAX_V; i++) used[i] = 0;
    vs.clear();
    for(int v=0; v<V; v++)
      if(!used[v]) dfs(v);
    for(int i=0; i<MAX_V; i++) used[i] = 0;
    int k = 0;
    for(int i=vs.size()-1; i>=0; i--)
      if(!used[vs[i]]) rdfs(vs[i], k++);
    return k;
  }
};


int main(){
  int n, m;
  cin >> n >> m;

  vector<vector<int>> memo(n);
  
  SCC<505> scc(n);
  rep(i,m){
    int g, r;
    cin >> g >> r;
    g--;
    vector<int> h(r);
    rep(j,r){
      cin >> h[j];
      h[j]--;
      scc.add_edge(h[j],g);
    }
    memo[g] = h;
  }

  scc.calc();
  vector<int> ret = scc.get_cmp();
  vector<pair<int,int>> v;
  rep(i,ret.size()){
    v.emplace_back(ret[i],i);
  }
  sort(ALLOF(v));

  map<int,int> get;
  rep(i,v.size()){
    int x = v[i].second;
    if(memo[x].size() == 0) get[x] = 1;
    else{
      bool flg = true;
      rep(j,memo[x].size()){
        if(get.count(memo[x][j])==0) flg = false;
      }
      if(flg) get[x] = 1;
    }
  }

  cout << get.size() << endl;
  
  return 0;
}

0