#include <iostream>
#include <vector>
#include <queue>
#include <deque>
using namespace std;
int N;
vector<int> edge[200001];
vector<int> plant(200000);
vector<int> edge_count(200001, 0);
vector<int> stream_count(200001, 0);
vector<int> visit(200001, 0);
int res = 0;
int main()
{
cin >> N;
int a, b;
for(int i = 1; i <= N-1; i++)
{
cin >> a >> b;
edge[a].push_back(b);
edge_count[a]++;
edge[b].push_back(a);
edge_count[b]++;
}
char c;
for(int i = 1; i <= N; i++)
{
cin >> c;
plant[i] = (c == '1');
}
deque<int> tbv1, tbv2;
for(int i = 1; i <= N; i++) if(edge_count[i] == 1)
{
if(plant[i]) tbv1.push_front(i);
else tbv1.push_back(i);
}
while(!(tbv1.empty() && tbv2.empty()))
{
a = tbv1.front();
tbv1.pop_front();
if(visit[a]) continue;
visit[a] = 1;
for(int v: edge[a])
{
if(!visit[v])
{
edge_count[v]--;
if(edge_count[v] <= 1)
{
if(plant[v]) tbv2.push_front(v);
else tbv2.push_back(v);
}
}
else
{
stream_count[a] += stream_count[v];
}
}
if(stream_count[a] > 1 && plant[a]) res--;
if(stream_count[a] == 0 && plant[a])
{
stream_count[a]++;
res++;
}
if(tbv1.empty()) swap(tbv1, tbv2);
}
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
8172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
8172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
8172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |