結果

問題 No.1023 Cyclic Tour
ユーザー kyort0nkyort0n
提出日時 2020-04-10 22:12:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 871 ms / 2,000 ms
コード長 4,024 bytes
コンパイル時間 2,822 ms
コンパイル使用メモリ 199,716 KB
実行使用メモリ 47,724 KB
最終ジャッジ日時 2023-10-14 00:50:03
合計ジャッジ時間 26,876 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 70 ms
4,712 KB
testcase_05 AC 69 ms
4,692 KB
testcase_06 AC 74 ms
4,640 KB
testcase_07 AC 71 ms
4,712 KB
testcase_08 AC 327 ms
17,800 KB
testcase_09 AC 310 ms
17,500 KB
testcase_10 AC 344 ms
27,812 KB
testcase_11 AC 406 ms
31,960 KB
testcase_12 AC 482 ms
39,524 KB
testcase_13 AC 450 ms
37,444 KB
testcase_14 AC 354 ms
19,736 KB
testcase_15 AC 476 ms
38,064 KB
testcase_16 AC 545 ms
24,816 KB
testcase_17 AC 639 ms
25,996 KB
testcase_18 AC 548 ms
23,180 KB
testcase_19 AC 440 ms
22,424 KB
testcase_20 AC 381 ms
16,648 KB
testcase_21 AC 411 ms
17,700 KB
testcase_22 AC 583 ms
27,256 KB
testcase_23 AC 641 ms
29,720 KB
testcase_24 AC 871 ms
41,688 KB
testcase_25 AC 394 ms
18,324 KB
testcase_26 AC 388 ms
15,328 KB
testcase_27 AC 305 ms
12,620 KB
testcase_28 AC 530 ms
22,180 KB
testcase_29 AC 846 ms
41,996 KB
testcase_30 AC 794 ms
37,136 KB
testcase_31 AC 673 ms
33,152 KB
testcase_32 AC 739 ms
35,604 KB
testcase_33 AC 747 ms
34,468 KB
testcase_34 AC 55 ms
5,448 KB
testcase_35 AC 127 ms
6,652 KB
testcase_36 AC 466 ms
21,888 KB
testcase_37 AC 622 ms
30,472 KB
testcase_38 AC 783 ms
38,076 KB
testcase_39 AC 634 ms
31,132 KB
testcase_40 AC 655 ms
31,060 KB
testcase_41 AC 644 ms
30,076 KB
testcase_42 AC 264 ms
12,176 KB
testcase_43 AC 388 ms
20,140 KB
testcase_44 AC 106 ms
9,256 KB
testcase_45 AC 519 ms
47,724 KB
testcase_46 AC 456 ms
38,212 KB
testcase_47 AC 99 ms
9,336 KB
testcase_48 AC 245 ms
13,492 KB
testcase_49 AC 249 ms
13,628 KB
testcase_50 AC 271 ms
13,400 KB
testcase_51 AC 174 ms
11,032 KB
testcase_52 AC 250 ms
11,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
//const ll mod = 1000000007;
struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<ll> Size;
    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1);
        for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

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

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        Size[x] += Size[y];
        return true;
    }

    ll size(int x){
        return Size[root(x)];
    }
};

typedef vector<vector<int>> UnWeightedGraph;

template< typename G >
struct StronglyConnectedComponents {
  const G &g;
  UnWeightedGraph gg, rg;
  vector< int > comp, order, used;

  StronglyConnectedComponents(G &g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
    for(int i = 0; i < g.size(); i++) {
      for(auto e : g[i]) {
        gg[i].emplace_back((int) e);
        rg[(int) e].emplace_back(i);
      }
    }
  }

  int operator[](int k) {
    return (comp[k]);
  }

  void dfs(int idx) {
    if(used[idx]) return;
    used[idx] = true;
    for(int to : gg[idx]) dfs(to);
    order.push_back(idx);
  }

  void rdfs(int idx, int cnt) {
    if(comp[idx] != -1) return;
    comp[idx] = cnt;
    for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(UnWeightedGraph &t) {
    for(int i = 0; i < gg.size(); i++) dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;

    t.resize(ptr);
    for(int i = 0; i < g.size(); i++) {
      for(auto &to : g[i]) {
        int x = comp[i], y = comp[to];
        if(x == y) continue;
        t[x].push_back(y);
      }
    }
  }
};


vector<l_l> e;

int main() {
    ll N, M;
    cin >> N >> M;
    UnionFind uni(N);
    for(int i = 0; i < M; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        if(c == 1) {
            if(!uni.merge(a, b)) {
                cout << "Yes" << endl;
                return 0;
            }
        } else {
            e.push_back({a, b});
        }
    }
    ll V = 0;
    map<int, int> inv;
    for(int i = 0; i < N; i++) {
        if(uni.root(i) == i) {
            inv[i] = V;
            V++;
        }
    }
    for(int i = 0; i < N; i++) {
        inv[i] = inv[uni.root(i)];
    }
    UnWeightedGraph g(V), t;
    set<l_l> st;
    for(auto tmp : e) {
        if(inv[tmp.first] == inv[tmp.second]) {
            cout << "Yes" << endl;
            return 0;
        }
        //cerr << inv[tmp.first] << " " << inv[tmp.second] << endl;
        if(st.count({inv[tmp.second], inv[tmp.first]})) {
            cout << "Yes" << endl;
            return 0;
        }
        st.insert({inv[tmp.first], inv[tmp.second]});
        g[inv[tmp.first]].push_back(inv[tmp.second]);
    }
    StronglyConnectedComponents<UnWeightedGraph> SCC(g);
    SCC.build(t);
    set<int> st2;
    for(int i = 0; i < V; i++) {
        //cerr << i << " " << SCC[i] << endl;
        if(st2.count(SCC[i])) {
            cout << "Yes" << endl;
            return 0;
        }
        st2.insert(SCC[i]);
    }
    cout << "No" << endl;
    return 0;
}
0