結果

問題 No.1023 Cyclic Tour
ユーザー SSRSSSRS
提出日時 2020-04-10 22:49:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 249 ms / 2,000 ms
コード長 1,838 bytes
コンパイル時間 2,499 ms
コンパイル使用メモリ 180,956 KB
実行使用メモリ 25,292 KB
最終ジャッジ日時 2023-10-14 03:36:11
合計ジャッジ時間 14,408 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 104 ms
10,980 KB
testcase_05 AC 103 ms
11,068 KB
testcase_06 AC 115 ms
11,556 KB
testcase_07 AC 106 ms
11,256 KB
testcase_08 AC 126 ms
18,364 KB
testcase_09 AC 125 ms
17,828 KB
testcase_10 AC 119 ms
18,056 KB
testcase_11 AC 136 ms
18,760 KB
testcase_12 AC 119 ms
20,640 KB
testcase_13 AC 113 ms
19,616 KB
testcase_14 AC 113 ms
19,376 KB
testcase_15 AC 121 ms
19,888 KB
testcase_16 AC 204 ms
25,036 KB
testcase_17 AC 208 ms
25,292 KB
testcase_18 AC 209 ms
24,632 KB
testcase_19 AC 200 ms
24,308 KB
testcase_20 AC 223 ms
15,452 KB
testcase_21 AC 236 ms
16,448 KB
testcase_22 AC 235 ms
20,072 KB
testcase_23 AC 245 ms
21,008 KB
testcase_24 AC 249 ms
24,304 KB
testcase_25 AC 238 ms
16,856 KB
testcase_26 AC 212 ms
13,348 KB
testcase_27 AC 189 ms
11,576 KB
testcase_28 AC 240 ms
23,568 KB
testcase_29 AC 228 ms
24,392 KB
testcase_30 AC 248 ms
23,800 KB
testcase_31 AC 233 ms
22,360 KB
testcase_32 AC 223 ms
23,040 KB
testcase_33 AC 241 ms
23,236 KB
testcase_34 AC 239 ms
18,320 KB
testcase_35 AC 231 ms
16,176 KB
testcase_36 AC 239 ms
18,360 KB
testcase_37 AC 230 ms
21,296 KB
testcase_38 AC 221 ms
23,312 KB
testcase_39 AC 232 ms
20,560 KB
testcase_40 AC 235 ms
21,228 KB
testcase_41 AC 236 ms
21,084 KB
testcase_42 AC 217 ms
13,648 KB
testcase_43 AC 222 ms
23,664 KB
testcase_44 AC 104 ms
11,192 KB
testcase_45 AC 128 ms
21,668 KB
testcase_46 AC 114 ms
21,668 KB
testcase_47 AC 103 ms
11,176 KB
testcase_48 AC 183 ms
15,396 KB
testcase_49 AC 179 ms
13,648 KB
testcase_50 AC 181 ms
15,116 KB
testcase_51 AC 177 ms
15,140 KB
testcase_52 AC 183 ms
15,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, M;
  cin >> N >> M;
  vector<vector<int>> E1(N), E2(N);
  for (int i = 0; i < M; i++){
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (c == 1){
      E1[a].push_back(b);
      E1[b].push_back(a);
    }
    if (c == 2){
      E2[a].push_back(b);
    }
  }
  int gcount = 0;
  vector<int> G(N, -1);
  for (int i = 0; i < N; i++){
    if (G[i] == -1){
      G[i] = gcount;
      queue<int> Q;
      Q.push(i);
      while (!Q.empty()){
        int v = Q.front();
        Q.pop();
        for (int w : E1[v]){
          if (G[w] == -1){
            G[w] = gcount;
            Q.push(w);
          }
        }
      }
      gcount++;
    }
  }
  vector<int> Vcount(gcount, 0);
  for (int i = 0; i < N; i++){
    Vcount[G[i]]++;
  }
  vector<int> Ecount(gcount, 0);
  for (int i = 0; i < N; i++){
    for (int j : E1[i]){
      Ecount[G[i]]++;
    }
  }
  vector<set<int>> E3(gcount);
  bool self_loop = false;
  for (int i = 0; i < N; i++){
    for (int j : E2[i]){
      E3[G[i]].insert(G[j]);
    }
  }
  //トポロジカルソートを試みる
  vector<int> indeg(gcount, 0);
  for (int i = 0; i < gcount; i++){
    for (int j : E3[i]){
      indeg[j]++;
    }
  }
  queue<int> Q;
  for (int i = 0; i < gcount; i++){
    if (indeg[i] == 0){
      Q.push(i);
    }
  }
  int count = 0;
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    count++;
    for (int w : E3[v]){
      indeg[w]--;
      if (indeg[w] == 0){
        Q.push(w);
      }
    }
  }
  if (count != gcount){
    cout << "Yes" << endl;
  } else {
    bool res = false;
    for (int i = 0; i < gcount; i++){
      if (Ecount[i] / 2 >= Vcount[i]){
        res = true;
      }
    }
    if (res){
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
0