結果

問題 No.2277 Honest or Dishonest ?
ユーザー miscalc
提出日時 2023-04-21 10:29:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 951 bytes
コンパイル時間 1,914 ms
コンパイル使用メモリ 201,068 KB
最終ジャッジ日時 2025-02-12 10:52:27
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#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