結果

問題 No.2277 Honest or Dishonest ?
ユーザー 沙耶花沙耶花
提出日時 2023-04-21 21:39:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 2,801 bytes
コンパイル時間 4,826 ms
コンパイル使用メモリ 259,088 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2024-04-25 18:51:27
合計ジャッジ時間 8,398 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 70 ms
6,944 KB
testcase_04 AC 73 ms
8,320 KB
testcase_05 AC 26 ms
8,448 KB
testcase_06 AC 59 ms
6,940 KB
testcase_07 AC 49 ms
6,944 KB
testcase_08 AC 22 ms
9,344 KB
testcase_09 AC 43 ms
7,552 KB
testcase_10 AC 37 ms
6,944 KB
testcase_11 AC 17 ms
6,944 KB
testcase_12 AC 33 ms
6,940 KB
testcase_13 AC 63 ms
6,944 KB
testcase_14 AC 40 ms
6,940 KB
testcase_15 AC 21 ms
7,424 KB
testcase_16 AC 44 ms
6,940 KB
testcase_17 AC 24 ms
6,940 KB
testcase_18 AC 62 ms
8,448 KB
testcase_19 AC 64 ms
8,448 KB
testcase_20 AC 55 ms
6,944 KB
testcase_21 AC 50 ms
6,948 KB
testcase_22 AC 45 ms
6,940 KB
testcase_23 AC 64 ms
6,940 KB
testcase_24 AC 40 ms
6,940 KB
testcase_25 AC 47 ms
8,448 KB
testcase_26 AC 15 ms
9,088 KB
testcase_27 AC 32 ms
6,944 KB
testcase_28 AC 22 ms
7,168 KB
testcase_29 AC 34 ms
8,064 KB
testcase_30 AC 28 ms
6,944 KB
testcase_31 AC 47 ms
6,940 KB
testcase_32 AC 31 ms
7,808 KB
testcase_33 AC 60 ms
7,936 KB
testcase_34 AC 66 ms
6,944 KB
testcase_35 AC 10 ms
6,940 KB
testcase_36 AC 42 ms
6,940 KB
testcase_37 AC 68 ms
6,940 KB
testcase_38 AC 17 ms
6,944 KB
testcase_39 AC 23 ms
6,940 KB
testcase_40 AC 10 ms
6,944 KB
testcase_41 AC 80 ms
6,940 KB
testcase_42 AC 50 ms
6,940 KB
testcase_43 AC 77 ms
6,944 KB
testcase_44 AC 80 ms
9,088 KB
testcase_45 AC 81 ms
9,176 KB
testcase_46 AC 80 ms
9,088 KB
testcase_47 AC 78 ms
9,216 KB
testcase_48 AC 79 ms
9,088 KB
testcase_49 AC 76 ms
6,944 KB
testcase_50 AC 75 ms
6,940 KB
testcase_51 AC 78 ms
6,944 KB
testcase_52 AC 78 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int potentialized_dsu<S>::merge(int, int, S) [with S = int]':
main.cpp:27:27: warning: 't' may be used uninitialized [-Wmaybe-uninitialized]
   27 |                         v = t - v;
      |                         ~~^~~~~~~
main.cpp:26:27: note: 't' was declared here
   26 |                         S t;
      |                           ^

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 1000000000000000001

template <class S>
struct potentialized_dsu {
  public:
    potentialized_dsu() : _n(0) {}
    potentialized_dsu(int n) : _n(n), parent_or_size(n, -1), d(n) {}

    int merge(int a, int b, S v) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
		
		if (-parent_or_size[x] < -parent_or_size[y]){
			std::swap(x, y);
			std::swap(a, b);
			S t;
			v = t - v;
		}
		
		v += diff(x,a);
		v -= diff(y,b);
		
		d[y] += d[x] + v;
		
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
		
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
		int l = leader(parent_or_size[a]);
		if(parent_or_size[a] != l){
			d[a] += d[parent_or_size[a]];
			parent_or_size[a] = l;
		}
        return parent_or_size[a];
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }
	
	S diff(int a,int b){
		assert(0 <= a && a < _n);
		assert(0 <= b && b < _n);
		leader(a);
		leader(b);
		return d[b] - d[a];
	}

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
	std::vector<S> d;
};


int main(){
	
	int n,q;
	cin>>n>>q;
	vector<int> a(q),b(q),c(q);
	potentialized_dsu<int> D(n);
	rep(i,q){
		cin>>a[i]>>b[i]>>c[i];
		a[i]--,b[i]--;
		if(c[i]==0)D.merge(a[i],b[i],0);
	}
	rep(i,q){
		if(c[i]==1){
			if(D.same(a[i],b[i])){
				if(D.diff(a[i],b[i])%2==0){
					cout<<0<<endl;
					return 0;
				}
			}
			D.merge(a[i],b[i],1);
		}
	}
	
	mint ans = mint(2).pow(D.groups().size());
	cout<<ans.val()<<endl;
	
	return 0;
}
0