QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#71968 | #4654. Tree | Isrothy | AC ✓ | 1032ms | 11292kb | C++23 | 5.7kb | 2023-01-12 22:39:12 | 2023-01-12 22:39:12 |
Judging History
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<long long> 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
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1032ms
memory: 11292kb
input:
100 7 12 1 3 2 1 1 4 5 1 4 7 6 5 2 3 4 6 3 1 6 4 7 1 1 2 2 2 2 1 1 2 50 49 49 29 41 33 41 48 27 46 41 36 9 1 41 7 17 12 23 10 8 15 32 6 21 45 18 40 49 21 41 17 41 8 2 35 17 16 38 31 13 5 32 20 36 3 25 18 29 47 29 37 15 23 11 13 29 9 47 44 29 39 41 27 25 28 36 25 19 42 27 38 45 34 15 19 14 24 11 43 1...
output:
2 3 1 4 2 6 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 850518977 850518977 850518977 850518977 850518977 850518977 850518977 8...
result:
ok 100 lines