이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<int>> adj(n);
int a, b;
for (int i = 0; i < n - 1; ++i) {
cin >> a >> b;
--a; --b; // Convert to zero-based index
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<bool> state(n);
for (int i = 0; i < n; ++i) {
int temp;
cin >> temp;
state[i] = (temp == 1);
}
bool found = false;
for (int i = 0; i < n; ++i) {
queue<pair<int, vector<bool>>> q;
q.push(make_pair(i, state));
int level = 0;
while (!q.empty() && level < 20) {
int q_size = q.size();
for (int j = 0; j < q_size; ++j) {
auto front = q.front();
q.pop();
int node = front.first;
vector<bool> current_state = front.second;
if (all_of(current_state.begin(), current_state.end(), [](bool v) { return !v; })) {
cout << level << endl;
return 0;
}
current_state[node] = !current_state[node];
for (int neighbor : adj[node]) {
current_state[neighbor] = !current_state[neighbor];
}
for (int k = 0; k < n; ++k) {
if (k != node) {
q.push(make_pair(k, current_state));
}
}
}
++level;
}
}
if (!found) {
cout << "IMPOSSIBLE" << endl;
}
return 0;
}
# | 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... |