結果

問題 No.1023 Cyclic Tour
ユーザー ir5ir5
提出日時 2024-06-18 00:24:38
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 274 ms / 2,000 ms
コード長 3,876 bytes
コンパイル時間 2,439 ms
コンパイル使用メモリ 160,208 KB
実行使用メモリ 20,480 KB
最終ジャッジ日時 2024-06-18 00:24:52
合計ジャッジ時間 13,878 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 77 ms
6,940 KB
testcase_05 AC 77 ms
6,940 KB
testcase_06 AC 87 ms
6,940 KB
testcase_07 AC 82 ms
6,940 KB
testcase_08 AC 95 ms
9,984 KB
testcase_09 AC 125 ms
14,848 KB
testcase_10 AC 125 ms
14,848 KB
testcase_11 AC 138 ms
15,616 KB
testcase_12 AC 170 ms
16,512 KB
testcase_13 AC 130 ms
15,744 KB
testcase_14 AC 131 ms
15,616 KB
testcase_15 AC 136 ms
16,000 KB
testcase_16 AC 219 ms
19,412 KB
testcase_17 AC 230 ms
19,584 KB
testcase_18 AC 257 ms
18,816 KB
testcase_19 AC 217 ms
18,560 KB
testcase_20 AC 174 ms
16,128 KB
testcase_21 AC 179 ms
17,408 KB
testcase_22 AC 199 ms
18,048 KB
testcase_23 AC 207 ms
18,432 KB
testcase_24 AC 259 ms
20,480 KB
testcase_25 AC 174 ms
16,256 KB
testcase_26 AC 179 ms
16,512 KB
testcase_27 AC 171 ms
15,872 KB
testcase_28 AC 227 ms
19,840 KB
testcase_29 AC 274 ms
20,480 KB
testcase_30 AC 233 ms
20,096 KB
testcase_31 AC 210 ms
19,072 KB
testcase_32 AC 210 ms
18,944 KB
testcase_33 AC 228 ms
19,712 KB
testcase_34 AC 140 ms
6,944 KB
testcase_35 AC 148 ms
6,940 KB
testcase_36 AC 186 ms
17,280 KB
testcase_37 AC 196 ms
18,156 KB
testcase_38 AC 212 ms
19,456 KB
testcase_39 AC 210 ms
18,432 KB
testcase_40 AC 216 ms
18,560 KB
testcase_41 AC 224 ms
18,560 KB
testcase_42 AC 156 ms
10,752 KB
testcase_43 AC 157 ms
11,008 KB
testcase_44 AC 104 ms
12,416 KB
testcase_45 AC 146 ms
19,456 KB
testcase_46 AC 106 ms
16,000 KB
testcase_47 AC 102 ms
12,672 KB
testcase_48 AC 191 ms
17,012 KB
testcase_49 AC 177 ms
16,716 KB
testcase_50 AC 181 ms
16,944 KB
testcase_51 AC 151 ms
9,052 KB
testcase_52 AC 158 ms
10,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <functional>
#include <iomanip>
using namespace std;
using ll = long long;

class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x<lhs.x;}void operator++(){++x;}};I i,n;
public:range(int n_):i({0}),n({n_}){}range(int i_,int n_):i({i_}),n({n_}){}I& begin(){return i;}I& end(){return n;}};


template<class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p){ return os << "{" << p.first << ", " << p.second << "}"; }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& obj) { os << "{"; for (const auto& e : obj) os << e << ", "; return os << "}"; }
template<typename T> ostream& operator<<(ostream& os, const set<T>& obj) { os << "set{"; for (const auto& e : obj) os << e << ", "; return os << "}"; }
template<typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& obj) { os << "map{"; for (const auto& e : obj) os << e << ", "; return os << "}"; }

#ifdef ONLINE_JUDGE
#define dump(expr) ;
#else
#define dump(expr) { cerr << "\033[33m#L" << __LINE__ << ": " << expr << "\033[39m" << endl; }
#endif

struct E {
  int fr, to, c;
  E(int fr_, int to_, int c_) : fr(fr_), to(to_), c(c_) {}
  friend ostream &operator<<(ostream &os, const E &e) { os << "(" << e.fr << " -> " << e.to << ")"; return os; }
};
typedef vector<E> V;
// typedef vector<V> G;

// note that each vertex will have both enterning edges & entered edges
void add_edge(vector<V> &vg, int fr, int to) {
  vg[fr].push_back(E(fr, to, 0));
  vg[to].push_back(E(fr, to, 0));
}

struct StronglyConnectedComponents {
  vector<char> visited;
  vector<int> t_order;
  vector<int> comp;
  int n_comps;

  StronglyConnectedComponents() {}

  // normal topological sort
  void dfs1(vector<V> &vg, int id, int &k) {
    visited[id] = true;
    for (auto it : vg[id]) if (it.fr == id && !visited[it.to]) dfs1(vg, it.to, k);
    t_order[--k] = id;
  }
  // dfs of reverse order
  void dfs2(vector<V> &vg, int id, int &k) {
    visited[id] = true;
    for (auto it : vg[id]) if (it.to == id && !visited[it.fr]) dfs2(vg, it.fr, k);
    comp[id] = k; // foreach vertex "id", return the topological order
  }

  void exec(vector<V> &vg) {
    int n = (int)vg.size(), k = n;
    // 1st dfs
    visited.assign(n, 0);
    t_order.assign(n, 0);
    comp.assign(n, 0);
    for (int j : range(n)) if (!visited[j]) dfs1(vg, j, k);
    // 2nd dfs
    visited.assign(n, 0);
    for (int i : range(n)) {
      int j = t_order[i];
      if (!visited[j])
        dfs2(vg, j, k), k++;
    }
    n_comps = k;
  }
};

struct UF {
  vector<int> data;
  UF(int n) : data(n, -1) {}
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  bool find(int x, int y) { return root(x) == root(y); }
  void uni(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[x] < data[y])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
  }
};

ll solve() {
  int n, m; cin >> n >> m;
  vector<int> as(m), bs(m), cs(m);

  for (int i : range(m)) cin >> as[i] >> bs[i] >> cs[i];

  UF uf(n + 1);
  for (int i : range(m)) if (cs[i] == 1) {
    if (uf.find(as[i], bs[i])) return 1;
    uf.uni(as[i], bs[i]);
  }

  vector<V> vg(n + 1);
  for (int i : range(m)) if (cs[i] == 2) {
    int a = uf.root(as[i]);
    int b = uf.root(bs[i]);
    if (a == b) return 1;
    add_edge(vg, a, b);
  }

  StronglyConnectedComponents scc;
  scc.exec(vg);

  map<int, int> sizes;
  for (int i : range(n + 1)) sizes[scc.comp[i]]++;

  for (auto p : sizes) if (p.second > 1) return 1;
  return 0;
}

int main() {
  cout << fixed << setprecision(12);
  cout << (solve() ? "Yes" : "No") << endl;
}
0