結果

問題 No.3695 同室と別室
コンテスト
ユーザー ooaiu
提出日時 2026-09-09 21:57:49
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
RE  
実行時間 -
コード長 7,262 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,114 ms
コンパイル使用メモリ 360,468 KB
実行使用メモリ 15,668 KB
最終ジャッジ日時 2026-09-09 21:57:56
合計ジャッジ時間 5,667 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 7 WA * 4 RE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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


template <class F> class y_combinator {
    F f;

   public:
    y_combinator(F&& f) : f(std::forward<F>(f)) {}
    template <class... Args> auto operator()(Args&&... args) const { return f(*this, std::forward<Args>(args)...); }
};

constexpr int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};

using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;

using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vc<T>>;
template <class T, class U = std::less<T>> using prique = priority_queue<T, vector<T>, U>;

#define overload(a, b, c, d, e, ...) e
#define len(x) (ll)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep1(n) for (ll _ = 0; _ < ll(n); _++)
#define rep2(i, n) for (ll i = 0; i < ll(n); i++)
#define rep3(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define rep4(i, a, b, c) for (ll i = ll(a); i < ll(b); i += ll(c))
#define rep(...) overload(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(i, n) for (ll i = ll(n) - 1; i >= 0; i--)

template <class T> void dedup(vector<T>& a) { sort(all(a)), a.erase(unique(all(a)), a.end()); }
template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template <class T> bool chmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }

namespace cppio {

template <typename T> struct is_tuple_like : false_type {};
template <typename... Ts> struct is_tuple_like<tuple<Ts...>> : true_type {};
template <typename T, typename U> struct is_tuple_like<pair<T, U>> : true_type {};

template <typename T, typename = void> struct is_container : false_type {};
template <typename T> struct is_container<T, void_t<decltype(begin(declval<T&>()))>> : bool_constant<!is_same_v<decay_t<T>, string> && !is_same_v<decay_t<T>, char*> && !is_same_v<decay_t<T>, const char*> && !is_array_v<remove_reference_t<T>>> {};

template <typename T> void _in(T& x) {
    if constexpr (is_tuple_like<T>::value) apply([](auto&... elems) { (_in(elems), ...); }, x);
    else if constexpr (is_container<T>::value) {
        for (auto& e : x) _in(e);
    } else cin >> x;
}

template <typename... Ts> void _in_all(Ts&... args) { (_in(args), ...); }

template <typename T> void _out(const T& x) {
    bool first = true;
    if constexpr (is_tuple_like<T>::value) {
        apply([&](const auto&... elems) { ((cout << (first ? "" : " "), _out(elems), first = false), ...); }, x);
    } else if constexpr (is_container<T>::value) {
        for (const auto& e : x) {
            if (!first) cout << ' ';
            _out(e), first = false;
        }
    } else cout << x;
}

template <typename... Ts> void _print(const Ts&... args) {
    bool first = true;
    ((cout << (first ? "" : " "), _out(args), first = false), ...);
}

template <typename... Ts> void _out_all(const Ts&... args) { _print(args...), cout << '\n'; }
template <typename... Ts> void _out_no_el(const Ts&... args) { _print(args...); }

}  // namespace cppio

#define IN(...) cppio::_in_all(__VA_ARGS__)
#define OUT(...) cppio::_out_all(__VA_ARGS__)
#define out(...) cppio::_out_no_el(__VA_ARGS__)

#define INT(...) int __VA_ARGS__; IN(__VA_ARGS__)
#define LL(...)  ll __VA_ARGS__; IN(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; IN(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__; IN(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__; IN(__VA_ARGS__)
#define VEC(type, name, size) vector<type> name(size); IN(name)
#define VV(type, name, h, w)  vector<vector<type>> name(h, vector<type>(w)); IN(name)
#define RETURN(...) do { __VA_ARGS__; return; } while (0)

bool Yes(bool b = true) { OUT(b ? "Yes" : "No"); return b; }
bool No(bool b = true) { Yes(!b); return b; }
bool YES(bool b = true) { OUT(b ? "YES" : "NO"); return b; }
bool NO(bool b = true) { YES(!b); return b; }

#ifdef ONLINE_JUDGE
#define debug(...) (void(0))
#endif

template <class T> struct Compress {
   private:
    std::vector<T> vs;
    bool built = false;

   public:
    Compress() = default;
    explicit Compress(const std::vector<T>& vs) : vs(vs) {}
    template <class InputIterator> Compress(InputIterator first, InputIterator last) : vs(first, last) {}

    void reserve(size_t n) { vs.reserve(n); }

    template <class... Ts> void add(const Ts&... xs) {
        (vs.push_back(xs), ...);
        built = false;
    }

    void build() {
        std::sort(vs.begin(), vs.end());
        vs.erase(std::unique(vs.begin(), vs.end()), vs.end());
        built = true;
    }

    int rank(const T& x) const {
        assert(built);
        return (int)(lower_bound(vs.begin(), vs.end(), x) - vs.begin());
    }

    const T& operator[](size_t i) const {
        assert(built && i < vs.size());
        return vs[i];
    }

    bool exists(const T& x) const {
        assert(built);
        size_t i = this->rank(x);
        return i < this->size() && this->operator[](i) == x;
    }

    size_t size() const {
        assert(built);
        return vs.size();
    }

    static std::vector<int> compressed(const std::vector<T>& vs) {
        Compress<T> cp(vs);
        cp.build();
        std::vector<int> res;
        res.reserve(vs.size());
        for (const auto& x : vs) res.push_back(cp.rank(x));
        return res;
    }
};

#include <atcoder/dsu>
#include <atcoder/modint>
using mint = atcoder::modint998244353;
void run_case() {
    INT(N, Q);
    vi T(Q), A(Q), B(Q);
    rep(i, Q) IN(T[i], A[i], B[i]), A[i]--, B[i]--;
    vc<pii> E;
    {
        atcoder::dsu uf(N);
        rep(i, Q) {
            if (T[i] == 0) {
                uf.merge(A[i], B[i]);
            }
        }
        rep(i, Q) {
            if (T[i] == 1) {
                if (uf.same(A[i], B[i])) RETURN(OUT(0));
            }
        }
        rep(i, Q) A[i] = uf.leader(A[i]), B[i] = uf.leader(B[i]);
        vi V;
        rep(i, Q) V.push_back(A[i]), V.push_back(B[i]);
        Compress<int> cp(V);
        cp.build();
        N = cp.size();
        rep(i, Q) A[i] = cp.rank(A[i]), B[i] = cp.rank(B[i]);
        rep(i, Q) {
            if (A[i] == B[i]) continue;
            if (A[i] > B[i]) swap(A[i], B[i]);
            E.push_back({A[i], B[i]});
        }
        dedup(E);
    }
    {
        vvc<int> G(N);
        for (auto [a, b] : E) G[a].push_back(b), G[b].push_back(a);
        vi col(N, -1);
        if (!y_combinator([&](auto self, int v, int c) -> bool {
                col[v] = c;
                for (int u : G[v]) {
                    if (col[u] == -1) {
                        if (!self(u, 1 - c)) return false;
                    } else {
                        if (col[u] == c) return false;
                    }
                }
                return true;
            })(0, 0)) {
            RETURN(OUT(-1));
        }
    }
    atcoder::dsu vf(N);
    rep(i, Q) if (T[i] == 1) vf.merge(A[i], B[i]);
    int k = 0;
    rep(i, N) if (i == vf.leader(i)) k += 1;
    OUT(mint::raw(2).pow(k).val());
}
int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::fixed(std::cout).precision(16);
    ll t = 1;
    while (t--) run_case();
}

0