結果

問題 No.1023 Cyclic Tour
ユーザー beetbeet
提出日時 2020-04-10 21:40:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 2,474 bytes
コンパイル時間 3,537 ms
コンパイル使用メモリ 215,924 KB
実行使用メモリ 31,300 KB
最終ジャッジ日時 2023-10-13 23:45:03
合計ジャッジ時間 14,440 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 26 ms
5,328 KB
testcase_05 AC 25 ms
5,524 KB
testcase_06 AC 29 ms
5,752 KB
testcase_07 AC 27 ms
5,364 KB
testcase_08 AC 41 ms
13,400 KB
testcase_09 AC 79 ms
21,628 KB
testcase_10 AC 77 ms
21,908 KB
testcase_11 AC 85 ms
22,740 KB
testcase_12 AC 96 ms
24,856 KB
testcase_13 AC 80 ms
23,484 KB
testcase_14 AC 84 ms
23,504 KB
testcase_15 AC 83 ms
23,628 KB
testcase_16 AC 119 ms
22,560 KB
testcase_17 AC 126 ms
22,368 KB
testcase_18 AC 118 ms
20,188 KB
testcase_19 AC 114 ms
20,548 KB
testcase_20 AC 77 ms
21,008 KB
testcase_21 AC 78 ms
21,472 KB
testcase_22 AC 97 ms
22,744 KB
testcase_23 AC 104 ms
22,984 KB
testcase_24 AC 128 ms
25,992 KB
testcase_25 AC 74 ms
20,988 KB
testcase_26 AC 76 ms
21,092 KB
testcase_27 AC 73 ms
20,692 KB
testcase_28 AC 128 ms
25,140 KB
testcase_29 AC 127 ms
25,800 KB
testcase_30 AC 123 ms
25,716 KB
testcase_31 AC 108 ms
24,788 KB
testcase_32 AC 111 ms
22,656 KB
testcase_33 AC 116 ms
24,744 KB
testcase_34 AC 42 ms
6,308 KB
testcase_35 AC 47 ms
6,544 KB
testcase_36 AC 85 ms
21,444 KB
testcase_37 AC 104 ms
23,464 KB
testcase_38 AC 127 ms
24,448 KB
testcase_39 AC 103 ms
23,144 KB
testcase_40 AC 107 ms
23,300 KB
testcase_41 AC 109 ms
23,384 KB
testcase_42 AC 55 ms
12,624 KB
testcase_43 AC 58 ms
14,220 KB
testcase_44 AC 43 ms
18,280 KB
testcase_45 AC 100 ms
31,300 KB
testcase_46 AC 69 ms
20,504 KB
testcase_47 AC 44 ms
19,092 KB
testcase_48 AC 75 ms
22,152 KB
testcase_49 AC 74 ms
21,592 KB
testcase_50 AC 73 ms
21,608 KB
testcase_51 AC 52 ms
12,328 KB
testcase_52 AC 54 ms
12,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using Int = long long;
const char newl = '\n';


struct UnionFind{
  int num;
  vector<int> rs,ps;
  UnionFind(){}
  UnionFind(int n):num(n),rs(n,1),ps(n,0){iota(ps.begin(),ps.end(),0);}
  int find(int x){
    return (x==ps[x]?x:ps[x]=find(ps[x]));
  }
  bool same(int x,int y){
    return find(x)==find(y);
  }
  void unite(int x,int y){
    x=find(x);y=find(y);
    if(x==y) return;
    if(rs[x]<rs[y]) swap(x,y);
    rs[x]+=rs[y];
    ps[y]=x;
    num--;
  }
  int size(int x){
    return rs[find(x)];
  }
  int count() const{
    return num;
  }
};


template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}




struct SCC{
  vector< vector<int> > G,R,T,C;
  vector<int> vs,used,blg;
  SCC(){}
  SCC(int n):G(n),R(n),used(n),blg(n){}

  void add_edge(int u,int v){
    G[u].emplace_back(v);
    R[v].emplace_back(u);
  }

  void dfs(int v){
    used[v]=1;
    for(int u:G[v])
      if(!used[u]) dfs(u);
    vs.emplace_back(v);
  }

  void rdfs(int v,int k){
    used[v]=1;
    blg[v]=k;
    C[k].emplace_back(v);
    for(int u:R[v])
      if(!used[u]) rdfs(u,k);
  }

  int build(){
    int n=G.size();
    for(int v=0;v<n;v++)
      if(!used[v]) dfs(v);

    fill(used.begin(),used.end(),0);
    int k=0;
    for(int i=n-1;i>=0;i--){
      if(!used[vs[i]]){
        T.emplace_back();
        C.emplace_back();
        rdfs(vs[i],k++);
      }
    }

    for(int v=0;v<n;v++)
      for(int u:G[v])
        if(blg[v]!=blg[u])
          T[blg[v]].push_back(blg[u]);

    for(int i=0;i<k;i++){
      sort(T[i].begin(),T[i].end());
      T[i].erase(unique(T[i].begin(),T[i].end()),T[i].end());
    }
    return k;
  }

  int operator[](int k) const{return blg[k];}
};

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,m;
  cin>>n>>m;

  vector<int> as(m),bs(m),cs(m);
  for(int i=0;i<m;i++) cin>>as[i]>>bs[i]>>cs[i],as[i]--,bs[i]--;

  UnionFind uf(n);
  for(int i=0;i<m;i++){
    if(cs[i]==2) continue;
    if(uf.same(as[i],bs[i])) drop("Yes");
    uf.unite(as[i],bs[i]);
  }

  SCC G(n);
  for(int i=0;i<m;i++){
    if(cs[i]==1) continue;
    if(uf.same(as[i],bs[i])) drop("Yes");
    int x=uf.find(as[i]);
    int y=uf.find(bs[i]);
    G.add_edge(x,y);
  }

  int k=G.build();
  cout<<(k<n?"Yes":"No")<<newl;
  return 0;
}
0