結果

問題 No.1023 Cyclic Tour
ユーザー 👑 emthrmemthrm
提出日時 2020-04-10 22:58:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 315 ms / 2,000 ms
コード長 3,199 bytes
コンパイル時間 2,479 ms
コンパイル使用メモリ 213,124 KB
最終ジャッジ日時 2025-01-09 16:38:50
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

using CostType = char;
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &x) const {
    return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src;
  }
  inline bool operator<=(const Edge &x) const { return !(x < *this); }
  inline bool operator>(const Edge &x) const { return x < *this; }
  inline bool operator>=(const Edge &x) const { return !(*this < x); }
};

vector<int> topological_sort(const vector<vector<Edge>> &graph) {
  int n = graph.size();
  vector<int> deg(n, 0);
  REP(i, n) {
    for (const Edge &e : graph[i]) ++deg[e.dst];
  }
  queue<int> que;
  REP(i, n) {
    if (deg[i] == 0) que.emplace(i);
  }
  vector<int> res;
  while (!que.empty()) {
    int ver = que.front(); que.pop();
    res.emplace_back(ver);
    for (const Edge &e : graph[ver]) {
      if (--deg[e.dst] == 0) que.emplace(e.dst);
    }
  }
  return res.size() == n ? res : vector<int>();
}

int main() {
  int n, m; cin >> n >> m;
  set<pair<int, int>> und, dir;
  while (m--) {
    int a, b, c; cin >> a >> b >> c; --a; --b;
    (c == 1 ? und : dir).emplace(a, b);
  }
  vector<int> p, q;
  for (auto [a, b] : dir) {
    if (und.count(make_pair(a, b)) == 1 || und.count(make_pair(b, a)) == 1 || dir.count(make_pair(b, a)) == 1) {
      cout << "Yes\n";
      return 0;
    }
    p.emplace_back(a);
    q.emplace_back(b);
  }
  vector<vector<int>> und_graph(n);
  for (auto [a, b] : und) {
    und_graph[a].emplace_back(b);
    und_graph[b].emplace_back(a);
  }
  vector<int> id(n, -1);
  int sz = 0;
  REP(i, n) {
    if (id[i] != -1) continue;
    function<void(int, int)> dfs = [&](int par, int ver) {
      for (int e : und_graph[ver]) {
        if (e == par) continue;
        if (id[e] == -1) {
          id[e] = id[i];
          dfs(ver, e);
        } else {
          cout << "Yes\n";
          exit(0);
        }
      }
    };
    id[i] = sz++;
    dfs(-1, i);
  }
  // REP(i, n) cout << id[i] << " \n"[i + 1 == n];
  vector<vector<Edge>> graph(sz);
  REP(i, p.size()) {
    int a = id[p[i]], b = id[q[i]];
    if (a == b) {
      cout << "Yes\n";
      return 0;
    }
    // cout << a << ' ' << b << '\n';
    graph[a].emplace_back(a, b);
  }
  cout << (topological_sort(graph).empty() ? "Yes\n" : "No\n");
  return 0;
}
0