#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<str> vstr;
#define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
#define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in)
#define REP(i,b) FOR(i,0,b,1)
#define REPD(i,b) FORD(i,b,0,1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define rsz resize
#define MANY_TESTS int tcase; cin >> tcase; while(tcase--)
const double EPS = 1e-9;
const int MOD = 1e9+7; // 998244353;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};
#ifdef DEBUG
#define DBG if(1)
#else
#define DBG if(0)
#endif
#define dbg(x) cout << "(" << #x << " : " << x << ")" << endl;
// ostreams
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "[" << par.first << ";" << par.second << "]"; return out;}
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template<class T>
ostream& operator<<(ostream& out, const vector<T> &v){ out << "["; REP(i, v.size()) out << v[i] << ", "; out << "]"; return out;}
// istreams
template<class T>
istream& operator>>(istream& in, vector<T> &v){ for(auto &x : v) in >> x; return in; }
template<class T, class U>
istream& operator>>(istream& in, pair<T, U> &p){ in >> p.ff >> p.ss; return in; }
//searches
template<typename T, typename U>
T bsl(T lo, T hi, U f){ hi++; T mid; while(lo < hi){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid+1; } return lo; }
template<typename U>
double bsld(double lo, double hi, U f, double p = 1e-9){ int r = 3 + (int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid; } return (lo + hi)/2; }
template<typename T, typename U>
T bsh(T lo, T hi, U f){ lo--; T mid; while(lo < hi){ mid = (lo + hi + 1)/2; f(mid) ? lo = mid : hi = mid-1; } return lo; }
template<typename U>
double bshd(double lo, double hi, U f, double p = 1e-9){ int r = 3+(int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? lo = mid : hi = mid; } return (lo + hi)/2; }
// some more utility functions
template<typename T>
pair<T, int> get_min(vector<T> &v){ typename vector<T> :: iterator it = min_element(v.begin(), v.end()); return mp(*it, it - v.begin());}
template<typename T>
pair<T, int> get_max(vector<T> &v){ typename vector<T> :: iterator it = max_element(v.begin(), v.end()); return mp(*it, it - v.begin());}
template<typename T> bool ckmin(T& a, const T& b){return b < a ? a = b , true : false;}
template<typename T> bool ckmax(T& a, const T& b){return b > a ? a = b, true : false;}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n; cin >> n;
vector<vi> g(n);
REP(i, n - 1){
int u, v;
cin >> u >> v;
u--; v--;
g[u].pb(v);
g[v].pb(u);
}
string S; cin >> S;
vector<vi> dp(n, vi(3, 0));
function<void(int, int)> dfs = [&](int v, int p){
for(int x : g[v]){
if(x == p) continue;
dfs(x, v);
}
int s0 = 0;
for(int x : g[v]) s0 += dp[x][0];
for(int x : g[v]){
if(x == p) continue;
dp[v][2] = max(dp[v][2], dp[x][2] + s0 - dp[x][0]);
}
if(S[v] == '1'){
for(int x : g[v]){
if(x == p) continue;
dp[v][2] = max(dp[v][2], 1 + dp[x][1] + s0 - dp[x][0]);
}
}
for(int x : g[v]){
if(x == p) continue;
dp[v][1] += max(dp[x][1], dp[x][0]);
}
if(S[v] == '1') dp[v][1]--;
if(S[v] == '1')
dp[v][1] = max(dp[v][1], s0 + 1);
dp[v][0] = s0;
};
dfs(0, -1);
cout << max(dp[0][0], max(dp[0][1], dp[0][2])) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
2 ms |
620 KB |
Output is correct |
25 |
Correct |
2 ms |
620 KB |
Output is correct |
26 |
Correct |
2 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
620 KB |
Output is correct |
29 |
Correct |
2 ms |
748 KB |
Output is correct |
30 |
Correct |
2 ms |
620 KB |
Output is correct |
31 |
Correct |
1 ms |
620 KB |
Output is correct |
32 |
Correct |
2 ms |
620 KB |
Output is correct |
33 |
Correct |
1 ms |
620 KB |
Output is correct |
34 |
Correct |
2 ms |
620 KB |
Output is correct |
35 |
Correct |
1 ms |
620 KB |
Output is correct |
36 |
Correct |
1 ms |
620 KB |
Output is correct |
37 |
Correct |
2 ms |
620 KB |
Output is correct |
38 |
Correct |
1 ms |
620 KB |
Output is correct |
39 |
Correct |
1 ms |
620 KB |
Output is correct |
40 |
Correct |
2 ms |
748 KB |
Output is correct |
41 |
Correct |
2 ms |
620 KB |
Output is correct |
42 |
Correct |
2 ms |
620 KB |
Output is correct |
43 |
Correct |
2 ms |
620 KB |
Output is correct |
44 |
Correct |
2 ms |
620 KB |
Output is correct |
45 |
Correct |
1 ms |
620 KB |
Output is correct |
46 |
Correct |
1 ms |
620 KB |
Output is correct |
47 |
Correct |
2 ms |
640 KB |
Output is correct |
48 |
Correct |
1 ms |
620 KB |
Output is correct |
49 |
Correct |
2 ms |
620 KB |
Output is correct |
50 |
Correct |
2 ms |
620 KB |
Output is correct |
51 |
Correct |
1 ms |
620 KB |
Output is correct |
52 |
Correct |
2 ms |
620 KB |
Output is correct |
53 |
Correct |
2 ms |
620 KB |
Output is correct |
54 |
Correct |
2 ms |
620 KB |
Output is correct |
55 |
Correct |
2 ms |
620 KB |
Output is correct |
56 |
Correct |
1 ms |
620 KB |
Output is correct |
57 |
Correct |
2 ms |
748 KB |
Output is correct |
58 |
Correct |
1 ms |
620 KB |
Output is correct |
59 |
Correct |
1 ms |
620 KB |
Output is correct |
60 |
Correct |
1 ms |
620 KB |
Output is correct |
61 |
Correct |
1 ms |
620 KB |
Output is correct |
62 |
Correct |
2 ms |
764 KB |
Output is correct |
63 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
2 ms |
620 KB |
Output is correct |
25 |
Correct |
2 ms |
620 KB |
Output is correct |
26 |
Correct |
2 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
620 KB |
Output is correct |
29 |
Correct |
2 ms |
748 KB |
Output is correct |
30 |
Correct |
2 ms |
620 KB |
Output is correct |
31 |
Correct |
1 ms |
620 KB |
Output is correct |
32 |
Correct |
2 ms |
620 KB |
Output is correct |
33 |
Correct |
1 ms |
620 KB |
Output is correct |
34 |
Correct |
2 ms |
620 KB |
Output is correct |
35 |
Correct |
1 ms |
620 KB |
Output is correct |
36 |
Correct |
1 ms |
620 KB |
Output is correct |
37 |
Correct |
2 ms |
620 KB |
Output is correct |
38 |
Correct |
1 ms |
620 KB |
Output is correct |
39 |
Correct |
1 ms |
620 KB |
Output is correct |
40 |
Correct |
2 ms |
748 KB |
Output is correct |
41 |
Correct |
2 ms |
620 KB |
Output is correct |
42 |
Correct |
2 ms |
620 KB |
Output is correct |
43 |
Correct |
2 ms |
620 KB |
Output is correct |
44 |
Correct |
2 ms |
620 KB |
Output is correct |
45 |
Correct |
1 ms |
620 KB |
Output is correct |
46 |
Correct |
1 ms |
620 KB |
Output is correct |
47 |
Correct |
2 ms |
640 KB |
Output is correct |
48 |
Correct |
1 ms |
620 KB |
Output is correct |
49 |
Correct |
2 ms |
620 KB |
Output is correct |
50 |
Correct |
2 ms |
620 KB |
Output is correct |
51 |
Correct |
1 ms |
620 KB |
Output is correct |
52 |
Correct |
2 ms |
620 KB |
Output is correct |
53 |
Correct |
2 ms |
620 KB |
Output is correct |
54 |
Correct |
2 ms |
620 KB |
Output is correct |
55 |
Correct |
2 ms |
620 KB |
Output is correct |
56 |
Correct |
1 ms |
620 KB |
Output is correct |
57 |
Correct |
2 ms |
748 KB |
Output is correct |
58 |
Correct |
1 ms |
620 KB |
Output is correct |
59 |
Correct |
1 ms |
620 KB |
Output is correct |
60 |
Correct |
1 ms |
620 KB |
Output is correct |
61 |
Correct |
1 ms |
620 KB |
Output is correct |
62 |
Correct |
2 ms |
764 KB |
Output is correct |
63 |
Correct |
1 ms |
620 KB |
Output is correct |
64 |
Correct |
211 ms |
25264 KB |
Output is correct |
65 |
Correct |
210 ms |
25260 KB |
Output is correct |
66 |
Correct |
198 ms |
25368 KB |
Output is correct |
67 |
Correct |
204 ms |
25388 KB |
Output is correct |
68 |
Correct |
211 ms |
25368 KB |
Output is correct |
69 |
Correct |
212 ms |
25372 KB |
Output is correct |
70 |
Correct |
211 ms |
25276 KB |
Output is correct |
71 |
Correct |
203 ms |
25292 KB |
Output is correct |
72 |
Correct |
193 ms |
25384 KB |
Output is correct |
73 |
Correct |
256 ms |
40492 KB |
Output is correct |
74 |
Correct |
243 ms |
40384 KB |
Output is correct |
75 |
Correct |
197 ms |
25132 KB |
Output is correct |
76 |
Correct |
235 ms |
25268 KB |
Output is correct |
77 |
Correct |
212 ms |
25128 KB |
Output is correct |
78 |
Correct |
236 ms |
25388 KB |
Output is correct |
79 |
Correct |
227 ms |
25140 KB |
Output is correct |
80 |
Correct |
223 ms |
25132 KB |
Output is correct |
81 |
Correct |
192 ms |
25572 KB |
Output is correct |
82 |
Correct |
198 ms |
25324 KB |
Output is correct |
83 |
Correct |
227 ms |
31408 KB |
Output is correct |
84 |
Correct |
238 ms |
34128 KB |
Output is correct |
85 |
Correct |
274 ms |
33592 KB |
Output is correct |
86 |
Correct |
198 ms |
27052 KB |
Output is correct |
87 |
Correct |
203 ms |
26152 KB |
Output is correct |
88 |
Correct |
219 ms |
26600 KB |
Output is correct |
89 |
Correct |
196 ms |
26680 KB |
Output is correct |
90 |
Correct |
216 ms |
26380 KB |
Output is correct |
91 |
Correct |
216 ms |
26640 KB |
Output is correct |
92 |
Correct |
206 ms |
27032 KB |
Output is correct |
93 |
Correct |
211 ms |
29948 KB |
Output is correct |
94 |
Correct |
277 ms |
31992 KB |
Output is correct |
95 |
Correct |
191 ms |
30436 KB |
Output is correct |
96 |
Correct |
214 ms |
28440 KB |
Output is correct |
97 |
Correct |
199 ms |
26500 KB |
Output is correct |
98 |
Correct |
199 ms |
25948 KB |
Output is correct |
99 |
Correct |
214 ms |
25640 KB |
Output is correct |
100 |
Correct |
220 ms |
29464 KB |
Output is correct |
101 |
Correct |
226 ms |
27048 KB |
Output is correct |
102 |
Correct |
205 ms |
26108 KB |
Output is correct |
103 |
Correct |
191 ms |
25804 KB |
Output is correct |
104 |
Correct |
153 ms |
26212 KB |
Output is correct |
105 |
Correct |
242 ms |
36656 KB |
Output is correct |
106 |
Correct |
139 ms |
25824 KB |
Output is correct |
107 |
Correct |
140 ms |
25824 KB |
Output is correct |
108 |
Correct |
259 ms |
41404 KB |
Output is correct |
109 |
Correct |
147 ms |
26080 KB |
Output is correct |
110 |
Correct |
222 ms |
41772 KB |
Output is correct |
111 |
Correct |
215 ms |
25404 KB |
Output is correct |