結果

問題 No.1023 Cyclic Tour
ユーザー niuezniuez
提出日時 2020-04-10 23:00:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 239 ms / 2,000 ms
コード長 2,968 bytes
コンパイル時間 4,292 ms
コンパイル使用メモリ 185,676 KB
実行使用メモリ 25,156 KB
最終ジャッジ日時 2023-10-14 04:08:52
合計ジャッジ時間 13,562 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 1 ms
4,372 KB
testcase_04 AC 67 ms
4,372 KB
testcase_05 AC 68 ms
4,368 KB
testcase_06 AC 72 ms
4,372 KB
testcase_07 AC 69 ms
4,372 KB
testcase_08 AC 83 ms
8,844 KB
testcase_09 AC 110 ms
14,720 KB
testcase_10 AC 106 ms
14,900 KB
testcase_11 AC 115 ms
15,260 KB
testcase_12 AC 121 ms
16,640 KB
testcase_13 AC 112 ms
16,112 KB
testcase_14 AC 119 ms
15,792 KB
testcase_15 AC 122 ms
16,232 KB
testcase_16 AC 235 ms
21,920 KB
testcase_17 AC 234 ms
22,164 KB
testcase_18 AC 239 ms
22,476 KB
testcase_19 AC 224 ms
21,748 KB
testcase_20 AC 153 ms
14,760 KB
testcase_21 AC 156 ms
15,128 KB
testcase_22 AC 176 ms
16,276 KB
testcase_23 AC 186 ms
16,800 KB
testcase_24 AC 214 ms
19,308 KB
testcase_25 AC 153 ms
14,940 KB
testcase_26 AC 159 ms
15,116 KB
testcase_27 AC 150 ms
14,108 KB
testcase_28 AC 215 ms
18,972 KB
testcase_29 AC 216 ms
20,184 KB
testcase_30 AC 216 ms
18,880 KB
testcase_31 AC 191 ms
18,920 KB
testcase_32 AC 208 ms
20,672 KB
testcase_33 AC 212 ms
19,268 KB
testcase_34 AC 55 ms
4,912 KB
testcase_35 AC 124 ms
5,820 KB
testcase_36 AC 161 ms
15,628 KB
testcase_37 AC 189 ms
18,640 KB
testcase_38 AC 210 ms
19,484 KB
testcase_39 AC 183 ms
18,220 KB
testcase_40 AC 193 ms
17,272 KB
testcase_41 AC 192 ms
17,476 KB
testcase_42 AC 147 ms
8,252 KB
testcase_43 AC 142 ms
10,272 KB
testcase_44 AC 77 ms
10,188 KB
testcase_45 AC 141 ms
25,156 KB
testcase_46 AC 152 ms
25,156 KB
testcase_47 AC 77 ms
10,444 KB
testcase_48 AC 157 ms
14,660 KB
testcase_49 AC 157 ms
14,612 KB
testcase_50 AC 153 ms
14,384 KB
testcase_51 AC 145 ms
8,052 KB
testcase_52 AC 151 ms
8,468 KB
権限があれば一括ダウンロードができます

ソースコード

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;
}

#include <vector>
#include <set>
using namespace std;

struct union_find {
  vector<int> par;
  vector<int> rank;
  union_find(int n) : par(n) , rank(n) {
    for(int i = 0;i < n;i++) par[i] = i;
  }
  int root(int i) {
    return par[i] == i ? i : par[i] = root(par[i]);
  }
  /* unite x, y return parent */
  int unite(int x,int y) {
    x = root(x);
    y = root(y);
    if(x == y) return -1;
    if(rank[x] < rank[y]) {
      par[x] = y;
      return y;
    }
    else {
      par[y] = x;
      if(rank[x] == rank[y]) rank[x]++;
      return x;
    }
  }
};

vector<vector<i64>> G;

int main() {
  i64 N, M;
  cin >> N >> M;
  std::vector<std::pair<i64, i64>> vec;
  union_find uf(N);
  rep(i,0,M) {
    i64 a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if(c == 1) {
      if(uf.root(a) == uf.root(b)) {
        cout << "Yes" << endl;
        return 0;
      }
      uf.unite(a, b);
    }
    else {
      vec.push_back({ a, b });
    }
  }
  G.resize(N);
  for(auto e: vec) {
    i64 a = uf.root(e.first);
    i64 b = uf.root(e.second);
    if(a == b) {
      cout << "Yes" << endl;
      return 0;
    }
    G[a].push_back(b);
  }
  auto res = strongly_connected_components(G);
  auto ans = *std::max_element(all(res));
  if(ans + 1 == N) {
    cout << "No" << endl;
  }
  else {
    cout << "Yes" << endl;
  }
}
0