#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define int long long
using namespace std;
const int N = 5e5 + 5;
const int mod = 1e9+7;
const int inf = 1e18 + 10;
const int L = 20 * 20 * 20;
vector<int> in(N), adj[N];
vector<int> arr;
void dfs(int node, int par){
arr.pb(node);
for(auto itr: adj[node]){
if(itr != par){
dfs(itr, node);
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
string s;
cin>>s;
for(int i = 0; i < n-1; i++){
int a, b;
cin>>a>>b;
in[a]++;
in[b]++;
adj[a].pb(b);
adj[b].pb(a);
}
int uc = 0;
for(int i = 1; i <= n; i++){
if(in[i] == 1){
uc = i;
break;
}
}
dfs(uc, uc);
int first = n, last = 0;
vector<int> tree(n);
int ans = 0;
for(int i = 0; i < n; i++){
tree[i] = s[arr[i] - 1] - '0';
if(tree[i] == 0){
first = min(first, i);
last = max(last, i);
}
}
vector<int> nt;
for(int i = first; i <= last; i++){
nt.pb(tree[i]);
}
for(int i = 0; i < nt.size(); i++){
if(i == nt.size()-1 && tree[i] == 1) break;
if(i == nt.size()-1){
ans++;
break;
}
if(tree[i] == 0 && tree[i+1] == 0){
ans += 2;
i++;
}
else if(tree[i] == 1 && tree[i+1] == 1){
ans += 4;
i++;
}
else if(tree[i] == 0 && tree[i+1] == 1){
ans++;
}
else if(tree[i] == 1 && tree[i+1] == 0){
tree[i] = 1, tree[i+1] = 1;
ans += 3;
}
}
cout<<ans<<endl;
return 0;
}
Compilation message
svjetlo.cpp: In function 'int32_t main()':
svjetlo.cpp:64:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i = 0; i < nt.size(); i++){
| ~~^~~~~~~~~~~
svjetlo.cpp:65:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | if(i == nt.size()-1 && tree[i] == 1) break;
| ~~^~~~~~~~~~~~~~
svjetlo.cpp:66:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | if(i == nt.size()-1){
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
15964 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
123 ms |
53716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
186 ms |
45528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
15964 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |