#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
const ll N = 2e5 + 50;
const ll Mod = 1e18;
ll n, dp[N][2];
string s;
vector<int> adj[N];
void dfs(int v, int p = 0){
for(int u: adj[v]){
if(u == p) continue;
dfs(u, v);
dp[v][1] += dp[u][1];
dp[v][0] = max(dp[v][0], dp[u][1]);
}
if(s[v] == '1'){
dp[v][1]--;
dp[v][0] = max(dp[v][1], dp[v][0] + 1);
dp[v][1] = max(dp[v][1], 1ll);
}
else{
dp[v][0] = dp[v][1];
}
//cout << dp[v][1] << sep << dp[v][0] << endl;
}
int main(){
fast_io;
cin >> n;
int u, v;
for(int i = 1; i < n; i++){
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
cin >> s; s = "$" + s;
dfs(1);
ll res = 0;
for(int i = 1; i <= n; i++) res = max(res, dp[i][0]);
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6744 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6744 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6744 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6744 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6744 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6744 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
3 ms |
6744 KB |
Output is correct |
23 |
Correct |
3 ms |
7000 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
6848 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
3 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
3 ms |
7004 KB |
Output is correct |
30 |
Correct |
3 ms |
6744 KB |
Output is correct |
31 |
Correct |
3 ms |
6748 KB |
Output is correct |
32 |
Correct |
2 ms |
6880 KB |
Output is correct |
33 |
Correct |
3 ms |
6748 KB |
Output is correct |
34 |
Correct |
2 ms |
6816 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
3 ms |
6808 KB |
Output is correct |
37 |
Correct |
2 ms |
6944 KB |
Output is correct |
38 |
Correct |
4 ms |
6744 KB |
Output is correct |
39 |
Correct |
2 ms |
6748 KB |
Output is correct |
40 |
Correct |
3 ms |
6748 KB |
Output is correct |
41 |
Correct |
2 ms |
6748 KB |
Output is correct |
42 |
Correct |
2 ms |
6744 KB |
Output is correct |
43 |
Correct |
3 ms |
6748 KB |
Output is correct |
44 |
Correct |
3 ms |
6748 KB |
Output is correct |
45 |
Correct |
2 ms |
6808 KB |
Output is correct |
46 |
Correct |
3 ms |
6748 KB |
Output is correct |
47 |
Correct |
2 ms |
6748 KB |
Output is correct |
48 |
Correct |
2 ms |
6748 KB |
Output is correct |
49 |
Correct |
4 ms |
6996 KB |
Output is correct |
50 |
Correct |
2 ms |
6748 KB |
Output is correct |
51 |
Correct |
2 ms |
6748 KB |
Output is correct |
52 |
Correct |
2 ms |
6844 KB |
Output is correct |
53 |
Correct |
2 ms |
6744 KB |
Output is correct |
54 |
Correct |
2 ms |
7064 KB |
Output is correct |
55 |
Correct |
2 ms |
6748 KB |
Output is correct |
56 |
Correct |
2 ms |
6748 KB |
Output is correct |
57 |
Correct |
3 ms |
7000 KB |
Output is correct |
58 |
Correct |
5 ms |
7004 KB |
Output is correct |
59 |
Correct |
2 ms |
6808 KB |
Output is correct |
60 |
Correct |
2 ms |
6748 KB |
Output is correct |
61 |
Correct |
3 ms |
6748 KB |
Output is correct |
62 |
Correct |
2 ms |
7004 KB |
Output is correct |
63 |
Correct |
3 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6744 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6744 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6744 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
3 ms |
6744 KB |
Output is correct |
23 |
Correct |
3 ms |
7000 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
6848 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
3 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
3 ms |
7004 KB |
Output is correct |
30 |
Correct |
3 ms |
6744 KB |
Output is correct |
31 |
Correct |
3 ms |
6748 KB |
Output is correct |
32 |
Correct |
2 ms |
6880 KB |
Output is correct |
33 |
Correct |
3 ms |
6748 KB |
Output is correct |
34 |
Correct |
2 ms |
6816 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
3 ms |
6808 KB |
Output is correct |
37 |
Correct |
2 ms |
6944 KB |
Output is correct |
38 |
Correct |
4 ms |
6744 KB |
Output is correct |
39 |
Correct |
2 ms |
6748 KB |
Output is correct |
40 |
Correct |
3 ms |
6748 KB |
Output is correct |
41 |
Correct |
2 ms |
6748 KB |
Output is correct |
42 |
Correct |
2 ms |
6744 KB |
Output is correct |
43 |
Correct |
3 ms |
6748 KB |
Output is correct |
44 |
Correct |
3 ms |
6748 KB |
Output is correct |
45 |
Correct |
2 ms |
6808 KB |
Output is correct |
46 |
Correct |
3 ms |
6748 KB |
Output is correct |
47 |
Correct |
2 ms |
6748 KB |
Output is correct |
48 |
Correct |
2 ms |
6748 KB |
Output is correct |
49 |
Correct |
4 ms |
6996 KB |
Output is correct |
50 |
Correct |
2 ms |
6748 KB |
Output is correct |
51 |
Correct |
2 ms |
6748 KB |
Output is correct |
52 |
Correct |
2 ms |
6844 KB |
Output is correct |
53 |
Correct |
2 ms |
6744 KB |
Output is correct |
54 |
Correct |
2 ms |
7064 KB |
Output is correct |
55 |
Correct |
2 ms |
6748 KB |
Output is correct |
56 |
Correct |
2 ms |
6748 KB |
Output is correct |
57 |
Correct |
3 ms |
7000 KB |
Output is correct |
58 |
Correct |
5 ms |
7004 KB |
Output is correct |
59 |
Correct |
2 ms |
6808 KB |
Output is correct |
60 |
Correct |
2 ms |
6748 KB |
Output is correct |
61 |
Correct |
3 ms |
6748 KB |
Output is correct |
62 |
Correct |
2 ms |
7004 KB |
Output is correct |
63 |
Correct |
3 ms |
6748 KB |
Output is correct |
64 |
Correct |
122 ms |
17616 KB |
Output is correct |
65 |
Correct |
113 ms |
17612 KB |
Output is correct |
66 |
Correct |
84 ms |
17596 KB |
Output is correct |
67 |
Correct |
74 ms |
17616 KB |
Output is correct |
68 |
Correct |
71 ms |
17592 KB |
Output is correct |
69 |
Correct |
112 ms |
17688 KB |
Output is correct |
70 |
Correct |
72 ms |
17568 KB |
Output is correct |
71 |
Correct |
75 ms |
17628 KB |
Output is correct |
72 |
Correct |
127 ms |
17508 KB |
Output is correct |
73 |
Correct |
110 ms |
32624 KB |
Output is correct |
74 |
Correct |
129 ms |
32748 KB |
Output is correct |
75 |
Correct |
74 ms |
17356 KB |
Output is correct |
76 |
Correct |
71 ms |
17364 KB |
Output is correct |
77 |
Correct |
78 ms |
17488 KB |
Output is correct |
78 |
Correct |
70 ms |
17428 KB |
Output is correct |
79 |
Correct |
80 ms |
17632 KB |
Output is correct |
80 |
Correct |
82 ms |
17408 KB |
Output is correct |
81 |
Correct |
75 ms |
17952 KB |
Output is correct |
82 |
Correct |
104 ms |
17528 KB |
Output is correct |
83 |
Correct |
115 ms |
23824 KB |
Output is correct |
84 |
Correct |
94 ms |
26156 KB |
Output is correct |
85 |
Correct |
114 ms |
25860 KB |
Output is correct |
86 |
Correct |
79 ms |
19280 KB |
Output is correct |
87 |
Correct |
92 ms |
18576 KB |
Output is correct |
88 |
Correct |
77 ms |
18952 KB |
Output is correct |
89 |
Correct |
94 ms |
18992 KB |
Output is correct |
90 |
Correct |
79 ms |
18736 KB |
Output is correct |
91 |
Correct |
77 ms |
18860 KB |
Output is correct |
92 |
Correct |
97 ms |
19452 KB |
Output is correct |
93 |
Correct |
89 ms |
22360 KB |
Output is correct |
94 |
Correct |
91 ms |
24260 KB |
Output is correct |
95 |
Correct |
111 ms |
22796 KB |
Output is correct |
96 |
Correct |
88 ms |
20652 KB |
Output is correct |
97 |
Correct |
157 ms |
18852 KB |
Output is correct |
98 |
Correct |
138 ms |
18252 KB |
Output is correct |
99 |
Correct |
114 ms |
18012 KB |
Output is correct |
100 |
Correct |
168 ms |
21796 KB |
Output is correct |
101 |
Correct |
96 ms |
19340 KB |
Output is correct |
102 |
Correct |
162 ms |
18332 KB |
Output is correct |
103 |
Correct |
136 ms |
18208 KB |
Output is correct |
104 |
Correct |
55 ms |
18372 KB |
Output is correct |
105 |
Correct |
114 ms |
28876 KB |
Output is correct |
106 |
Correct |
59 ms |
18180 KB |
Output is correct |
107 |
Correct |
62 ms |
18304 KB |
Output is correct |
108 |
Correct |
117 ms |
33740 KB |
Output is correct |
109 |
Correct |
66 ms |
18324 KB |
Output is correct |
110 |
Correct |
172 ms |
33952 KB |
Output is correct |
111 |
Correct |
122 ms |
17480 KB |
Output is correct |