QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#71967#4654. TreeIsrothyCompile Error//C++235.7kb2023-01-12 22:38:262023-01-12 22:38:29

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-12 22:38:29]
  • 评测
  • [2023-01-12 22:38:26]
  • 提交

answer

#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <vector>

const int MOD = 1e9 + 7;

long long ex_gcd(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = ex_gcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

long long inv(long long a) {
    long long x, y;
    a = (a + MOD) % MOD;
    ex_gcd(a, MOD, x, y);
    return (x % MOD + MOD) % MOD;
}

struct Matrix {
    std::vector<std::vector<long long>> mat;
    size_t n, m;
    Matrix(size_t n, size_t m) : n(n), m(m) {
        mat.resize(n);
        for (auto &v: mat) {
            v.resize(m);
        }
    }
    long long &operator()(size_t i, size_t j) {
        return mat[i][j];
    }
    std::vector<long long> &operator()(size_t i) {
        return mat[i];
    }

    Matrix argument(std::vector<long long> v) const {
        assert(n == v.size());
        Matrix res(n, m + 1);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                res(i, j) = mat[i][j];
            }
            res(i, m) = v[i];
        }
        return res;
    }

    Matrix transpose() const {
        Matrix res(m, n);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                res(j, i) = mat[i][j];
            }
        }
        return res;
    }

    [[nodiscard]] Matrix remove_column(size_t k) const {
        assert(m != 1 && k < m);
        Matrix res(n, m - 1);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m - 1; ++j) {
                res(i, j) = mat[i][j < k ? j : j + 1];
            }
        }
        return res;
    }
    Matrix remove_row(size_t k) const {
        assert(n != 1 && k < n);
        Matrix res(n - 1, m);
        for (int i = 0; i < n - 1; ++i) {
            for (int j = 0; j < m; ++j) {
                res(i, j) = mat[i < k ? i : i + 1][j];
            }
        }
        return res;
    }

    long long determinate() const {
        assert(n == m);
        Matrix tmp = *this;
        long long res = 1;
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                while (tmp(j, i) != 0) {
                    long long t = tmp(i, i) / tmp(j, i);
                    for (int k = i; k < n; ++k) {
                        tmp(i, k) = (tmp(i, k) - t * tmp(j, k)) % MOD;
                    }
                    std::swap(tmp(i), tmp(j));
                    res = -res;
                }
            }
            res = (res * tmp(i, i)) % MOD;
        }
        return res;
    }

    std::vector<std::vector<long long>> Gaussian_elimination(const std::vector<long long> &v) const {
        assert(n == v.size());
        std::vector<T> v0(m);
        Matrix tmp = this->argument(v);
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                while (tmp(j, i) != 0) {
                    long long t = tmp(i, i) / tmp(j, i);
                    for (int k = i; k <= m; ++k) {
                        tmp(i, k) = (tmp(i, k) - t * tmp(j, k)) % MOD;
                    }
                    std::swap(tmp(i), tmp(j));
                }
            }
        }

        for (int i = n - 1; i >= 0; --i) {
            if (tmp(i, i) == 0) {
                continue;
            }
            long long t = inv(tmp(i, i));
            for (int j = i; j <= m; ++j) {
                tmp(i, j) = tmp(i, j) * t % MOD;
            }
            tmp(i, i) = 1;
            for (int j = 0; j < i; ++j) {
                auto s = tmp(j, i);
                for (int k = i; k <= m; ++k) {
                    tmp(j, k) = (tmp(j, k) - tmp(i, k) * s) % MOD;
                }
            }
            v0[i] = tmp(i, m);
        }
        std::vector<std::vector<long long>> res;
        res.push_back(v0);
        for (int i = 0; i < n; ++i) {
            if (tmp(i, i) == 0) {
                std::vector<long long> v(m, 0);
                v[i] = 1;
                for (int j = 0; j < n; ++j) {
                    if (i != j) {
                        v[j] = -tmp(j, i);
                    }
                }
                res.push_back(v);
            }
        }
        return res;
    }
};

int main() {
    int _;
    for (scanf("%d", &_); _ != 0; --_) {
        int n, m;
        scanf("%d%d", &n, &m);
        if (n == 1) {
            puts("1");
            continue;
        }
        Matrix mat(n, n);
        for (int i = 0; i < m; ++i) {
            int u, v;
            scanf("%d%d", &u, &v);
            --u, --v;
            mat(u, v) -= 1;
            mat(v, v) += 1;
        }
        auto x = mat.Gaussian_elimination(std::vector<long long>(n, 0))[1];
        auto y = mat.transpose().Gaussian_elimination(std::vector<long long>(n, 0))[1];
        auto none_zero = [](int x) {
            return x != 0;
        };
        int i = (int) (std::find_if(x.begin(), x.end(), none_zero) - x.begin());
        int j = (int) (std::find_if(y.begin(), y.end(), none_zero) - y.begin());
        auto tx = inv(x[i]);
        auto ty = inv(y[j]);
        for (int k = 0; k < n; ++k) {
            x[k] = x[k] * tx % MOD;
            y[k] = y[k] * ty % MOD;
        }
        auto det = mat.remove_column(i).remove_row(j).determinate();
        if ((i + j) & 1) {
            det = -det;
        }
        for (int k = 0; k < n; ++k) {
            long long ans = det * x[k] % MOD * y[k] % MOD;
            printf("%lld ", (ans + MOD) % MOD);
        }
        puts("");
    }
    return 0;
}

Details

answer.code: In member function ‘std::vector<std::vector<long long int> > Matrix::Gaussian_elimination(const std::vector<long long int>&) const’:
answer.code:108:21: error: ‘T’ was not declared in this scope
  108 |         std::vector<T> v0(m);
      |                     ^
answer.code:108:22: error: template argument 1 is invalid
  108 |         std::vector<T> v0(m);
      |                      ^
answer.code:108:22: error: template argument 2 is invalid
answer.code:137:15: error: invalid types ‘int[int]’ for array subscript
  137 |             v0[i] = tmp(i, m);
      |               ^
answer.code:140:22: error: no matching function for call to ‘std::vector<std::vector<long long int> >::push_back(int&)’
  140 |         res.push_back(v0);
      |         ~~~~~~~~~~~~~^~~~
In file included from /usr/include/c++/11/vector:67,
                 from /usr/include/c++/11/functional:62,
                 from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,
                 from /usr/include/c++/11/algorithm:74,
                 from answer.code:1:
/usr/include/c++/11/bits/stl_vector.h:1187:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::vector<long long int>; _Alloc = std::allocator<std::vector<long long int> >; std::vector<_Tp, _Alloc>::value_type = std::vector<long long int>]’
 1187 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/11/bits/stl_vector.h:1187:35: note:   no known conversion for argument 1 from ‘int’ to ‘const value_type&’ {aka ‘const std::vector<long long int>&’}
 1187 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/11/bits/stl_vector.h:1203:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::vector<long long int>; _Alloc = std::allocator<std::vector<long long int> >; std::vector<_Tp, _Alloc>::value_type = std::vector<long long int>]’
 1203 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/11/bits/stl_vector.h:1203:30: note:   no known conversion for argument 1 from ‘int’ to ‘std::vector<std::vector<long long int> >::value_type&&’ {aka ‘std::vector<long long int>&&’}
 1203 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
answer.code: In function ‘int main()’:
answer.code:159:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  159 |     for (scanf("%d", &_); _ != 0; --_) {
      |          ~~~~~^~~~~~~~~~
answer.code:161:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  161 |         scanf("%d%d", &n, &m);
      |         ~~~~~^~~~~~~~~~~~~~~~
answer.code:169:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  169 |             scanf("%d%d", &u, &v);
      |             ~~~~~^~~~~~~~~~~~~~~~