This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define sz(a) (int)a.size()
#define ll long long
#define int long long
#define ld long double
const int N = 5e5 + 100;
int dp[N][2][2];
int n;
vector <int> g[N];
string s;
void dfs(int v, int p = -1)
{
int sum = 0;
bool fl = (s[v] == '0' ? 1 : 0);
for(auto to : g[v])
{
if(to == p)
{
continue;
}
dfs(to, v);
if(dp[to][0][0] != 1e9)
{
sum += dp[to][0][0] + (dp[to][0][0] == 0 ? 0 : 3);
}
else
{
sum += dp[to][0][1] + 1;
fl = !fl;
}
}
if(sum || s[v] == '0')
{
sum++;
}
dp[v][0][fl] = sum;
}
int dfs1(int v, int p = -1, int g1 = 0, int g2 = 0)
{
int ret = 1e9;
int sum = 0;
bool fl = 0;
if(dp[v][0][0] != 1e9) fl = 0;
else fl = 1;
sum = dp[v][0][fl];
for(auto to : g[v])
{
if(to == p)
{
continue;
}
int new_sum = sum;
bool new_fl = fl;
if(dp[to][0][0] != 1e9)
{
new_sum -= dp[to][0][0] + (dp[to][0][0] == 0 ? 0 : 3);
}
else
{
new_sum -= dp[to][0][1] + 1;
new_fl = !new_fl;
}
if(new_sum == 1 && !new_fl) new_sum = 0;
int new_sum1 = new_sum;
if(g1 && !new_sum1) new_sum1++;
new_sum1 += g1;
if(g2) new_sum1++;
else if(g1) new_sum1 += 3;
ret = min(ret, dfs1(to, v, new_sum1, g2 ^ new_fl));
if(new_fl == 0)
{
if(new_sum == 0)
{
new_sum = 1;
}
dp[v][1][0] = min(dp[v][1][0], new_sum + dp[to][1][1]);
dp[v][1][1] = min(dp[v][1][1], new_sum + dp[to][1][0] + 2);
}
else
{
dp[v][1][0] = min(dp[v][1][0], new_sum + dp[to][1][0] + 2);
dp[v][1][1] = min(dp[v][1][1], new_sum + dp[to][1][1]);
}
}
dp[v][1][fl] = min(dp[v][1][fl], max(1LL, dp[v][0][fl]));
// cout << dp[v][1][1] << " " << dp[v][1][0] << " ";
if(g1 == 0)
{
ret = min(ret, dp[v][1][1]);
}
else if(g2)
{
ret = min(ret, dp[v][1][0] + g1 + 1);
}
else
{
ret = min(ret, dp[v][1][1] + g1 + 3);
}
int fl1 = 0;
if(dp[v][0][1] != 1e9)
{
fl1 = 1;
}
int sum1 = dp[v][0][fl];
if(!sum1)
{
sum1 = 1;
}
sum1 += g1;
if(g2)
{
sum1++;
fl1 ^= 1;
}
else if(g1)
{
sum1 += 3;
}
int mini[2];
mini[0] = mini[1] = 1e9;
for(auto to : g[v])
{
if(to == p)
{
continue;
}
int fl2 = 0;
if(dp[to][0][1] != 1e9)
{
fl2 = 1;
}
int s1 = dp[to][0][fl2];
if(fl2)
{
s1++;
}
else if(s1)
{
s1 += 3;
}
int new_sum = sum1 - s1;
int new_fl = fl2 ^ fl1;
ret = min(ret, dp[to][1][0] + 2 + new_sum + mini[new_fl]);
ret = min(ret, dp[to][1][1] + new_sum + mini[!new_fl]);
if(!fl2)
{
mini[0] = min(mini[0], dp[to][1][1] - s1);
mini[1] = min(mini[1], dp[to][1][0] + 2 - s1);
}
else
{
mini[0] = min(mini[0], dp[to][1][0] + 2 - s1);
mini[1] = min(mini[1], dp[to][1][1] - s1);
}
}
// cout << ret << "\n";
return ret;
}
signed main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(0);
//cout.tie(0);
cin >> n;
cin >> s;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < 2; j++)
{
for(int t = 0; t < 2; t++)
{
dp[i][j][t] = 1e9;
}
}
}
for(int i = 0; i < n - 1; i++)
{
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(0);
cout << dfs1(0);
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... |