結果

問題 No.1023 Cyclic Tour
ユーザー mugen_1337mugen_1337
提出日時 2020-05-26 22:45:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 393 ms / 2,000 ms
コード長 3,336 bytes
コンパイル時間 3,421 ms
コンパイル使用メモリ 219,008 KB
実行使用メモリ 40,920 KB
最終ジャッジ日時 2024-04-21 04:40:58
合計ジャッジ時間 16,005 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_04 AC 32 ms
5,376 KB
testcase_05 AC 31 ms
5,376 KB
testcase_06 AC 31 ms
5,376 KB
testcase_07 AC 30 ms
5,376 KB
testcase_08 AC 137 ms
12,616 KB
testcase_09 AC 181 ms
27,824 KB
testcase_10 AC 178 ms
27,832 KB
testcase_11 AC 194 ms
29,144 KB
testcase_12 AC 227 ms
32,360 KB
testcase_13 AC 211 ms
30,644 KB
testcase_14 AC 214 ms
30,384 KB
testcase_15 AC 217 ms
31,032 KB
testcase_16 AC 391 ms
34,016 KB
testcase_17 AC 393 ms
33,908 KB
testcase_18 AC 378 ms
31,536 KB
testcase_19 AC 359 ms
31,544 KB
testcase_20 AC 209 ms
24,204 KB
testcase_21 AC 198 ms
24,940 KB
testcase_22 AC 268 ms
27,240 KB
testcase_23 AC 277 ms
27,924 KB
testcase_24 AC 374 ms
32,760 KB
testcase_25 AC 199 ms
24,160 KB
testcase_26 AC 197 ms
24,264 KB
testcase_27 AC 195 ms
23,088 KB
testcase_28 AC 355 ms
31,576 KB
testcase_29 AC 386 ms
33,368 KB
testcase_30 AC 353 ms
31,848 KB
testcase_31 AC 306 ms
30,584 KB
testcase_32 AC 328 ms
31,296 KB
testcase_33 AC 353 ms
31,228 KB
testcase_34 AC 23 ms
5,376 KB
testcase_35 AC 49 ms
5,376 KB
testcase_36 AC 220 ms
25,440 KB
testcase_37 AC 277 ms
29,252 KB
testcase_38 AC 343 ms
31,620 KB
testcase_39 AC 284 ms
28,240 KB
testcase_40 AC 324 ms
28,620 KB
testcase_41 AC 351 ms
28,628 KB
testcase_42 AC 131 ms
12,000 KB
testcase_43 AC 223 ms
13,372 KB
testcase_44 AC 64 ms
21,260 KB
testcase_45 AC 247 ms
40,920 KB
testcase_46 AC 244 ms
35,832 KB
testcase_47 AC 64 ms
21,752 KB
testcase_48 AC 165 ms
25,080 KB
testcase_49 AC 163 ms
25,080 KB
testcase_50 AC 157 ms
25,332 KB
testcase_51 AC 87 ms
11,896 KB
testcase_52 AC 123 ms
12,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct UnionFind{
    vector<int> par;
    vector<int> siz;
    int c;//親の個数(グループ数)
    void init(int n){
        c=n;
        for(int i=0;i<n;i++){
            par.push_back(i);
            siz.push_back(1);
        }
    }
    UnionFind(int n){
        init(n);
    }
    int root(int x){
        //根を返す
        return(par[x]==x?x:(par[x]=root(par[x])));
    }
    bool sameParent(int x,int y){
        return(root(x)==root(y));
    }
    void unite(int x, int y){
        x=root(x);y=root(y);
        if(x==y)    return;
        if(siz[x]<siz[y]) swap(x,y);
        siz[x] += siz[y];par[y]=x;c--;
    }
    int size(int x){
        return siz[root(x)];
    }
};

struct StronglyConnectedComponents{
    vector<vector<int>> g;
    vector<vector<int>> to,from;
    vector<int> comp,order,used;
    StronglyConnectedComponents(vector<vector<int>> &g):
    g(g),to(g.size()),from(g.size()),comp(g.size(),-1),used(g.size()){
        for(int i=0;i<g.size();i++){
            for(auto x:g[i]){
                to[i].push_back(x);
                from[x].push_back(i);
            }
        }
    }
    int operator[](int k){
        return comp[k];
    }
    void dfs(int now){
        if(used[now]) return ;
        used[now]=true;
        for(int x:to[now])dfs(x);
        order.push_back(now);
    }
    void rdfs(int now,int cnt){
        if(comp[now]!=-1) return ;
        comp[now]=cnt;
        for(int x:from[now]) rdfs(x,cnt);
    }
    vector<vector<int>> build(){
        vector<vector<int>> t;
        for(int i=0;i<to.size();i++) dfs(i);
        reverse(order.begin(),order.end());
        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 v:to[i]){
                int x=comp[i],y=comp[v];
                if(x==y) continue;
                t[x].push_back(y);
            }
        }
        return t;
    }
};

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int n,m;cin>>n>>m;
    UnionFind uf(n);
    vector<pair<int,int>> es;
    rep(i,m){
        int a,b,c;cin>>a>>b>>c;a--,b--;
        if(c==1){
            if(uf.sameParent(a,b)){
                cout<<"Yes"<<endl;
                return 0;
            }
            uf.unite(a,b);
        }
        else{
            es.push_back({a,b});
        }
    }

    map<int,int> id;
    rep(i,n){
        id[i]=uf.root(i);
    }

    vector<vector<int>> g(id.size());
    for(auto &p:es){
        if(id[p.first]==id[p.second]){
            cout<<"Yes"<<endl;
            return 0;
        }
        g[id[p.first]].push_back(id[p.second]);
    }

    StronglyConnectedComponents SCC(g);
    auto t=SCC.build();
    cout<<(t.size()==id.size()?"No":"Yes")<<endl;
    return 0;
}
0