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 int long long
const int INF = 1e9 + 42;
vector<vector<int> > nxt;
vector<vector<int>> filsDe;
vector<int> pereDe;
vector<vector<vector<int > > > dp;
vector<int > initState;
int occ = 0;
void dfsInit(int nde,int prc )
{
pereDe[nde] = prc;
for(int iV: nxt[nde])
{
if(iV != prc)
{
filsDe[nde].push_back(iV);
dfsInit(iV, nde);
}
}
}
int fDP(int isParentT, int isMeT, int nde)
{
if(dp[isParentT][isMeT][nde] != -1)
{
return dp[isParentT][isMeT][nde];
}
int nbFils = filsDe[nde].size();
int totT = isParentT + isMeT + initState[nde];
int ans = INF;
int sumNoToggle = 0;
vector<int > diff(nbFils);
vector<int > diffCumul(nbFils+1);
for(int iF = 0; iF < nbFils; iF++)
{
int costToggled = fDP(isMeT, 1, filsDe[nde][iF]) + 1;
int costNoToggled = fDP(isMeT, 0, filsDe[nde][iF]);
sumNoToggle += costNoToggled;
diff[iF] = costToggled - costNoToggled;
}
sort(diff.begin(), diff.end());
diffCumul[0] = 0;
for(int i=1; i<= nbFils; i++)
{
diffCumul[i] = diffCumul[i-1]+ diff[i-1];
}
if(totT %2 == 0)
{//We need to toggle an even nb of children.
for(int i = 0; i<= nbFils; i+=2)
{
ans = min(ans, sumNoToggle + diffCumul[i]);
}
}
else
{
//We need to toggle an odd nb of children.
for(int i = 1; i<= nbFils; i+=2)
{
ans = min(ans, sumNoToggle + diffCumul[i]);
}
}
if(ans >= INF)
ans = INF;
dp[isParentT][isMeT][nde] = ans;
//printf("%lld\n", occ++);
return dp[isParentT][isMeT][nde];
}
signed main()
{
int N;
scanf("%lld", &N);
int nbArretes = N-1;
nxt.resize(N+1);
filsDe.resize(N+1);
pereDe.resize(N+1);
for(int iA = 0; iA < nbArretes; iA++)
{
int u, v;
scanf("%lld%lld", &u, &v);
nxt[u].push_back(v);
nxt[v].push_back(u);
}
int root = 1;
dfsInit(root, root);
initState.resize(N+1);
for(int iNde = 1; iNde <=N; iNde++)
{
scanf("%lld", &initState[iNde]);
}
dp.resize(2, vector<vector<int>>(2, vector<int>(N+1, -1)));
int ans = fDP(0, 0, root);
ans = min(ans, fDP(0, 1, root) + 1);
if(ans >= INF)
printf("impossible\n");
else
printf("%lld", ans);
}
Compilation message (stderr)
xanadu.cpp: In function 'int main()':
xanadu.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | scanf("%lld", &N);
| ~~~~~^~~~~~~~~~~~
xanadu.cpp:88:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | scanf("%lld%lld", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
xanadu.cpp:98:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | scanf("%lld", &initState[iNde]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |