結果

問題 No.2277 Honest or Dishonest ?
コンテスト
ユーザー miscalc
提出日時 2023-04-21 10:29:47
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 951 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,348 ms
コンパイル使用メモリ 218,204 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2026-06-30 09:26:22
合計ジャッジ時間 6,696 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/modint>
using namespace atcoder;
using mint = modint998244353;

int main()
{
  int N, Q;
  cin >> N >> Q;
  vector<vector<pair<int, int>>> G(N);
  for (int i = 0; i < Q; i++)
  {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    G.at(a).push_back({b, c});
    G.at(b).push_back({a, c});
  }

  mint ans = 1;
  vector<int> colors(N, -1);  // -1: 未決定、0: 正直者、1: 嘘つき
  auto dfs = [&](auto self, int v) -> void
  {
    for (auto [nv, d] : G.at(v))
    {
      int new_color = colors.at(v) ^ d;
      if (colors.at(nv) == -1)
      {
        colors.at(nv) = new_color;
        self(self, nv);
      }
      else if (colors.at(nv) != new_color)  // 矛盾したら 0
        ans *= 0;
    }
  };
  for (int v = 0; v < N; v++)
  {
    if (colors.at(v) == -1)
    {
      ans *= 2;
      colors.at(v) = 0;
      dfs(dfs, v);
    }
  }
  cout << ans.val() << endl;
}
0