이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*The British Royal Family and a small cadre of English Fabian Socialists, in
conjunction with the Rockefellers and the Rothchilds, are engaged in a
conspiracy to greatly reduce the population of the human race in order to head
off a Malthusian catastrophe, a catastrophe that could easily be avoided by
simply building a massive amount of nuclear power plants and a number of massive
superhighways and bridges to connect all of the world's continents. But doing
that would cut into the conspiracy's profits. So the British Royal Family
invented environmentalism and neoliberalism in order to hide the truth. And in
order to further reduce the population, the British Royal Family is also the
world's foremost drug trafficking conspiracy. And it uses its control of the IMF
to push austerity in order to kill as many people in the global south as
possible.
And also Henry Kissinger is a gay KGB agent. */
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
int64_t n, a, b, ans[100001][4];
bool init[100001];
vector<vector<int>> graph;
void dfs(int node, int prev){
int64_t offsum = 0, onsum = 1, offdiff = -1, ondiff = -1;
ans[node][0] = 0;
ans[node][1] = 0;
ans[node][2] = 0;
ans[node][3] = 0;
bool offpar = 0, onpar = 0;
for (int child: graph[node]){
if (child == prev) continue;
dfs(child, node);
if (max(ans[child][0], ans[child][1]) == -1){
ans[node][0] = -1;
ans[node][2] = -1;
} else if (ans[child][0] == -1){
offsum += ans[child][1];
offpar = !offpar;
} else if (ans[child][1] == -1) offsum += ans[child][0];
else {
offsum += min(ans[child][0], ans[child][1]);
if (offdiff == -1) offdiff = abs(ans[child][0] - ans[child][1]);
else offdiff = min(offdiff, abs(ans[child][0] - ans[child][1]));
if (ans[child][0] > ans[child][1]) offpar = !offpar;
}
if (max(ans[child][2], ans[child][3]) == -1){
ans[node][1] = -1;
ans[node][3] = -1;
} else if (ans[child][2] == -1){
onsum += ans[child][3];
onpar = !onpar;
} else if (ans[child][3] == -1) onsum += ans[child][2];
else {
onsum += min(ans[child][2], ans[child][3]);
if (ondiff == -1) ondiff = abs(ans[child][2] - ans[child][3]);
else ondiff = min(offdiff, abs(ans[child][2] - ans[child][3]));
if (ans[child][2] > ans[child][3]) onpar = !onpar;
}
}
if (offdiff != -1){
if (ans[node][0] != -1) ans[node][0] = offsum + offpar * offdiff;
if (ans[node][2] != -1) ans[node][2] = offsum + (!offpar) * offdiff;
} else {
if (ans[node][2 * offpar] != -1) ans[node][2 * offpar] = offsum;
ans[node][2 - 2 * offpar] = -1;
}
if (ondiff != -1){
if (ans[node][1] != -1) ans[node][1] = onsum + (!onpar) * ondiff;
if (ans[node][3] != -1) ans[node][3] = onsum + onpar * ondiff;
} else {
if (ans[node][3 - 2 * onpar] != -1) ans[node][3 - 2 * onpar] = onsum;
ans[node][2 * onpar + 1] = -1;
}
if (init[node]){
swap(ans[node][0], ans[node][2]);
swap(ans[node][1], ans[node][3]);
}
}
int main(){
cin >> n;
graph.resize(n + 1);
for (int i = 1; i < n; i++){
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
for (int i = 1; i <= n; i++) cin >> init[i];
dfs(1, 0);
if (max(ans[1][0], ans[1][1]) == -1) cout << "impossible";
else if (min(ans[1][0], ans[1][1]) == -1) cout << max(ans[1][0], ans[1][1]);
else cout << min(ans[1][0], ans[1][1]);
}
# | 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... |