結果

問題 No.1023 Cyclic Tour
ユーザー iiljjiiljj
提出日時 2020-04-11 18:02:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 315 ms / 2,000 ms
コード長 11,290 bytes
コンパイル時間 3,121 ms
コンパイル使用メモリ 237,208 KB
実行使用メモリ 52,552 KB
最終ジャッジ日時 2023-10-19 09:33:51
合計ジャッジ時間 14,914 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 67 ms
16,196 KB
testcase_05 AC 62 ms
16,196 KB
testcase_06 AC 80 ms
18,044 KB
testcase_07 AC 72 ms
16,988 KB
testcase_08 AC 99 ms
23,444 KB
testcase_09 AC 152 ms
31,248 KB
testcase_10 AC 151 ms
31,140 KB
testcase_11 AC 163 ms
32,856 KB
testcase_12 AC 175 ms
37,200 KB
testcase_13 AC 166 ms
35,260 KB
testcase_14 AC 165 ms
35,020 KB
testcase_15 AC 180 ms
36,184 KB
testcase_16 AC 312 ms
51,792 KB
testcase_17 AC 309 ms
52,552 KB
testcase_18 AC 315 ms
51,856 KB
testcase_19 AC 299 ms
50,588 KB
testcase_20 AC 168 ms
28,800 KB
testcase_21 AC 180 ms
31,032 KB
testcase_22 AC 229 ms
38,012 KB
testcase_23 AC 238 ms
40,088 KB
testcase_24 AC 297 ms
48,272 KB
testcase_25 AC 183 ms
32,700 KB
testcase_26 AC 156 ms
27,740 KB
testcase_27 AC 135 ms
26,060 KB
testcase_28 AC 285 ms
46,836 KB
testcase_29 AC 309 ms
49,208 KB
testcase_30 AC 295 ms
46,576 KB
testcase_31 AC 262 ms
42,904 KB
testcase_32 AC 287 ms
46,128 KB
testcase_33 AC 279 ms
45,452 KB
testcase_34 AC 88 ms
16,460 KB
testcase_35 AC 94 ms
18,572 KB
testcase_36 AC 205 ms
35,320 KB
testcase_37 AC 243 ms
41,412 KB
testcase_38 AC 274 ms
46,480 KB
testcase_39 AC 234 ms
39,596 KB
testcase_40 AC 240 ms
41,020 KB
testcase_41 AC 241 ms
40,852 KB
testcase_42 AC 130 ms
24,920 KB
testcase_43 AC 194 ms
33,416 KB
testcase_44 AC 108 ms
27,092 KB
testcase_45 AC 205 ms
42,536 KB
testcase_46 AC 195 ms
42,536 KB
testcase_47 AC 103 ms
31,560 KB
testcase_48 AC 113 ms
22,656 KB
testcase_49 AC 118 ms
22,924 KB
testcase_50 AC 111 ms
24,024 KB
testcase_51 AC 98 ms
20,280 KB
testcase_52 AC 99 ms
20,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* #region Head */

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll, ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;
using vll = vc<ll>;
using vvll = vvc<ll>;
using vld = vc<ld>;
using vvld = vvc<ld>;
using vs = vc<string>;
using vvs = vvc<string>;
template <class T, class U> using um = unordered_map<T, U>;
template <class T> using us = unordered_set<T>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqa = priority_queue<T, vc<T>, greater<T>>;

#define REP(i, m, n) for (ll i = (m), i##_len = (ll)(n); i < i##_len; ++(i))
#define REPM(i, m, n) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; ++(i))
#define REPR(i, m, n) for (ll i = (m), i##_min = (ll)(n); i >= i##_min; --(i))
#define REPD(i, m, n, d) for (ll i = (m), i##_len = (ll)(n); i < i##_len; i += (d))
#define REPMD(i, m, n, d) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; i += (d))
#define REPI(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define ALL(x) begin(x), end(x)
#define SIZE(x) ((ll)(x).size())
#define PERM(c)                                                                                                        \
    sort(ALL(c));                                                                                                      \
    for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define UNIQ(v) v.erase(unique(ALL(v)), v.end());

#define endl '\n'
#define sqrt sqrtl
#define floor floorl
#define log2 log2l

constexpr ll INF = 1'010'000'000'000'000'017LL;
constexpr ll MOD = 1'000'000'007LL; // 1e9 + 7
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.14159265358979323846;

template <typename T> istream &operator>>(istream &is, vc<T> &vec) { // vector 入力
    for (T &x : vec) is >> x;
    return is;
}
template <typename T> ostream &operator<<(ostream &os, vc<T> &vec) { // vector 出力 (for dump)
    os << "{";
    REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "" : ", ");
    os << "}";
    return os;
}
template <typename T> ostream &operator>>(ostream &os, vc<T> &vec) { // vector 出力 (inline)
    REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "\n" : " ");
    return os;
}

template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &pair_var) { // pair 入力
    is >> pair_var.first >> pair_var.second;
    return is;
}
template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { // pair 出力
    os << "(" << pair_var.first << ", " << pair_var.second << ")";
    return os;
}

// map, um, set, us 出力
template <class T> ostream &out_iter(ostream &os, T &map_var) {
    os << "{";
    REPI(itr, map_var) {
        os << *itr;
        auto itrcp = itr;
        if (++itrcp != map_var.end()) os << ", ";
    }
    os << "}";
    return os;
}
template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { return out_iter(os, map_var); }
template <typename T, typename U> ostream &operator<<(ostream &os, um<T, U> &map_var) { return out_iter(os, map_var); }
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { return out_iter(os, set_var); }
template <typename T> ostream &operator<<(ostream &os, us<T> &set_var) { return out_iter(os, set_var); }

// dump
#define DUMPOUT cerr
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail> void dump_func(Head &&head, Tail &&... tail) {
    DUMPOUT << head;
    if (sizeof...(Tail) > 0) DUMPOUT << ", ";
    dump_func(move(tail)...);
}

// chmax (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>> bool chmax(T &xmax, const U &x, Comp comp = {}) {
    if (comp(xmax, x)) {
        xmax = x;
        return true;
    }
    return false;
}

// chmin (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>> bool chmin(T &xmin, const U &x, Comp comp = {}) {
    if (comp(x, xmin)) {
        xmin = x;
        return true;
    }
    return false;
}

// ローカル用
#define DEBUG_

#ifdef DEBUG_
#define DEB
#define dump(...)                                                                                                      \
    DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                                                                    \
            << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl                                        \
            << "    ",                                                                                                 \
        dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

struct AtCoderInitialize {
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;
    AtCoderInitialize() {
        ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
        cout << fixed << setprecision(IOS_PREC);
        if (AUTOFLUSH) cout << unitbuf;
    }
} ATCODER_INITIALIZE;

string yes = "Yes", no = "No";
// string yes = "YES", no = "NO";
void yn(bool p) { cout << (p ? yes : no) << endl; }

/* #endregion */

/* #region Graph */

// グラフ用テンプレ
using Weight = ll;
using Flow = ll;

// エッジ(本来エッジは双方向だが,ここでは単方向で管理)
struct Edge {
    ll src;        // エッジ始点となる頂点
    ll dst;        // エッジ終点となる頂点
    Weight weight; // 重み
    Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(ll src, ll dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
using Node = vc<Edge>;  // 同じ頂点を始点とするエッジ集合
using Graph = vc<Node>; // graph[i] := 頂点 i を始点とするエッジ集合
using Array = vector<Weight>;
using Matrix = vector<Array>;

// 双方向のエッジを追加する
void add_edge(Graph &g, ll a, ll b, Weight w = 1) {
    g[a].emplace_back(a, b, w);
    g[b].emplace_back(b, a, w);
}
// 単方向のアークを追加する
void add_arc(Graph &g, ll a, ll b, Weight w = 1) { g[a].emplace_back(a, b, w); }

// Edge 標準出力
ostream &operator<<(ostream &os, Edge &edge) {
    os << "(" << edge.src << " -> " << edge.dst << ", " << edge.weight << ")";
    return os;
}

/* #endregion */

/* #region UnionFind */

struct UnionFind {
    ll _groupcount; // グループ数
    vc<ll> parent;  // 各要素の直接の親リスト,親がいないときは自分自身を指す
    vc<ll> gsize; // 各要素が根である場合,その要素が属するグループの要素数 (root 要素のみ有効な値を持つ)

    // コンストラクタ
    UnionFind() {}
    // コンストラクタ,要素数 n の UnionFind 木を構築する
    UnionFind(ll n) : _groupcount(n), gsize(n, 1), parent(n, 0) { iota(ALL(parent), 0LL); }

    // x の属する部分木の根要素を返す
    ll find(ll x) { return x == parent[x] ? x : parent[x] = find(parent[x]); }

    // x と y が同じグループかどうか判定する
    bool same(ll x, ll y) { return find(x) == find(y); }

    // x と y を同じグループにする
    void unite(ll x, ll y) {
        ll rx = find(x); // x の属するグループの根
        ll ry = find(y); // y の属するグループの根
        if (rx == ry)    // unite 済
            return;
        // assert(x != y)
        if (gsize[rx] < gsize[ry]) swap(rx, ry);
        // assert(gsize[x] > gsize[y]); // x 側の木の方が大きい

        gsize[rx] += gsize[ry]; // x を根とする1つのグループに統合
        parent[ry] = rx;        // ry 以下の木を rx 配下に接続する
        _groupcount--;
    }

    // x が属するグループの要素数を返す
    ll size(ll x) { return gsize[find(x)]; }

    // 全体のグループ数を返す
    ll count() const { return _groupcount; }
};

/* #endregion */

// 強連結成分分解
vll scc(const Graph &g) {
    ll n = SIZE(g); // ノード数
    Graph rg(n);    // 逆向きのグラフ
    for (const Node &es : g)
        for (Edge e : es) {
            swap(e.src, e.dst);
            rg[e.src].emplace_back(e);
        }

    vll order;
    order.reserve(n);

    { // dfs 1回目
        vc<bool> visited(n), added(n);
        REP(i, 0, n) {
            if (visited[i]) continue;
            stack<ll> stk;
            stk.push(i);
            while (!stk.empty()) {
                ll cur = stk.top();
                visited[cur] = true;
                bool pushed = false;
                for (const Edge &e : g[cur])
                    if (!visited[e.dst]) {
                        stk.push(e.dst);
                        pushed = true;
                    }
                if (!pushed) { // カレントノードからは未訪問ノードへ到達できない
                    ll t = stk.top();
                    stk.pop(); // 未訪問ノードへ到達できないノードは除いていく
                    if (!added[t]) {
                        added[t] = true;
                        order.push_back(t);
                    }
                }
            }
        }
        reverse(ALL(order));
    }

    vll ret(n, -1);
    { // dfs 2回目
        ll groupnum = 0;
        for (ll &v : order) {
            if (ret[v] != -1) continue;
            stack<ll> stk;
            stk.push(v);
            while (!stk.empty()) {
                ll cur = stk.top();
                stk.pop();
                ret[cur] = groupnum;
                for (Edge &e : rg[cur])
                    if (ret[e.dst] == -1) stk.push(e.dst);
            }
            ++groupnum;
        }
    }
    return ret;
}

// Problem
void solve() {
    ll n, m;
    cin >> n >> m;
    vll a(m), b(m), c(m); // c[i] == 1 で無向辺, c[i] == 2 で有向辺
    REP(i, 0, m) {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--, b[i]--;
    }

    { // 無向辺のみの閉路を検出する
        Graph graph(n, Node(0));
        REP(i, 0, m) if (c[i] == 1) add_edge(graph, a[i], b[i]);

        vc<bool> visited(n, false);
        function<bool(ll, ll)> dfs = [&](ll idx, ll par) {
            if (visited[idx]) return true;
            visited[idx] = true;
            for (Edge &edge : graph[idx])
                if (edge.dst != par && dfs(edge.dst, idx)) return true;
            return false;
        };
        bool ok = false;
        REP(i, 0, n) if (!visited[i]) if (ok = dfs(i, -1)) break;
        if (ok) {
            yn(true);
            return;
        }
    }

    { // 無向辺を縮約した有向グラフの閉路を検出する
        UnionFind uf(n);
        REP(i, 0, m) if (c[i] == 1) uf.unite(a[i], b[i]);
        vc<us<ll>> dsts(n);
        REP(i, 0, m) if (c[i] == 2) dsts[uf.find(a[i])].insert(uf.find(b[i]));

        Graph graph(n, Node(0));
        REP(i, 0, n) REPI(itr, dsts[i]) {
            if (i == *itr) {
                yn(true);
                return;
            }
            add_arc(graph, i, *itr);
        }

        vll ret = scc(graph);
        // dump(graph, ret);
        sort(ALL(ret));
        UNIQ(ret);
        yn(SIZE(ret) != n);
    }
}

// entry point
int main() {
    solve();
    return 0;
}
0