結果

問題 No.1023 Cyclic Tour
ユーザー MayimgMayimg
提出日時 2020-07-07 10:01:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 124 ms / 2,000 ms
コード長 1,828 bytes
コンパイル時間 2,306 ms
コンパイル使用メモリ 216,124 KB
実行使用メモリ 18,848 KB
最終ジャッジ日時 2023-10-26 00:17:16
合計ジャッジ時間 12,306 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 26 ms
4,348 KB
testcase_05 AC 25 ms
4,348 KB
testcase_06 AC 28 ms
4,348 KB
testcase_07 AC 26 ms
4,348 KB
testcase_08 AC 45 ms
12,504 KB
testcase_09 AC 44 ms
12,228 KB
testcase_10 AC 41 ms
12,232 KB
testcase_11 AC 51 ms
12,776 KB
testcase_12 AC 62 ms
13,840 KB
testcase_13 AC 52 ms
13,288 KB
testcase_14 AC 47 ms
13,284 KB
testcase_15 AC 57 ms
13,556 KB
testcase_16 AC 102 ms
18,584 KB
testcase_17 AC 106 ms
18,848 KB
testcase_18 AC 112 ms
18,812 KB
testcase_19 AC 106 ms
18,288 KB
testcase_20 AC 78 ms
12,248 KB
testcase_21 AC 87 ms
13,052 KB
testcase_22 AC 105 ms
15,160 KB
testcase_23 AC 111 ms
15,668 KB
testcase_24 AC 124 ms
18,056 KB
testcase_25 AC 82 ms
13,308 KB
testcase_26 AC 68 ms
10,656 KB
testcase_27 AC 58 ms
9,324 KB
testcase_28 AC 119 ms
17,776 KB
testcase_29 AC 112 ms
18,304 KB
testcase_30 AC 114 ms
17,792 KB
testcase_31 AC 94 ms
16,472 KB
testcase_32 AC 103 ms
18,028 KB
testcase_33 AC 106 ms
17,528 KB
testcase_34 AC 20 ms
4,348 KB
testcase_35 AC 46 ms
4,672 KB
testcase_36 AC 94 ms
14,364 KB
testcase_37 AC 102 ms
16,460 KB
testcase_38 AC 110 ms
17,760 KB
testcase_39 AC 114 ms
15,660 KB
testcase_40 AC 104 ms
15,924 KB
testcase_41 AC 103 ms
15,924 KB
testcase_42 AC 66 ms
10,140 KB
testcase_43 AC 108 ms
17,508 KB
testcase_44 AC 29 ms
8,708 KB
testcase_45 AC 59 ms
17,280 KB
testcase_46 AC 53 ms
17,280 KB
testcase_47 AC 29 ms
8,708 KB
testcase_48 AC 57 ms
9,624 KB
testcase_49 AC 58 ms
9,624 KB
testcase_50 AC 54 ms
9,624 KB
testcase_51 AC 56 ms
9,624 KB
testcase_52 AC 55 ms
9,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
/////////////////////////////////////////////////////////////////////////
// UNION FIND
/////////////////////////////////////////////////////////////////////////
class UnionFind {
private:
  int siz;
  vector<int> a;

public:
  UnionFind(int x) : siz(x), a(x, -1) {}

  int root(int x) {
    return a[x] < 0 ? x : a[x] = root(a[x]);
  }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    siz--;
    if (a[x] > a[y]) swap(x, y);
    a[x] += a[y];
    a[y] =x;
    return true;
  }

  bool same(int x, int y) {
    return root(x) == root(y);
  }

  int size(int x) {
    return -a[root(x)];
  }
  
  int connected_component() {
  	return siz;
  }
};
/////////////////////////////////////////////////////////////////////////
// END UNION FIND
/////////////////////////////////////////////////////////////////////////

vector<set<int>> g;
vector<int> vis;

void dfs (int cur) {
  if (vis[cur] == 2) return;
  if (vis[cur] == 1) {
    cout << "Yes" << endl;
    exit(0);
  }
  vis[cur] = 1;
  for (int nxt : g[cur]) {
    dfs(nxt);
  }
  vis[cur] = 2;
}

signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> de;
  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    int u, v, t;
    cin >> u >> v >> t;
    u--;
    v--;
    if (t == 1) {
      if (!uf.unite(u, v)) {
        cout << "Yes" << endl;
        return 0;
      }
    } else {
      de.emplace_back(u, v);
    }
  }
  g = vector<set<int>>(n);
  vis = vector<int>(n);
  for (auto& p : de) {
    p.first = uf.root(p.first);
    p.second = uf.root(p.second);
    g[p.first].insert(p.second);
  }
  for (int i = 0; i < n; i++) {
    if (!vis[i]) dfs(i);
  }
  cout << "No" << endl;
  return 0;
}
0