QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#804241 | #9866. Extracting Weights | ucup-team133# | WA | 1ms | 3872kb | C++23 | 8.3kb | 2024-12-07 21:12:44 | 2024-12-07 21:12:45 |
Judging History
answer
#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif
template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& v) {
for (auto& e : v) {
is >> e;
}
return is;
}
template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
for (std::string_view sep = ""; const auto& e : v) {
os << std::exchange(sep, " ") << e;
}
return os;
}
template <class T, class U = T> bool chmin(T& x, U&& y) {
return y < x and (x = std::forward<U>(y), true);
}
template <class T, class U = T> bool chmax(T& x, U&& y) {
return x < y and (x = std::forward<U>(y), true);
}
template <class T> void mkuni(std::vector<T>& v) {
std::ranges::sort(v);
auto result = std::ranges::unique(v);
v.erase(result.begin(), result.end());
}
template <class T> int lwb(const std::vector<T>& v, const T& x) {
return std::distance(v.begin(), std::ranges::lower_bound(v, x));
}
template <int MAX_W> struct F2Matrix {
int H, W;
std::vector<std::bitset<MAX_W>> A;
F2Matrix(int H, int W) : H(H), W(W), A(H) {
assert(W <= MAX_W);
for (int i = 0; i < H; i++) A[i].reset();
}
bool empty() const { return A.empty(); }
int size() const { return H; }
int height() const { return H; }
int width() const { return W; }
inline const std::bitset<MAX_W>& operator[](int i) const { return A[i]; }
inline std::bitset<MAX_W>& operator[](int i) { return A[i]; }
static F2Matrix identity(int n) {
F2Matrix res(n, n);
for (int i = 0; i < n; i++) res[i][i] = 1;
return res;
}
F2Matrix& operator+=(const F2Matrix& B) {
assert(H == B.height() and W == B.width());
for (int i = 0; i < H; i++) (*this)[i] ^= B[i];
return *this;
}
F2Matrix& operator-=(const F2Matrix& B) { return *this += B; }
F2Matrix& operator*=(const F2Matrix& B) {
assert(W == B.height());
F2Matrix C(H, B.width());
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (A[i][j]) {
C[i] ^= B[j];
}
}
}
std::swap(A, C.A);
return *this;
}
F2Matrix operator+(const F2Matrix& B) const { return F2Matrix(*this) += B; }
F2Matrix operator-(const F2Matrix& B) const { return F2Matrix(*this) -= B; }
F2Matrix operator*(const F2Matrix& B) const { return F2Matrix(*this) *= B; }
F2Matrix pow(long long n) const {
assert(H == W);
assert(0 <= n);
F2Matrix x = *this, r = identity(H);
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
int rank() const { return F2Matrix(*this).gauss_jordan(); }
int det() const {
assert(H == W);
return rank() == H ? 1 : 0;
}
F2Matrix inv() const {
assert(height() == width());
int n = height();
F2Matrix B(*this), C = identity(n);
for (int j = 0; j < n; j++) {
int pivot = -1;
for (int i = j; i < n; i++) {
if (B[i][j]) {
pivot = i;
break;
}
}
assert(pivot != -1);
if (pivot != j) {
std::swap(B[pivot], B[j]);
std::swap(C[pivot], C[j]);
}
for (int i = 0; i < n; i++) {
if (i != j and B[i][j]) {
B[i] ^= B[j];
C[i] ^= C[j];
}
}
}
return C;
}
std::vector<std::bitset<MAX_W>> system_of_linear_equations(const std::vector<bool>& b) {
assert(W + 1 <= MAX_W);
assert(int(b.size()) == H);
F2Matrix B(*this);
for (int i = 0; i < H; i++) B[i][W] = b[i];
int rank = B.gauss_jordan(W);
for (int i = rank; i < H; i++) {
if (B[i][W]) {
return {};
}
}
std::vector<std::bitset<MAX_W>> res(1);
std::vector<int> pivot(W, -1);
for (int i = 0, j = 0; i < rank; i++) {
while (not B[i][j]) j++;
res[0][j] = B[i][W];
pivot[j] = i;
}
for (int j = 0; j < W; j++) {
if (pivot[j] != -1) continue;
std::bitset<MAX_W> x;
x[j] = 1;
for (int k = 0; k < j; k++) {
if (pivot[k] != -1) {
x[k] = B[pivot[k]][j];
}
}
res.emplace_back(x);
}
return res;
}
private:
int gauss_jordan(int pivot_end = -1) {
if (empty()) return 0;
if (pivot_end == -1) pivot_end = W;
assert(pivot_end <= MAX_W);
int rank = 0;
for (int j = 0; j < pivot_end; j++) {
int pivot = -1;
for (int i = rank; i < H; i++) {
if ((*this)[i][j]) {
pivot = i;
break;
}
}
if (pivot == -1) continue;
if (pivot != rank) std::swap((*this)[pivot], (*this)[rank]);
for (int i = 0; i < H; i++) {
if (i != rank and (*this)[i][j]) {
(*this)[i] ^= (*this)[rank];
}
}
rank++;
}
return rank;
}
};
using namespace std;
using ll = long long;
const int MAX_N = 1 << 8;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
int n, k;
cin >> n >> k;
vector<vector<int>> G(n);
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
x--, y--;
G[x].emplace_back(y);
G[y].emplace_back(x);
}
vector<bool> path(n, false);
vector<vector<bool>> mat;
vector<pair<int, int>> es;
{
path[0] = true;
mat.emplace_back(path);
es.emplace_back(-1, 0);
path[0] = false;
}
auto dfs = [&](auto self, int v, int p, int d, int r) -> void {
path[v] = true;
if (d == k) {
if (r < v) {
mat.emplace_back(path);
es.emplace_back(r, v);
}
path[v] = false;
return;
}
for (int& u : G[v]) {
if (u == p) continue;
self(self, u, v, d + 1, r);
}
path[v] = false;
};
for (int i = 0; i < n; i++) dfs(dfs, i, -1, 0, i);
int row = es.size();
F2Matrix<MAX_N> M(row, n);
for (int i = 0; i < row; i++) {
for (int j = 0; j < n; j++) {
M[i][j] = mat[i][j];
}
}
int rank = 0;
vector<int> ord(row);
iota(ord.begin(), ord.end(), 0);
for (int j = 0; j < n; j++) {
int pivot = -1;
for (int i = rank; i < row; i++) {
if (M[i][j]) {
pivot = i;
break;
}
}
if (pivot == -1) {
cout << "NO" << endl;
return 0;
}
if (pivot != rank) {
swap(M[pivot], M[rank]);
swap(ord[pivot], ord[rank]);
}
for (int i = 0; i < row; i++) {
if (i != rank and M[i][j]) {
M[i] ^= M[rank];
}
}
rank++;
}
assert(ord[0] == 0);
cout << "YES" << endl;
cout << "! " << n - 1;
for (int i = 1; i < n; i++) {
auto [u, v] = es[ord[i]];
cout << " " << u + 1 << " " << v + 1;
}
cout << endl;
vector<int> w(n);
w[0] = 0;
for (int i = 1; i < n; i++) {
cin >> w[i];
assert(w[i] != -1);
}
F2Matrix<MAX_N> Mat(n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Mat[i][j] = mat[ord[i]][j];
}
}
rank = 0;
for (int j = 0; j < n; j++) {
int pivot = -1;
for (int i = rank; i < n; i++) {
if (Mat[i][j]) {
pivot = i;
break;
}
}
assert(pivot != -1);
if (pivot != rank) swap(Mat[pivot], Mat[rank]);
for (int i = 0; i < n; i++) {
if (i != rank and Mat[i][j]) {
Mat[i] ^= Mat[rank];
w[i] ^= w[rank];
}
}
rank++;
}
cout << "!";
for (int i = 1; i < n; i++) cout << " " << w[i];
cout << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3872kb
input:
4 1 1 2 2 3 2 4
output:
YES ! 3 1 2 2 3 2 4
result:
wrong answer Token "!" doesn't correspond to pattern "?"