結果

問題 No.2277 Honest or Dishonest ?
ユーザー simansiman
提出日時 2023-04-23 20:21:47
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 1,849 bytes
コンパイル時間 1,566 ms
コンパイル使用メモリ 145,224 KB
実行使用メモリ 10,368 KB
最終ジャッジ日時 2024-04-25 19:17:08
合計ジャッジ時間 7,010 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 98 ms
9,728 KB
testcase_04 AC 98 ms
9,600 KB
testcase_05 AC 34 ms
7,072 KB
testcase_06 AC 84 ms
9,472 KB
testcase_07 AC 65 ms
8,424 KB
testcase_08 AC 25 ms
6,940 KB
testcase_09 AC 60 ms
7,936 KB
testcase_10 AC 49 ms
7,936 KB
testcase_11 AC 22 ms
6,940 KB
testcase_12 AC 43 ms
8,064 KB
testcase_13 AC 93 ms
9,700 KB
testcase_14 AC 52 ms
8,444 KB
testcase_15 AC 27 ms
6,940 KB
testcase_16 AC 64 ms
8,420 KB
testcase_17 AC 31 ms
7,168 KB
testcase_18 AC 89 ms
9,000 KB
testcase_19 AC 91 ms
9,224 KB
testcase_20 AC 74 ms
8,576 KB
testcase_21 AC 66 ms
9,072 KB
testcase_22 AC 62 ms
8,192 KB
testcase_23 AC 88 ms
9,912 KB
testcase_24 AC 55 ms
7,776 KB
testcase_25 AC 69 ms
8,320 KB
testcase_26 AC 16 ms
6,944 KB
testcase_27 AC 44 ms
7,808 KB
testcase_28 AC 29 ms
6,948 KB
testcase_29 AC 47 ms
7,560 KB
testcase_30 AC 41 ms
7,680 KB
testcase_31 AC 73 ms
8,792 KB
testcase_32 AC 44 ms
7,392 KB
testcase_33 AC 91 ms
9,248 KB
testcase_34 AC 104 ms
9,640 KB
testcase_35 AC 11 ms
6,940 KB
testcase_36 AC 63 ms
8,576 KB
testcase_37 AC 98 ms
9,984 KB
testcase_38 AC 23 ms
7,040 KB
testcase_39 AC 30 ms
7,168 KB
testcase_40 AC 12 ms
6,940 KB
testcase_41 AC 112 ms
10,152 KB
testcase_42 AC 75 ms
8,832 KB
testcase_43 AC 123 ms
10,368 KB
testcase_44 AC 118 ms
10,216 KB
testcase_45 AC 120 ms
10,092 KB
testcase_46 AC 122 ms
10,240 KB
testcase_47 AC 121 ms
10,240 KB
testcase_48 AC 120 ms
10,240 KB
testcase_49 AC 120 ms
10,168 KB
testcase_50 AC 122 ms
10,240 KB
testcase_51 AC 122 ms
10,112 KB
testcase_52 AC 123 ms
10,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

const ll MOD = 998244353;

struct Node {
  int id;
  int cnt;

  Node(int id = -1, int cnt = -1) {
    this->id = id;
    this->cnt = cnt;
  }

  bool operator>(const Node &n) const {
    return cnt < n.cnt;
  }
};

struct Edge {
  int a;
  int b;
  int c;

  Edge(int a = -1, int b = -1, int c = -1) {
    this->a = a;
    this->b = b;
    this->c = c;
  }
};

vector<Edge> G[100010];

int main() {
  int N, Q;
  cin >> N >> Q;

  for (int i = 0; i < Q; ++i) {
    int a, b, c;
    cin >> a >> b >> c;

    G[a].push_back(Edge(a, b, c));
    G[b].push_back(Edge(b, a, c));
  }

  ll ans = 1;
  const int UNDEFINED = -1;
  vector<bool> visited(N + 1, false);
  vector<int> is_liar(N + 1, UNDEFINED);

  for (int i = 1; i <= N; ++i) {
    if (visited[i]) continue;

    queue<int> que;
    que.push(i);
    is_liar[i] = 0;
    bool ok = true;
    // fprintf(stderr, "i: %d\n", i);

    while (not que.empty() && ok) {
      int v = que.front();
      que.pop();

      if (visited[v]) continue;
      visited[v] = true;
      // fprintf(stderr, "  v: %d\n", v);

      for (Edge &e : G[v]) {
        if (is_liar[e.b] != UNDEFINED) {
          if (is_liar[e.a] == 0) {
            ok &= (is_liar[e.b] == e.c);
          } else {
            ok &= (is_liar[e.b] != e.c);
          }
        } else {
          if (is_liar[e.a] == 0) {
            is_liar[e.b] = e.c;
          } else {
            is_liar[e.b] = e.c ^ 1;
          }
        }

        que.push(e.b);
      }
    }

    if (ok) {
      ans *= 2;
    } else {
      ans *= 0;
    }
    ans %= MOD;
  }

  cout << ans << endl;


  return 0;
}
0