結果

問題 No.1023 Cyclic Tour
ユーザー kyort0nkyort0n
提出日時 2020-04-10 22:05:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,024 bytes
コンパイル時間 2,431 ms
コンパイル使用メモリ 201,080 KB
実行使用メモリ 41,180 KB
最終ジャッジ日時 2023-10-14 00:40:55
合計ジャッジ時間 26,838 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 66 ms
4,656 KB
testcase_05 AC 66 ms
4,604 KB
testcase_06 AC 69 ms
4,648 KB
testcase_07 AC 66 ms
4,648 KB
testcase_08 AC 292 ms
21,456 KB
testcase_09 AC 300 ms
21,100 KB
testcase_10 WA -
testcase_11 AC 395 ms
29,088 KB
testcase_12 AC 449 ms
35,484 KB
testcase_13 AC 421 ms
33,612 KB
testcase_14 AC 375 ms
24,716 KB
testcase_15 AC 486 ms
34,040 KB
testcase_16 AC 565 ms
29,852 KB
testcase_17 AC 617 ms
30,888 KB
testcase_18 AC 558 ms
28,156 KB
testcase_19 AC 450 ms
26,172 KB
testcase_20 AC 407 ms
15,544 KB
testcase_21 AC 420 ms
16,756 KB
testcase_22 AC 527 ms
24,996 KB
testcase_23 AC 633 ms
27,408 KB
testcase_24 AC 867 ms
38,504 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 551 ms
26,020 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 53 ms
5,456 KB
testcase_35 AC 118 ms
6,672 KB
testcase_36 AC 421 ms
20,572 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 598 ms
27,804 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 282 ms
12,140 KB
testcase_43 AC 406 ms
23,984 KB
testcase_44 WA -
testcase_45 AC 457 ms
38,516 KB
testcase_46 WA -
testcase_47 AC 100 ms
9,412 KB
testcase_48 AC 249 ms
12,616 KB
testcase_49 AC 256 ms
12,616 KB
testcase_50 AC 274 ms
12,652 KB
testcase_51 AC 173 ms
11,132 KB
testcase_52 AC 253 ms
11,288 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;
    StronglyConnectedComponents<UnWeightedGraph> SCC(g);
    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]);
    }
    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