結果

問題 No.1023 Cyclic Tour
ユーザー niuez
提出日時 2020-04-10 22:34:02
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,241 bytes
コンパイル時間 2,210 ms
コンパイル使用メモリ 182,576 KB
実行使用メモリ 61,412 KB
最終ジャッジ日時 2024-09-15 21:07:52
合計ジャッジ時間 21,748 ms
ジャッジサーバーID
(参考情報)
judge6 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 27 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()

template<class T>
static inline std::vector<T> ndvec(size_t&& n, T val) noexcept {
  return std::vector<T>(n, std::forward<T>(val));
}

template<class... Tail>
static inline auto ndvec(size_t&& n, Tail&&... tail) noexcept {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...));
}

template<class T, class Cond>
struct chain {
  Cond cond; chain(Cond cond) : cond(cond) {}
  bool operator()(T& a, const T& b) const {
    if(cond(a, b)) { a = b; return true; }
    return false;
  }
};
template<class T, class Cond>
chain<T, Cond> make_chain(Cond cond) { return chain<T, Cond>(cond); }

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

vector<i64> strongly_connected_components(const vector<vector<i64>>& g) {
  i64 n = g.size();
  vector<vector<i64>> rg(n);
  for(int i = 0;i < n;i++) {
    for(auto j: g[i]) {
      rg[j].push_back(i);
    }
  }

  vector<bool> vis(n,false);
  vector<i64> vs;
  vector<i64> res(n,-1);
  function<void(int)> dfs = [&](int v) {
    vis[v] = true;
    for(auto& t : g[v]) {
      if(!vis[t]) dfs(t);
    }
    vs.push_back(v);
  };

  function<void(int,int)> rdfs = [&](int v,int k) {
    vis[v] = true;
    res[v] = k;
    for(auto to : rg[v]) {
      if(!vis[to]) rdfs(to,k);
    }
  };

  for(int i = 0;i < n;i++) {
    if(!vis[i]) dfs(i);
  }

  vis.assign(n,false);
  int k = 0;
  for(int i = n - 1;i >= 0;i--) {
    if(!vis[vs[i]]) rdfs(vs[i] , k++);
  }
  return res;
}

vector<vector<i64>> G;

int main() {
  i64 N, M;
  cin >> N >> M;
  G.resize(N * 3);
  rep(i,0,N) {
    G[i * 3].push_back(i * 3 + 1);
    G[i * 3 + 1].push_back(i * 3 + 2);
  }
  rep(i,0,M) {
    i64 a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if(c == 1) {
      G[a * 3 + 1].push_back(b * 3 + 2);
      G[b * 3 + 1].push_back(a * 3 + 2);
    }
    else {
      G[a * 3 + 2].push_back(b * 3);
    }
  }
  auto res = strongly_connected_components(G);
  i64 ans = *std::max_element(all(res));
  if(ans + 1 == N * 3) {
    cout << "No" << endl;
  }
  else  {
    cout << "Yes" << endl;
  }
}
0