이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
#define lg long long
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const lg N = 1e5+5;
vector<lg> adj[N];
lg c[N];
lg dp[N][2][2];
lg n;
lg dfs(lg src, lg ef, lg cur, lg par = - 1)
{
auto&ret = dp[src][ef][cur];
if(~ret) return ret;
ret = n+2;
lg sz = adj[src].size();
vector<vector<lg>> dp2(sz+1, vector<lg> (2, n+2));
dp2[0][0] = 0;
for(int i = 1; i <= sz; i++)
{
if(adj[src][i-1] == par)
{
dp2[i][0] = dp2[i-1][0];
dp2[i][1] = dp2[i-1][1];
continue;
}
dp2[i][0] = min(dp2[i-1][0]+dfs(adj[src][i-1], 0, ef^c[adj[src][i-1]], src), dp2[i-1][1]+dfs(adj[src][i-1], 1, ef^1^c[adj[src][i-1]], src)+1);
dp2[i][1] = min(dp2[i-1][1]+dfs(adj[src][i-1], 0, ef^c[adj[src][i-1]], src), dp2[i-1][0]+dfs(adj[src][i-1], 1, ef^1^c[adj[src][i-1]], src)+1);
}
ret = dp2[sz][cur];
return ret;
}
int main()
{
fastio;
cin >> n;
for(int i = 1; i < n; i++)
{
lg u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 1; i <= n; i++) cin >> c[i];
memset(dp, -1, sizeof(dp));
lg ans = min(dfs(1, 0, c[1]), dfs(1, 1, c[1]^1)+1);
if(ans > n)
{
cout << "impossible\n";
return 0;
}
cout << ans << '\n';
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... |