結果

問題 No.1023 Cyclic Tour
ユーザー SSRSSSRS
提出日時 2020-04-10 22:45:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,949 bytes
コンパイル時間 1,869 ms
コンパイル使用メモリ 181,176 KB
実行使用メモリ 25,640 KB
最終ジャッジ日時 2023-10-14 03:18:46
合計ジャッジ時間 14,155 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 WA -
testcase_02 AC 2 ms
4,352 KB
testcase_03 WA -
testcase_04 AC 107 ms
11,096 KB
testcase_05 AC 104 ms
11,112 KB
testcase_06 AC 119 ms
11,504 KB
testcase_07 AC 108 ms
10,920 KB
testcase_08 AC 122 ms
18,308 KB
testcase_09 AC 119 ms
17,828 KB
testcase_10 AC 115 ms
18,304 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 114 ms
19,500 KB
testcase_15 WA -
testcase_16 AC 202 ms
25,276 KB
testcase_17 AC 212 ms
25,640 KB
testcase_18 AC 212 ms
24,564 KB
testcase_19 AC 198 ms
24,420 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 240 ms
16,724 KB
testcase_26 AC 213 ms
13,352 KB
testcase_27 AC 185 ms
11,572 KB
testcase_28 AC 233 ms
23,584 KB
testcase_29 AC 226 ms
24,316 KB
testcase_30 AC 238 ms
23,988 KB
testcase_31 AC 223 ms
22,792 KB
testcase_32 AC 226 ms
22,984 KB
testcase_33 AC 251 ms
23,316 KB
testcase_34 AC 231 ms
18,304 KB
testcase_35 AC 228 ms
15,920 KB
testcase_36 WA -
testcase_37 AC 225 ms
21,136 KB
testcase_38 AC 218 ms
23,312 KB
testcase_39 WA -
testcase_40 AC 228 ms
21,548 KB
testcase_41 AC 238 ms
21,008 KB
testcase_42 AC 217 ms
13,548 KB
testcase_43 AC 220 ms
23,704 KB
testcase_44 AC 104 ms
11,180 KB
testcase_45 AC 130 ms
21,728 KB
testcase_46 AC 115 ms
21,856 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 182 ms
15,148 KB
testcase_52 AC 189 ms
15,184 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<set<int>> E3(gcount);
  vector<int> Ecount(gcount, 0);
  for (int i = 0; i < N; i++){
    for (int j : E1[i]){
      Ecount[G[i]]++;
    }
  }
  bool self_loop = false;
  for (int i = 0; i < N; i++){
    for (int j : E2[i]){
      E3[G[i]].insert(G[j]);
      if (G[i] == G[j]){
        self_loop = true;
      }
    }
  }
  //トポロジカルソートを試みる
  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] >= Vcount[i]){
        res = true;
      }
    }
    if (res){
      cout << "Yes" << endl;
    } else if (self_loop){
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
0