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;
const long long MX=1e5+10;
vector < long long > mas[MX];
long long st[MX];
long long mn[MX][2][2];
long long n;
void DFS(long long zar,long long mun)
{
for(auto u:mas[zar])
{
if(u!=mun)
{
DFS(u,zar);
}
}
if(st[zar])
{
mn[zar][0][1]=0;
mn[zar][1][1]=n+1;
mn[zar][0][0]=n+1;
mn[zar][1][0]=1;
}
else
{
mn[zar][0][1]=n+1;
mn[zar][1][1]=1;
mn[zar][0][0]=0;
mn[zar][1][0]=n+1;
}
for(auto u:mas[zar])
{
if(u==mun)
{
continue;
}
long long zn00=mn[zar][0][0];
long long zn01=mn[zar][0][1];
long long zn10=mn[zar][1][0];
long long zn11=mn[zar][1][1];
mn[zar][0][0]=min({zn00+mn[u][0][0],zn01+mn[u][1][0],n+1});
mn[zar][0][1]=min({zn00+mn[u][1][0],zn01+mn[u][0][0],n+1});
mn[zar][1][0]=min({zn10+mn[u][0][1],zn11+mn[u][1][1],n+1});
mn[zar][1][1]=min({zn11+mn[u][0][1],zn10+mn[u][1][1],n+1});
}
return;
}
int main()
{
cin.tie(0);
ios_base::sync_with_stdio(0);
cin>>n;
long long a,b;
for(long long i=1;i<n;i++)
{
cin>>a>>b;
mas[a].push_back(b);
mas[b].push_back(a);
}
for(long long i=1;i<=n;i++)
{
cin>>st[i];
}
DFS(1,0);
if(min(mn[1][0][0],mn[1][1][0])>n)
{
cout<<"impossible\n";
}
else
{
cout<<min(mn[1][0][0],mn[1][1][0])<<"\n";
}
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... |