#include <bits/stdc++.h>
using namespace std;
vector <int> v[200005];
int din[200005][2],val[200005];
void dfs(int x,int tata)
{
int ok=0,i;
for (i=0;i<v[x].size();i++)
{
if (v[x][i]!=tata)
{
ok=1;
dfs(v[x][i],x);
}
}
if (ok==0)
{
if (val[x]==1)
{
din[x][1]=1;
din[x][0]=1;
}
}
else
{
if (v[x].size()==1)
{
din[x][1]=max(din[v[x][0]][0]+1,din[v[x][0]][1]+1);
din[x][0]=1;
}
else
{
if (val[x]==0)
{
for (i=0;i<v[x].size();i++)
{
din[x][0]=din[x][0]+max(din[v[x][i]][0],din[v[x][i]][1]);
}
}
else
{
int sum1=0,sum0=0;
for (i=0;i<v[x].size();i++)
{
int nod=v[x][i];
sum1=sum1+max(din[nod][1],din[nod][0]);
}
din[x][1]=sum1-1;
din[x][0]=1;
}
}
}
}
int n,i,x,y;
char ch;
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0);
#ifdef HOME
ifstream cin("date.in");
ofstream cout("date.out");
#endif // HOME
cin>>n;
for (i=1;i<=n-1;i++)
{
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for (i=1;i<=n;i++)
{
cin>>ch;
val[i]=ch-'0';
}
dfs(1,0);
/*for (i=1;i<=n;i++)
{
cout<<i<<" "<<din[i][0]<<" "<<din[i][1]<<'\n';
}*/
cout<<max(din[1][0],din[1][1]);
return 0;
}
Compilation message
power.cpp: In function 'void dfs(int, int)':
power.cpp:9:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | for (i=0;i<v[x].size();i++)
| ~^~~~~~~~~~~~
power.cpp:36:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (i=0;i<v[x].size();i++)
| ~^~~~~~~~~~~~
power.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (i=0;i<v[x].size();i++)
| ~^~~~~~~~~~~~
power.cpp:43:24: warning: unused variable 'sum0' [-Wunused-variable]
43 | int sum1=0,sum0=0;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4964 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4964 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4964 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |