이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
// author: aykhn
using namespace std;
#define int long long
#define inf 0x3F3F3F3F
const int MXN = 1e5 + 5;
int n;
vector<int> adj[MXN];
int val[MXN], dp[MXN][2][2];
// dp[i][j][k] = j by doing k ops and ok subtree
void dfs(int a, int p)
{
vector<int> v1;
int f = 0;
for (int v : adj[a])
{
if (v == p) continue;
f = 1;
dfs(v, a);
v1.push_back(v);
}
if (!f)
{
if (!val[a]) dp[a][0][0] = 0, dp[a][0][1] = inf, dp[a][1][0] = inf, dp[a][1][1] = 1;
else dp[a][0][0] = inf, dp[a][0][1] = 1, dp[a][1][0] = 0, dp[a][1][1] = inf;
return;
}
if (!val[a])
{
dp[a][0][0] = 0;
vector<int> nw;
for (int v : v1)
{
dp[a][0][0] += dp[v][0][0];
nw.push_back(dp[v][0][1] - dp[v][0][0]);
}
sort(nw.begin(), nw.end());
int s = 0, mn = 0;
for (int i = 0; i < nw.size(); i++)
{
s += nw[i];
if (i % 2 == 1) mn = min(mn, s);
}
dp[a][0][0] += mn;
//---
dp[a][0][1] = 1;
nw.clear();
for (int v : v1)
{
dp[a][0][1] += dp[v][1][0];
nw.push_back(dp[v][1][1] - dp[v][1][0]);
}
sort(nw.begin(), nw.end());
s = 0, mn = inf;
for (int i = 0; i < nw.size(); i++)
{
s += nw[i];
if (i % 2 == 0) mn = min(mn, s);
}
dp[a][0][1] += mn;
//---
dp[a][1][0] = 0;
nw.clear();
for (int v : v1)
{
dp[a][1][0] += dp[v][0][0];
nw.push_back(dp[v][0][1] - dp[v][0][0]);
}
sort(nw.begin(), nw.end());
s = 0, mn = inf;
for (int i = 0; i < nw.size(); i++)
{
s += nw[i];
if (i % 2 == 0) mn = min(mn, s);
}
dp[a][1][0] += mn;
//---
dp[a][1][1] = 1;
nw.clear();
for (int v : v1)
{
dp[a][1][1] += dp[v][1][0];
nw.push_back(dp[v][1][1] - dp[v][1][0]);
}
sort(nw.begin(), nw.end());
s = 0, mn = 0;
for (int i = 0; i < nw.size(); i++)
{
s += nw[i];
if (i % 2 == 1) mn = min(mn, s);
}
dp[a][1][1] += mn;
}
else
{
dp[a][0][0] = 0;
vector<int> nw;
for (int v : v1)
{
dp[a][0][0] += dp[v][0][0];
nw.push_back(dp[v][0][1] - dp[v][0][0]);
}
sort(nw.begin(), nw.end());
int s = 0, mn = inf;
for (int i = 0; i < nw.size(); i++)
{
s += nw[i];
if (i % 2 == 0) mn = min(mn, s);
}
dp[a][0][0] += mn;
//---
dp[a][0][1] = 1;
nw.clear();
for (int v : v1)
{
dp[a][0][1] += dp[v][1][0];
nw.push_back(dp[v][1][1] - dp[v][1][0]);
}
sort(nw.begin(), nw.end());
s = 0, mn = 0;
for (int i = 0; i < nw.size(); i++)
{
s += nw[i];
if (i % 2 == 1) mn = min(mn, s);
}
dp[a][0][1] += mn;
//---
dp[a][1][0] = 0;
nw.clear();
for (int v : v1)
{
dp[a][1][0] += dp[v][0][0];
nw.push_back(dp[v][0][1] - dp[v][0][0]);
}
sort(nw.begin(), nw.end());
s = 0, mn = 0;
for (int i = 0; i < nw.size(); i++)
{
s += nw[i];
if (i % 2 == 1) mn = min(mn, s);
}
dp[a][1][0] += mn;
//---
dp[a][1][1] = 1;
nw.clear();
for (int v : v1)
{
dp[a][1][1] += dp[v][1][0];
nw.push_back(dp[v][1][1] - dp[v][1][0]);
}
sort(nw.begin(), nw.end());
s = 0, mn = inf;
for (int i = 0; i < nw.size(); i++)
{
s += nw[i];
if (i % 2 == 0) mn = min(mn, s);
}
dp[a][1][1] += mn;
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) cin >> val[i];
dfs(1, 1);
if (dp[1][0][1] >= inf && dp[1][0][0] >= inf)
{
cout << "impossible\n";
return 0;
}
cout << min(dp[1][0][1], dp[1][0][0]) << '\n';
}
//
컴파일 시 표준 에러 (stderr) 메시지
xanadu.cpp: In function 'void dfs(long long int, long long int)':
xanadu.cpp:43:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int i = 0; i < nw.size(); i++)
| ~~^~~~~~~~~~~
xanadu.cpp:59:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i = 0; i < nw.size(); i++)
| ~~^~~~~~~~~~~
xanadu.cpp:75:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int i = 0; i < nw.size(); i++)
| ~~^~~~~~~~~~~
xanadu.cpp:91:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for (int i = 0; i < nw.size(); i++)
| ~~^~~~~~~~~~~
xanadu.cpp:109:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for (int i = 0; i < nw.size(); i++)
| ~~^~~~~~~~~~~
xanadu.cpp:125:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | for (int i = 0; i < nw.size(); i++)
| ~~^~~~~~~~~~~
xanadu.cpp:141:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | for (int i = 0; i < nw.size(); i++)
| ~~^~~~~~~~~~~
xanadu.cpp:157:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
157 | for (int i = 0; i < nw.size(); i++)
| ~~^~~~~~~~~~~
# | 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... |