이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#define endl '\n'
#define db double
#define ll __int128
#define int long long
#define pb push_back
#define fs first
#define sd second
#define Mod long(1e9 + 7)
#define all(x) x.begin(), x.end()
#define unvisited long(-1)
#define Eps double(1e-9)
#define _for(i, n) for(int i = 0; i < (n); i++)
#define dbg(x) cout << #x ": " << x << endl;
const int Max = 1e6 + 7, Inf = 1e9 + 7;
void print(bool x) { cout << (x ? "YES" : "NO") << endl; }
string tostring (__int128 x)
{
string ans = "";
while(x > 0)
{
ans += (x % 10 + '0');
x /= 10;
}
reverse(all(ans));
return ans;
}
vector <vector<int>> v, dp;
vector <int> a;
void dfs(int node, int parent)
{
dp[node][a[node]] = 0;
dp[node][(a[node] ^ 1) + 2] = 1;
for(auto& u : v[node]) if(u != parent)
{
dfs(u, node);
vector <int> dp1(4, Inf);
dp1[0] = min(dp[u][0] + dp[node][0], dp[u][2] + dp[node][1]);
dp1[1] = min(dp[u][2] + dp[node][0], dp[u][0] + dp[node][1]);
dp1[2] = min(dp[u][3] + dp[node][3], dp[u][1] + dp[node][2]);
dp1[3] = min(dp[u][3] + dp[node][2], dp[u][1] + dp[node][3]);
swap(dp1, dp[node]);
}
}
void solve()
{
int n; cin >> n;
a.assign(n+1, 0);
v.assign(n+1, vector <int> ());
dp.assign(n+1, vector <int> (4, Inf));
_for(i, n-1){
int a, b; cin >> a >> b;
v[a].pb(b); v[b].pb(a);
}
_for(i, n) cin >> a[i+1];
dfs(1, 0);
int ans = min(dp[1][0], dp[1][2]);
if(ans >= Inf)
cout << "impossible" << endl;
else
cout << ans << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int Q = 1; //cin >> Q;
while (Q--)
{
solve();
}
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... |