結果

問題 No.1023 Cyclic Tour
ユーザー petite_progpetite_prog
提出日時 2020-04-12 09:14:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 5,651 bytes
コンパイル時間 2,162 ms
コンパイル使用メモリ 190,804 KB
実行使用メモリ 28,272 KB
最終ジャッジ日時 2024-09-22 02:16:04
合計ジャッジ時間 10,262 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 28 ms
5,376 KB
testcase_05 AC 26 ms
5,376 KB
testcase_06 AC 27 ms
5,376 KB
testcase_07 AC 27 ms
5,376 KB
testcase_08 AC 34 ms
7,900 KB
testcase_09 AC 58 ms
16,076 KB
testcase_10 AC 60 ms
16,176 KB
testcase_11 AC 58 ms
16,732 KB
testcase_12 AC 63 ms
18,792 KB
testcase_13 AC 60 ms
17,872 KB
testcase_14 AC 61 ms
17,612 KB
testcase_15 AC 66 ms
18,008 KB
testcase_16 AC 109 ms
22,640 KB
testcase_17 AC 112 ms
22,640 KB
testcase_18 AC 107 ms
21,964 KB
testcase_19 AC 125 ms
21,708 KB
testcase_20 AC 66 ms
13,968 KB
testcase_21 AC 61 ms
14,316 KB
testcase_22 AC 77 ms
15,848 KB
testcase_23 AC 93 ms
16,360 KB
testcase_24 AC 98 ms
19,184 KB
testcase_25 AC 61 ms
14,180 KB
testcase_26 AC 58 ms
14,040 KB
testcase_27 AC 55 ms
13,388 KB
testcase_28 AC 96 ms
18,528 KB
testcase_29 AC 103 ms
19,552 KB
testcase_30 AC 96 ms
18,672 KB
testcase_31 AC 87 ms
18,160 KB
testcase_32 AC 93 ms
20,672 KB
testcase_33 AC 103 ms
18,928 KB
testcase_34 AC 19 ms
5,376 KB
testcase_35 AC 43 ms
5,376 KB
testcase_36 AC 69 ms
14,560 KB
testcase_37 AC 82 ms
18,404 KB
testcase_38 AC 95 ms
19,540 KB
testcase_39 AC 83 ms
16,476 KB
testcase_40 AC 87 ms
16,860 KB
testcase_41 AC 85 ms
16,732 KB
testcase_42 AC 54 ms
7,012 KB
testcase_43 AC 52 ms
8,540 KB
testcase_44 AC 32 ms
11,392 KB
testcase_45 AC 85 ms
28,272 KB
testcase_46 AC 93 ms
28,272 KB
testcase_47 AC 32 ms
11,776 KB
testcase_48 AC 59 ms
13,932 KB
testcase_49 AC 59 ms
14,064 KB
testcase_50 AC 58 ms
13,808 KB
testcase_51 AC 54 ms
6,892 KB
testcase_52 AC 51 ms
7,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
   ∫ ∫ ∫
   ノヽ
  (_  )
 (_    )
(______ )
 ヽ(´・ω・)ノ 
   |  /
   UU
*/
#pragma region macro
#include <bits/stdc++.h>
typedef long long int64;
using namespace std;
using P = pair<int64, int64>;
typedef vector<int> vi;
const int MOD = (int)1e9 + 7;
const int64 INF = 1LL << 62;
const int inf = 1<<30;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i,s,n) for (int i = s; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end() //コンテナじゃないと使えない!!
#define debug(x) cerr << #x << ": " << x << "\n";
#define mp make_pair
#define bn '\n'
template <typename T>
ostream& operator<<(ostream& os, const vector<T> &V){
    int N = V.size();
    REP(i,N){
        os << V[i];
        if (i!=N-1) os << " ";
    }
    os << "\n";
    return os;
}
template <typename T,typename S>
ostream& operator<<(ostream& os, pair<T,S> const&P){
    os << "(";
    os << P.first;
    os << " , ";
    os << P.second;
    os << ")";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T> &S){
    auto it=S.begin();
    while(it!=S.end()){
        os << *it;
        os << " ";
        it++;
    }
    os << "\n";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, deque<T> &q){
    for(auto it=q.begin();it<q.end();it++){
        os<<*it;
        os<<" ";
    }
    os<<endl;
    return os;
}
template <typename T,typename S>
ostream& operator<<(ostream& os, map<T,S> const&M){
    for(auto e:M){
        os<<e;
    }
    os<<endl;
    return os;
}
vector<pair<int,int>> dxdy = {mp(0,1),mp(1,0),mp(-1,0),mp(0,-1)};
#pragma endregion
//fixed<<setprecision(10)<<ans<<endl;

struct UnionFind{
    int N;
    vector<int> node;
    UnionFind(){}
    UnionFind(int N):N(N){
        node.assign(N,-1);
    }
    int get_root(int x){
        if (node[x]<0){
            return x;
        }
        node[x] = get_root(node[x]);
        return node[x];
    }
    void unite(int x,int y){
        int root_x = get_root(x);
        int root_y = get_root(y);
        int larger,smaller;
        if(root_x != root_y){
            if(node[root_x] < node[root_y]){ //size of x is lager than one of y
                larger  = root_x;
                smaller = root_y;
            }else{
                larger  = root_y;
                smaller = root_x;
            }
            node[larger] += node[smaller];
            node[smaller] = larger;
        }
    }
    int size(int x){
        return -node[x];
    }
    bool same(int x,int y){
        return get_root(x) == get_root(y);
    }
};

struct StronglyConnectedComponents{
    vector<int> topological_index; //属する強連結成分のトポロジカル順序
    vector<bool> visited;
    vector<vector<int>> edge, edge_rev;
    vector<int> post_order;
    int N;
    int scc_size = 0; //強連結成分の数

    //O(N+M)
    StronglyConnectedComponents(vector<vector<int>>& edge):edge(edge){
        N = edge.size();
        edge_rev.resize(N);
        for(int v=0;v<N;v++){
            for(auto to:edge[v]){
                edge_rev[to].emplace_back(v);
            }
        }
        visited.assign(N,false);
        topological_index.resize(N);
        for(int i=0;i<N;i++){
            if(not visited[i]) dfs(i);
        }
        visited.assign(N,false);
        reverse(post_order.begin(), post_order.end());
        for(auto v:post_order){
            if(not visited[v]) dfs_rev(v,scc_size++);
        }
    }

    void dfs(int v){
        visited[v] = true;
        for(auto to:edge[v]){
            if(not visited[to]) dfs(to);
        }
        post_order.emplace_back(v);
    }

    void dfs_rev(int v, int idx){
        visited[v] = true;
        topological_index[v] = idx;
        for(auto to:edge_rev[v]){
            if(not visited[to]) dfs_rev(to, idx);
        }
    }

    //vが属している強連結成分のトポロジカル順序
    int get_topological_index(int v){
        return topological_index[v];
    }

    //強連結成分の数
    int get_scc_size(){
        return scc_size;
    }

    vector<vector<int>> build_graph(){
        vector<vector<int>> new_edge(N);
        for(int i=0;i<N;i++){
            int topo = topological_index[i];
            for(auto to:edge[i]){
                new_edge[topo].emplace_back(topological_index[to]);
            }
        }
        for(int i=0;i<scc_size;i++){
            sort(new_edge[i].begin(), new_edge[i].end());
            new_edge[i].erase(unique(new_edge[i].begin(), new_edge[i].end()), new_edge[i].end());
        }
        return new_edge;
    }

};


int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N,M;
    cin >> N >> M;
    vector<pair<int,int>> directed;
    UnionFind UF(N);
    int a,b,c;
    REP(i,M){
        cin >> a >> b >> c;
        if(c==1){
            if(UF.same(--a,--b)){
                cout << "Yes" << bn;
                return 0;
            }
            UF.unite(a,b);
        }else{
            directed.emplace_back(--a,--b);
        }
    }
    vector<vector<int>> edge(N);
    for(auto e:directed){
        tie(a,b) = e;
        if(UF.same(a,b)){
            cout << "Yes" << bn;
            return 0;
        }
        edge[UF.get_root(a)].emplace_back(UF.get_root(b));
    }
    StronglyConnectedComponents SCC(edge);
    if(SCC.get_scc_size() != N){
        cout << "Yes" << bn;
    }else{
        cout << "No" << bn;
    }

}
0