This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define X first
#define Y second
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T i, U ...j) {
cout << i << ' ', abc(j...);
}
template <typename T> void printv(T l, T r) {
for (; l != r; ++l)
cout << *l << " \n"[l + 1 == r];
}
#ifdef Doludu
#define test(x...) abc("[" + string(#x) + "]", x);
#else
#define test(x...) void(0);
#endif
const int N = 100000;
int main () {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector <vector <int>> dis(n, vector <int> (n, n + 87));
for (int i = 0, u, v; i < n - 1; ++i) {
cin >> u >> v, --u, --v;
dis[u][v] = dis[v][u] = 1;
}
for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
vector <int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
int tans = 1 << 30, best = 0;
for (int s = 0; s < 1 << n; ++s) {
bool fail = false;
for (int i = 0; i < n; ++i) {
if (a[i] != -1 && (s >> i & 1) != a[i]) {
fail = true;
}
}
if (fail)
continue;
for (int d = n - 1; ~d; --d) {
bool fail = false;
for (int i = 0; i < n; ++i) {
bool ok = false;
for (int j = 0; j < n; ++j) if (j ^ i && dis[i][j] <= d && (s >> j & 1) == (s >> i & 1)) {
ok = true;
}
if (!ok)
fail = true;
}
if (!fail && tans > d) {
tans = d, best = s;
}
}
}
if (tans == 1 << 30)
tans = -1;
cout << tans << endl;
if (tans != -1) {
for (int i = 0; i < n; ++i)
cout << (best >> i & 1) << ' ';
cout << endl;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |