結果
問題 | No.2277 Honest or Dishonest ? |
ユーザー |
![]() |
提出日時 | 2023-04-21 21:41:17 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 47 ms / 2,000 ms |
コード長 | 1,914 bytes |
コンパイル時間 | 2,131 ms |
コンパイル使用メモリ | 199,300 KB |
最終ジャッジ日時 | 2025-02-12 11:16:48 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 50 |
ソースコード
#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; }