#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for (auto& a : x)
#define sz(a) (int)(a).size()
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 200005;
int n;
string s;
vector<int> g[mxN];
bool ima[mxN];
void dfs1(int v, int p){
ima[v] = (s[v] == '1');
for(auto u : g[v]){
if(u != p){
dfs1(u, v);
ima[v] |= ima[u];
}
}
}
int rez = 0;
int sad = 0;
void dfs2(int v, int p){
int br = 0;
for(auto u : g[v])if(u != p)br += ima[u];
sad += br; sad -= (s[v] == '1'); rez = max(rez, sad);
for(auto u : g[v]){
if(u != p){
sad -= ima[u];
dfs2(u, v);
sad += ima[u];
}
}
sad -= br; sad += (s[v] == '1');
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
ff(i,1,n - 1){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
cin >> s; s = ' ' + s;
ff(root,1,n){
dfs1(root, -1);
dfs2(root, -1);
}
int br = 0;
ff(i,1,n)br += (s[i] == '1');
if(br >= 2)rez = max(rez, 2);
cout << rez << '\n';
return 0;
}
/*
6
2 3
4 3
1 3
3 5
6 2
110011
8
1 2
3 5
6 4
4 5
5 2
7 2
2 8
11111111
16
7 10
5 11
9 4
14 12
2 11
14 16
4 2
1 13
11 3
7 1
15 9
2 1
11 6
14 9
8 9
0111111001001110
// probati bojenje sahovski
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |