結果

問題 No.1421 国勢調査 (Hard)
ユーザー tokusakuraitokusakurai
提出日時 2021-03-05 22:22:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 934 ms / 2,000 ms
コード長 6,964 bytes
コンパイル時間 2,684 ms
コンパイル使用メモリ 212,768 KB
実行使用メモリ 9,376 KB
最終ジャッジ日時 2024-04-16 10:07:33
合計ジャッジ時間 9,263 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 929 ms
9,252 KB
testcase_23 AC 931 ms
9,376 KB
testcase_24 AC 924 ms
8,996 KB
testcase_25 AC 925 ms
9,252 KB
testcase_26 AC 934 ms
9,120 KB
testcase_27 AC 56 ms
9,088 KB
testcase_28 AC 145 ms
9,128 KB
testcase_29 AC 85 ms
9,248 KB
testcase_30 AC 54 ms
9,088 KB
testcase_31 AC 84 ms
9,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

template<int mod>
struct Mod_Int{
    int x;

    Mod_Int() : x(0) {}

    Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    Mod_Int &operator += (const Mod_Int &p){
        if((x += p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator -= (const Mod_Int &p){
        if((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator *= (const Mod_Int &p){
        x = (int) (1LL * x * p.x % mod);
        return *this;
    }

    Mod_Int &operator /= (const Mod_Int &p){
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator ++ () {return *this += Mod_Int(1);}

    Mod_Int operator ++ (int){
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator -- () {return *this -= Mod_Int(1);}

    Mod_Int operator -- (int){
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator - () const {return Mod_Int(-x);}

    Mod_Int operator + (const Mod_Int &p) const {return Mod_Int(*this) += p;}

    Mod_Int operator - (const Mod_Int &p) const {return Mod_Int(*this) -= p;}

    Mod_Int operator * (const Mod_Int &p) const {return Mod_Int(*this) *= p;}

    Mod_Int operator / (const Mod_Int &p) const {return Mod_Int(*this) /= p;}

    bool operator == (const Mod_Int &p) const {return x == p.x;}

    bool operator != (const Mod_Int &p) const {return x != p.x;}

    Mod_Int inverse() const{
        assert(*this != Mod_Int(0));
        return pow(mod-2);
    }

    Mod_Int pow(long long k) const{
        Mod_Int now = *this, ret = 1;
        for(; k > 0; k >>= 1, now *= now){
            if(k&1) ret *= now;
        }
        return ret;
    }

    friend ostream &operator << (ostream &os, const Mod_Int &p){
        return os << p.x;
    }

    friend istream &operator >> (istream &is, Mod_Int &p){
        long long a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

using mint = Mod_Int<2>;

template<typename T>
struct Matrix{
    vector<vector<T>> A;

    Matrix(int n, int m) : A(n, vector<T>(m, 0)) {}

    int height() const {return A.size();}

    int width() const {return A.front().size();}

    inline const vector<T> &operator [] (int k) const {return A[k];}

    inline vector<T> &operator [] (int k) {return A[k];}

    static Matrix I(int l){
        Matrix ret(l, l);
        for(int i = 0; i < l; i++) ret[i][i] = 1;
        return ret;
    }

    Matrix &operator *= (const Matrix &B){
        int n = height(), m = width(), p = B.width();
        assert(m == B.height());
        Matrix ret(n, p);
        for(int i = 0; i < n; i++){
            for(int k = 0; k < m; k++){
                for(int j = 0; j < p; j++){
                    ret[i][j] += A[i][k]*B[k][j];
                }
            }
        }
        swap(A, ret.A);
        return *this;
    }

    Matrix operator * (const Matrix &B) const {return Matrix(*this) *= B;}

    Matrix pow(long long k) const{
        int n = height(), m = width();
        assert(n == m);
        Matrix now = *this, ret = I(n);
        for(; k > 0; k >>= 1, now *= now){
            if(k&1) ret *= now;
        }
        return ret;
    }

    bool eq(const T &a, const T &b) const{
        return a == b;
        //return abs(a-b) <= EPS;
    }

    pair<int, T> row_reduction(){
        int n = height(), m = width(), check = 0, rank = 0;
        T det = 1;
        for(int j = 0; j < m; j++){
            int pivot = check;
            for(int i = check; i < n; i++){
                if(A[i][j] != 0) pivot = i;
                //if(abs(A[i][j]) > abs(A[pivot][j])) pivot = i;
            }
            if(check != pivot) det *= T(-1);
            swap(A[check], A[pivot]);
            if(eq(A[check][j], 0)) {det = T(0); continue;}
            rank++;
            det *= A[check][j];
            for(int k = j+1; k < m; k++) A[check][k] /= A[check][j];
            A[check][j] = 1;
            for(int i = 0; i < n; i++){
                if(i == check) continue;
                for(int k = j+1; k < m; k++) A[i][k] -= A[i][j]*A[check][k];
                A[i][j] = 0;
            }
            if(++check == n) break;
        }
        return make_pair(rank, det);
    }

    vector<vector<T>> Gausiann_elimination(const vector<T> &b){
        int n = height(), m = width();
        assert(b.size() == n);
        for(int i = 0; i < n; i++) A[i].push_back(b[i]);
        row_reduction();
        vector<vector<T>> ret;
        vector<int> p(n, m+1);
        vector<bool> is_zero(m, true);
        for(int i = 0; i < n; i++){
            for(int j = 0; j <= m; j++){
                if(!eq(A[i][j], 0)) {p[i] = j; break;}
            }
            if(p[i] < m) is_zero[p[i]] = false;
            if(p[i] == m) return ret;
        }
        vector<T> basis(m, 0);
        for(int i = 0; i < n; i++){
            if(p[i] < m) basis[p[i]] = A[i][m];
        }
        ret.push_back(basis);
        for(int j = 0; j < m; j++){
            if(!is_zero[j]) continue;
            basis[j] = 1;
            for(int i = 0; i < n; i++){
                if(p[i] < m) basis[p[i]] = -A[i][j];
            }
            ret.push_back(basis), basis[j] = 0;
        }
        return ret;
    }
};

int main(){
    int N, M; cin >> N >> M;

    vector<int> A(M), Y(M);
    vector<vector<int>> B(M);

    rep(i, M){
        cin >> A[i];
        B[i].resize(A[i]);
        rep(j, A[i]) {cin >> B[i][j]; B[i][j]--;}
        cin >> Y[i];
    }

    using mat = Matrix<mint>;

    vector<int> ret(N, 0);

    rep(i, 30){
        mat a(M, N);
        vector<mint> x(M, 0);
        rep(j, M){
            rep(k, A[j]){
                a[j][B[j][k]] = 1;
            }
            if((Y[j]>>i)&1) x[j] = 1;
        }

        vector<vector<mint>> ans = a.Gausiann_elimination(x);

        if(ans.empty()) {cout << "-1\n"; return 0;}

        rep(j, N){
            if(ans[0][j] == 1) ret[j] |= (1<<i);
        }
    }

    rep(i, N) cout << ret[i] << '\n';
}
0