結果

問題 No.2277 Honest or Dishonest ?
ユーザー hongrockhongrock
提出日時 2023-04-21 21:41:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,914 bytes
コンパイル時間 2,242 ms
コンパイル使用メモリ 202,332 KB
実行使用メモリ 8,736 KB
最終ジャッジ日時 2024-04-25 18:51:33
合計ジャッジ時間 5,455 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 32 ms
7,412 KB
testcase_04 AC 37 ms
8,272 KB
testcase_05 AC 14 ms
7,552 KB
testcase_06 AC 25 ms
6,964 KB
testcase_07 AC 23 ms
6,944 KB
testcase_08 AC 11 ms
7,408 KB
testcase_09 AC 23 ms
7,860 KB
testcase_10 AC 17 ms
6,944 KB
testcase_11 AC 10 ms
6,940 KB
testcase_12 AC 15 ms
6,940 KB
testcase_13 AC 31 ms
7,272 KB
testcase_14 AC 19 ms
7,060 KB
testcase_15 AC 12 ms
7,152 KB
testcase_16 AC 22 ms
6,944 KB
testcase_17 AC 13 ms
6,944 KB
testcase_18 AC 36 ms
8,156 KB
testcase_19 AC 35 ms
8,248 KB
testcase_20 AC 27 ms
7,800 KB
testcase_21 AC 22 ms
6,940 KB
testcase_22 AC 24 ms
7,752 KB
testcase_23 AC 27 ms
6,944 KB
testcase_24 AC 23 ms
7,484 KB
testcase_25 AC 27 ms
8,092 KB
testcase_26 AC 7 ms
7,364 KB
testcase_27 AC 15 ms
6,940 KB
testcase_28 AC 13 ms
7,168 KB
testcase_29 AC 19 ms
7,620 KB
testcase_30 AC 15 ms
6,940 KB
testcase_31 AC 24 ms
7,012 KB
testcase_32 AC 18 ms
7,488 KB
testcase_33 AC 33 ms
8,024 KB
testcase_34 AC 33 ms
7,584 KB
testcase_35 AC 5 ms
6,944 KB
testcase_36 AC 22 ms
6,940 KB
testcase_37 AC 28 ms
6,964 KB
testcase_38 AC 9 ms
6,944 KB
testcase_39 AC 11 ms
6,944 KB
testcase_40 AC 6 ms
6,940 KB
testcase_41 AC 33 ms
7,184 KB
testcase_42 AC 25 ms
7,200 KB
testcase_43 AC 42 ms
8,664 KB
testcase_44 AC 44 ms
8,608 KB
testcase_45 AC 42 ms
8,724 KB
testcase_46 AC 43 ms
8,736 KB
testcase_47 AC 40 ms
8,608 KB
testcase_48 AC 42 ms
8,608 KB
testcase_49 AC 39 ms
8,476 KB
testcase_50 AC 36 ms
8,604 KB
testcase_51 AC 36 ms
8,612 KB
testcase_52 AC 39 ms
8,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define rep(i, a, n) for(int i=(a); i<(n); ++i)
#define per(i, a, n) for(int i=(a); i>(n); --i)
#define pb emplace_back
#define mp make_pair
#define clr(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(),(x).end()
#define lowbit(x) (x & -x)
#define fi first
#define se second
#define lson o<<1
#define rson o<<1|1
#define gmid l[o]+r[o]>>1
 
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
using ui = unsigned int;
 
constexpr int mod = 998244353;
constexpr int inf = 0x3f3f3f3f;
constexpr double EPS = 1e-8;
const double PI = acos(-1.0);

constexpr int N = 1e5 + 10;

int n, Q, m, fa[N], sz[N];
vector<pii> G;
vector<int> V[N];
int color[N];

int find_fa(int x){
  return x == fa[x] ? x : (fa[x] = find_fa(fa[x]));
}

void join(int x, int y){
  x = find_fa(x);
  y = find_fa(y);
  if(x == y)  return;
  --m;
  if(sz[x] < sz[y]) swap(x, y);
  fa[y] = x;
  sz[x] += sz[y];
}

bool dfs(int x){
  for(int y : V[x]){
    int z = find_fa(y);
    if(color[z]){
      if(color[x] == color[z])  return 0;
    } else {
      color[z] = 3 - color[x];
      if(!dfs(z)) return 0;
    }
  }
  return 1;
}

ll solve(){
  for(auto [x, y] : G){
    int x0 = find_fa(x);
    int y0 = find_fa(y);
    if(x0 == y0)  return 0;
    V[x0].pb(y0);
    V[y0].pb(x0);
  }
  ll ans = 1;
  
  rep(i, 1, n + 1){
    int x = find_fa(i);
    if(x != i)  continue;
    if(color[x] == 0){
      color[x] = 1;
      if(!dfs(x)) return 0;
      ans = ans * 2 % mod;
    }
  }
  return ans;
}

void _main(){
  int x, y, z;
  cin >> n >> Q;
  rep(i, 1, n + 1){
    fa[i] = i;
    sz[i] = 1;
  }
  m = n;
  while(Q--){
    cin >> x >> y >> z;
    if(z == 0){
      join(x, y);
    } else {
      G.pb(x, y);
    }
  }
  cout << solve() << '\n';
}

int main(){
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  _main();
  return 0;
}
0