이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x << " = " << x << endl;
# define pll pair <ll, ll>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 1e5 + 25;
const ll inf = 2e9 + 0;
const ll mod = 1e9 + 123;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
ll n, col[maxn];
vector <ll> g[maxn];
ll dp[maxn][2][2];
void dfs (ll v, ll pa)
{
ll tmp[2][2];
tmp[0][0] = 0;
tmp[1][0] = 0;
tmp[0][1] = inf;
tmp[1][1] = inf;
for (ll to : g[v])
{
if (to == pa) continue;
dfs(to, v);
ll t[2][2];
t[0][0] = min(tmp[0][1] + dp[to][0][1], tmp[0][0] + dp[to][0][0]);
t[0][1] = min(tmp[0][0] + dp[to][0][1], tmp[0][1] + dp[to][0][0]);
t[1][0] = min(tmp[1][1] + dp[to][1][1], tmp[1][0] + dp[to][1][0]);
t[1][1] = min(tmp[1][0] + dp[to][1][1], tmp[1][1] + dp[to][1][0]);
for (ll i = 0; i < 2; ++i)
{
for (ll j = 0; j < 2; ++j)
{
tmp[i][j] = t[i][j];
}
}
}
if (col[v])
{
dp[v][0][0] = tmp[0][1];
dp[v][0][1] = 1 + tmp[1][0];
dp[v][1][0] = tmp[0][0];
dp[v][1][1] = 1 + tmp[1][1];
}
else
{
dp[v][0][0] = tmp[0][0];
dp[v][0][1] = 1 + tmp[1][1];
dp[v][1][0] = tmp[0][1];
dp[v][1][1] = 1 + tmp[1][0];
}
}
void ma1n (/* SABR */)
{
cin >> n;
for (ll i = 1, u, v; i < n; ++i)
{
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for (ll i = 1; i <= n; ++i)
{
cin >> col[i];
}
dfs(1, 1);
ll ans = min({dp[1][0][1], dp[1][0][0]});
if (ans >= inf) cout << "impossible" << nl;
else cout << ans << nl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("angry.in", "r", stdin);
// freopen("angry.out", "w", stdout);
int ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; ++test)
{
// cout << "Case " << test << ":" << ' ';
ma1n();
}
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... |