#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 2e5 + 5;
const int oo = 1e18 + 7, mod = 1e9 + 7;
int n;
bool plant[N];
vector<int> Adj[N];
/*
Basically, there are three main cases:
- Not choosing subtree of u (including u)
- Just choosing subtree of u
- Not just choosing subtree of u -> every node that is parent of at least one node in subtree u is broken
*/
int dp[N][2];// cases above (ofc first case -> 0)
int par[N];
void dfs(int u, int p){
par[u] = p;
for(auto v : Adj[u]){
if(v == p) continue;
dfs(v, u);
}
int mx = 0;
for(auto v : Adj[u]){
if(v == p) continue;
dp[u][0] += max(dp[v][1], 0LL);// you can not choose anything in v
mx = max(mx, dp[v][1]);
}
dp[u][0] -= plant[u];
dp[u][0] = max(dp[u][0], mx + plant[u]);// just choosing one subtree of u => you can choose u
dp[u][1] = plant[u];// just choosing u
int sum = 0;
for(auto v : Adj[u]){
if(v == p) continue;
sum += max(0LL, dp[v][1]);
}
sum -= plant[u];
dp[u][1] = max(dp[u][1], sum);
}
void process(){
cin >> n;
for(int i = 1; i < n; i++){
int x, y;
cin >> x >> y;
Adj[x].pb(y);
Adj[y].pb(x);
}
string s;
cin >> s;
for(int i = 1; i <= n; i++) plant[i] = s[i - 1] - '0';
dfs(1, 1);
int mx = -1;
for(int i = 1; i <= n; i++) mx = max(mx, dp[i][0]);
cout << mx;
}
signed main(){
ios_base::sync_with_stdio(0);
process();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
3 ms |
5024 KB |
Output is correct |
7 |
Correct |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5024 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5028 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4944 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
4944 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
16 |
Correct |
3 ms |
4944 KB |
Output is correct |
17 |
Correct |
3 ms |
4944 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
3 ms |
4944 KB |
Output is correct |
20 |
Correct |
3 ms |
4944 KB |
Output is correct |
21 |
Correct |
3 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
3 ms |
5024 KB |
Output is correct |
7 |
Correct |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5024 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5028 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4944 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
4944 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
16 |
Correct |
3 ms |
4944 KB |
Output is correct |
17 |
Correct |
3 ms |
4944 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
3 ms |
4944 KB |
Output is correct |
20 |
Correct |
3 ms |
4944 KB |
Output is correct |
21 |
Correct |
3 ms |
4944 KB |
Output is correct |
22 |
Correct |
4 ms |
5092 KB |
Output is correct |
23 |
Correct |
4 ms |
5072 KB |
Output is correct |
24 |
Correct |
4 ms |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
4 ms |
5072 KB |
Output is correct |
27 |
Correct |
4 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5168 KB |
Output is correct |
29 |
Correct |
4 ms |
5200 KB |
Output is correct |
30 |
Correct |
3 ms |
5160 KB |
Output is correct |
31 |
Correct |
3 ms |
5160 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5172 KB |
Output is correct |
34 |
Correct |
4 ms |
5164 KB |
Output is correct |
35 |
Correct |
4 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5164 KB |
Output is correct |
37 |
Correct |
3 ms |
5072 KB |
Output is correct |
38 |
Correct |
4 ms |
5072 KB |
Output is correct |
39 |
Correct |
3 ms |
5200 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
5200 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
43 |
Correct |
4 ms |
5072 KB |
Output is correct |
44 |
Correct |
4 ms |
5072 KB |
Output is correct |
45 |
Correct |
3 ms |
5072 KB |
Output is correct |
46 |
Correct |
3 ms |
5072 KB |
Output is correct |
47 |
Correct |
4 ms |
5072 KB |
Output is correct |
48 |
Correct |
4 ms |
5160 KB |
Output is correct |
49 |
Correct |
4 ms |
5200 KB |
Output is correct |
50 |
Correct |
3 ms |
5072 KB |
Output is correct |
51 |
Correct |
4 ms |
5072 KB |
Output is correct |
52 |
Correct |
3 ms |
5164 KB |
Output is correct |
53 |
Correct |
3 ms |
5072 KB |
Output is correct |
54 |
Correct |
3 ms |
5072 KB |
Output is correct |
55 |
Correct |
4 ms |
5164 KB |
Output is correct |
56 |
Correct |
3 ms |
5072 KB |
Output is correct |
57 |
Correct |
4 ms |
5164 KB |
Output is correct |
58 |
Correct |
3 ms |
5072 KB |
Output is correct |
59 |
Correct |
4 ms |
5072 KB |
Output is correct |
60 |
Correct |
4 ms |
5200 KB |
Output is correct |
61 |
Correct |
4 ms |
5140 KB |
Output is correct |
62 |
Correct |
4 ms |
5200 KB |
Output is correct |
63 |
Correct |
4 ms |
5024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
3 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
3 ms |
4944 KB |
Output is correct |
6 |
Correct |
3 ms |
5024 KB |
Output is correct |
7 |
Correct |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
3 ms |
5024 KB |
Output is correct |
9 |
Correct |
3 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
5028 KB |
Output is correct |
11 |
Correct |
3 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4944 KB |
Output is correct |
13 |
Correct |
3 ms |
4944 KB |
Output is correct |
14 |
Correct |
3 ms |
4944 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
16 |
Correct |
3 ms |
4944 KB |
Output is correct |
17 |
Correct |
3 ms |
4944 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
3 ms |
4944 KB |
Output is correct |
20 |
Correct |
3 ms |
4944 KB |
Output is correct |
21 |
Correct |
3 ms |
4944 KB |
Output is correct |
22 |
Correct |
4 ms |
5092 KB |
Output is correct |
23 |
Correct |
4 ms |
5072 KB |
Output is correct |
24 |
Correct |
4 ms |
5072 KB |
Output is correct |
25 |
Correct |
3 ms |
5072 KB |
Output is correct |
26 |
Correct |
4 ms |
5072 KB |
Output is correct |
27 |
Correct |
4 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5168 KB |
Output is correct |
29 |
Correct |
4 ms |
5200 KB |
Output is correct |
30 |
Correct |
3 ms |
5160 KB |
Output is correct |
31 |
Correct |
3 ms |
5160 KB |
Output is correct |
32 |
Correct |
3 ms |
5072 KB |
Output is correct |
33 |
Correct |
3 ms |
5172 KB |
Output is correct |
34 |
Correct |
4 ms |
5164 KB |
Output is correct |
35 |
Correct |
4 ms |
5072 KB |
Output is correct |
36 |
Correct |
4 ms |
5164 KB |
Output is correct |
37 |
Correct |
3 ms |
5072 KB |
Output is correct |
38 |
Correct |
4 ms |
5072 KB |
Output is correct |
39 |
Correct |
3 ms |
5200 KB |
Output is correct |
40 |
Correct |
3 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
5200 KB |
Output is correct |
42 |
Correct |
4 ms |
5072 KB |
Output is correct |
43 |
Correct |
4 ms |
5072 KB |
Output is correct |
44 |
Correct |
4 ms |
5072 KB |
Output is correct |
45 |
Correct |
3 ms |
5072 KB |
Output is correct |
46 |
Correct |
3 ms |
5072 KB |
Output is correct |
47 |
Correct |
4 ms |
5072 KB |
Output is correct |
48 |
Correct |
4 ms |
5160 KB |
Output is correct |
49 |
Correct |
4 ms |
5200 KB |
Output is correct |
50 |
Correct |
3 ms |
5072 KB |
Output is correct |
51 |
Correct |
4 ms |
5072 KB |
Output is correct |
52 |
Correct |
3 ms |
5164 KB |
Output is correct |
53 |
Correct |
3 ms |
5072 KB |
Output is correct |
54 |
Correct |
3 ms |
5072 KB |
Output is correct |
55 |
Correct |
4 ms |
5164 KB |
Output is correct |
56 |
Correct |
3 ms |
5072 KB |
Output is correct |
57 |
Correct |
4 ms |
5164 KB |
Output is correct |
58 |
Correct |
3 ms |
5072 KB |
Output is correct |
59 |
Correct |
4 ms |
5072 KB |
Output is correct |
60 |
Correct |
4 ms |
5200 KB |
Output is correct |
61 |
Correct |
4 ms |
5140 KB |
Output is correct |
62 |
Correct |
4 ms |
5200 KB |
Output is correct |
63 |
Correct |
4 ms |
5024 KB |
Output is correct |
64 |
Correct |
131 ms |
20276 KB |
Output is correct |
65 |
Correct |
127 ms |
20256 KB |
Output is correct |
66 |
Correct |
160 ms |
20356 KB |
Output is correct |
67 |
Correct |
143 ms |
20356 KB |
Output is correct |
68 |
Correct |
126 ms |
20196 KB |
Output is correct |
69 |
Correct |
138 ms |
20188 KB |
Output is correct |
70 |
Correct |
138 ms |
20304 KB |
Output is correct |
71 |
Correct |
139 ms |
20244 KB |
Output is correct |
72 |
Correct |
175 ms |
20276 KB |
Output is correct |
73 |
Correct |
148 ms |
26792 KB |
Output is correct |
74 |
Correct |
145 ms |
26732 KB |
Output is correct |
75 |
Correct |
153 ms |
21488 KB |
Output is correct |
76 |
Correct |
144 ms |
21516 KB |
Output is correct |
77 |
Correct |
160 ms |
20152 KB |
Output is correct |
78 |
Correct |
157 ms |
20216 KB |
Output is correct |
79 |
Correct |
153 ms |
20208 KB |
Output is correct |
80 |
Correct |
153 ms |
20236 KB |
Output is correct |
81 |
Correct |
128 ms |
20280 KB |
Output is correct |
82 |
Correct |
117 ms |
20276 KB |
Output is correct |
83 |
Correct |
144 ms |
23300 KB |
Output is correct |
84 |
Correct |
176 ms |
24616 KB |
Output is correct |
85 |
Correct |
156 ms |
24444 KB |
Output is correct |
86 |
Correct |
168 ms |
21048 KB |
Output is correct |
87 |
Correct |
145 ms |
20740 KB |
Output is correct |
88 |
Correct |
148 ms |
20892 KB |
Output is correct |
89 |
Correct |
125 ms |
20892 KB |
Output is correct |
90 |
Correct |
121 ms |
20716 KB |
Output is correct |
91 |
Correct |
128 ms |
20916 KB |
Output is correct |
92 |
Correct |
150 ms |
21128 KB |
Output is correct |
93 |
Correct |
147 ms |
22200 KB |
Output is correct |
94 |
Correct |
110 ms |
23228 KB |
Output is correct |
95 |
Correct |
136 ms |
22552 KB |
Output is correct |
96 |
Correct |
137 ms |
21472 KB |
Output is correct |
97 |
Correct |
135 ms |
20536 KB |
Output is correct |
98 |
Correct |
129 ms |
20440 KB |
Output is correct |
99 |
Correct |
144 ms |
20320 KB |
Output is correct |
100 |
Correct |
125 ms |
21952 KB |
Output is correct |
101 |
Correct |
147 ms |
20740 KB |
Output is correct |
102 |
Correct |
140 ms |
20412 KB |
Output is correct |
103 |
Correct |
127 ms |
20468 KB |
Output is correct |
104 |
Correct |
89 ms |
20892 KB |
Output is correct |
105 |
Correct |
144 ms |
24800 KB |
Output is correct |
106 |
Correct |
82 ms |
20788 KB |
Output is correct |
107 |
Correct |
72 ms |
20740 KB |
Output is correct |
108 |
Correct |
148 ms |
27272 KB |
Output is correct |
109 |
Correct |
84 ms |
21036 KB |
Output is correct |
110 |
Correct |
165 ms |
27460 KB |
Output is correct |
111 |
Correct |
129 ms |
20220 KB |
Output is correct |