//Never stop trying
#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef string str;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
const int MOD = 1e9 + 7; //998244353
const ll INF = 1e18;
const int MX = 2e5 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
template<class T> using V = vector<T>;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
//constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
ll random(ll a, ll b){
return a + rng() % (b - a + 1);
}
#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define dbg(x) cerr << #x << " : " << x << endl;
#define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;
void IO() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
/////////////////////////ONLY CLEAN CODES ALLOWED/////////////////////////
int N;
vi adj[MX],gen(MX),f1(MX),f2(MX);
void dfs1(int u=1, int p=0){
int ans=gen[u];
int s=-gen[u];
for(auto v: adj[u]) if(v!=p){
dfs1(v,u);
s+=f1[v];
}
ckmax(ans,s);
f1[u]=ans;
}
void dfs2(int u=1, int p=0){
int ans=gen[u];
int s=-gen[u];
for(auto v: adj[u]) if(v!=p){
dfs2(v,u);
ckmax(ans,f2[v]);
s+=f1[v];
if(gen[u]) ckmax(ans,f1[v]+1);
}
ckmax(ans,s);
f2[u]=ans;
}
int main() {
boost; IO();
cin>>N;
FOR(i,0,N-1){
int u,v; cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
FOR(i,1,N+1){
char c; cin>>c;
gen[i]=c-'0';
}
dfs1();
dfs2();
cout << f2[1] << endl;
return 0;
}
//Change your approach
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
5 ms |
7404 KB |
Output is correct |
4 |
Correct |
5 ms |
7404 KB |
Output is correct |
5 |
Correct |
5 ms |
7404 KB |
Output is correct |
6 |
Correct |
5 ms |
7404 KB |
Output is correct |
7 |
Correct |
5 ms |
7404 KB |
Output is correct |
8 |
Correct |
5 ms |
7404 KB |
Output is correct |
9 |
Correct |
5 ms |
7404 KB |
Output is correct |
10 |
Correct |
5 ms |
7404 KB |
Output is correct |
11 |
Correct |
5 ms |
7404 KB |
Output is correct |
12 |
Correct |
5 ms |
7404 KB |
Output is correct |
13 |
Correct |
6 ms |
7532 KB |
Output is correct |
14 |
Correct |
5 ms |
7404 KB |
Output is correct |
15 |
Correct |
5 ms |
7404 KB |
Output is correct |
16 |
Correct |
6 ms |
7424 KB |
Output is correct |
17 |
Correct |
6 ms |
7404 KB |
Output is correct |
18 |
Correct |
5 ms |
7404 KB |
Output is correct |
19 |
Correct |
5 ms |
7404 KB |
Output is correct |
20 |
Correct |
5 ms |
7404 KB |
Output is correct |
21 |
Correct |
5 ms |
7404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
5 ms |
7404 KB |
Output is correct |
4 |
Correct |
5 ms |
7404 KB |
Output is correct |
5 |
Correct |
5 ms |
7404 KB |
Output is correct |
6 |
Correct |
5 ms |
7404 KB |
Output is correct |
7 |
Correct |
5 ms |
7404 KB |
Output is correct |
8 |
Correct |
5 ms |
7404 KB |
Output is correct |
9 |
Correct |
5 ms |
7404 KB |
Output is correct |
10 |
Correct |
5 ms |
7404 KB |
Output is correct |
11 |
Correct |
5 ms |
7404 KB |
Output is correct |
12 |
Correct |
5 ms |
7404 KB |
Output is correct |
13 |
Correct |
6 ms |
7532 KB |
Output is correct |
14 |
Correct |
5 ms |
7404 KB |
Output is correct |
15 |
Correct |
5 ms |
7404 KB |
Output is correct |
16 |
Correct |
6 ms |
7424 KB |
Output is correct |
17 |
Correct |
6 ms |
7404 KB |
Output is correct |
18 |
Correct |
5 ms |
7404 KB |
Output is correct |
19 |
Correct |
5 ms |
7404 KB |
Output is correct |
20 |
Correct |
5 ms |
7404 KB |
Output is correct |
21 |
Correct |
5 ms |
7404 KB |
Output is correct |
22 |
Correct |
6 ms |
7532 KB |
Output is correct |
23 |
Correct |
6 ms |
7532 KB |
Output is correct |
24 |
Correct |
6 ms |
7532 KB |
Output is correct |
25 |
Correct |
7 ms |
7532 KB |
Output is correct |
26 |
Correct |
6 ms |
7532 KB |
Output is correct |
27 |
Correct |
7 ms |
7532 KB |
Output is correct |
28 |
Correct |
6 ms |
7532 KB |
Output is correct |
29 |
Correct |
6 ms |
7680 KB |
Output is correct |
30 |
Correct |
6 ms |
7664 KB |
Output is correct |
31 |
Correct |
6 ms |
7532 KB |
Output is correct |
32 |
Correct |
7 ms |
7532 KB |
Output is correct |
33 |
Correct |
6 ms |
7532 KB |
Output is correct |
34 |
Correct |
6 ms |
7532 KB |
Output is correct |
35 |
Correct |
6 ms |
7532 KB |
Output is correct |
36 |
Correct |
8 ms |
7532 KB |
Output is correct |
37 |
Correct |
6 ms |
7532 KB |
Output is correct |
38 |
Correct |
6 ms |
7532 KB |
Output is correct |
39 |
Correct |
6 ms |
7532 KB |
Output is correct |
40 |
Correct |
6 ms |
7532 KB |
Output is correct |
41 |
Correct |
6 ms |
7532 KB |
Output is correct |
42 |
Correct |
6 ms |
7532 KB |
Output is correct |
43 |
Correct |
6 ms |
7532 KB |
Output is correct |
44 |
Correct |
6 ms |
7532 KB |
Output is correct |
45 |
Correct |
6 ms |
7532 KB |
Output is correct |
46 |
Correct |
6 ms |
7532 KB |
Output is correct |
47 |
Correct |
8 ms |
7532 KB |
Output is correct |
48 |
Correct |
8 ms |
7532 KB |
Output is correct |
49 |
Correct |
6 ms |
7532 KB |
Output is correct |
50 |
Correct |
6 ms |
7536 KB |
Output is correct |
51 |
Correct |
6 ms |
7532 KB |
Output is correct |
52 |
Correct |
6 ms |
7532 KB |
Output is correct |
53 |
Correct |
6 ms |
7532 KB |
Output is correct |
54 |
Correct |
6 ms |
7532 KB |
Output is correct |
55 |
Correct |
6 ms |
7532 KB |
Output is correct |
56 |
Correct |
6 ms |
7532 KB |
Output is correct |
57 |
Correct |
6 ms |
7660 KB |
Output is correct |
58 |
Correct |
6 ms |
7532 KB |
Output is correct |
59 |
Correct |
6 ms |
7532 KB |
Output is correct |
60 |
Correct |
6 ms |
7532 KB |
Output is correct |
61 |
Correct |
6 ms |
7532 KB |
Output is correct |
62 |
Correct |
6 ms |
7660 KB |
Output is correct |
63 |
Correct |
6 ms |
7532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
5 ms |
7404 KB |
Output is correct |
4 |
Correct |
5 ms |
7404 KB |
Output is correct |
5 |
Correct |
5 ms |
7404 KB |
Output is correct |
6 |
Correct |
5 ms |
7404 KB |
Output is correct |
7 |
Correct |
5 ms |
7404 KB |
Output is correct |
8 |
Correct |
5 ms |
7404 KB |
Output is correct |
9 |
Correct |
5 ms |
7404 KB |
Output is correct |
10 |
Correct |
5 ms |
7404 KB |
Output is correct |
11 |
Correct |
5 ms |
7404 KB |
Output is correct |
12 |
Correct |
5 ms |
7404 KB |
Output is correct |
13 |
Correct |
6 ms |
7532 KB |
Output is correct |
14 |
Correct |
5 ms |
7404 KB |
Output is correct |
15 |
Correct |
5 ms |
7404 KB |
Output is correct |
16 |
Correct |
6 ms |
7424 KB |
Output is correct |
17 |
Correct |
6 ms |
7404 KB |
Output is correct |
18 |
Correct |
5 ms |
7404 KB |
Output is correct |
19 |
Correct |
5 ms |
7404 KB |
Output is correct |
20 |
Correct |
5 ms |
7404 KB |
Output is correct |
21 |
Correct |
5 ms |
7404 KB |
Output is correct |
22 |
Correct |
6 ms |
7532 KB |
Output is correct |
23 |
Correct |
6 ms |
7532 KB |
Output is correct |
24 |
Correct |
6 ms |
7532 KB |
Output is correct |
25 |
Correct |
7 ms |
7532 KB |
Output is correct |
26 |
Correct |
6 ms |
7532 KB |
Output is correct |
27 |
Correct |
7 ms |
7532 KB |
Output is correct |
28 |
Correct |
6 ms |
7532 KB |
Output is correct |
29 |
Correct |
6 ms |
7680 KB |
Output is correct |
30 |
Correct |
6 ms |
7664 KB |
Output is correct |
31 |
Correct |
6 ms |
7532 KB |
Output is correct |
32 |
Correct |
7 ms |
7532 KB |
Output is correct |
33 |
Correct |
6 ms |
7532 KB |
Output is correct |
34 |
Correct |
6 ms |
7532 KB |
Output is correct |
35 |
Correct |
6 ms |
7532 KB |
Output is correct |
36 |
Correct |
8 ms |
7532 KB |
Output is correct |
37 |
Correct |
6 ms |
7532 KB |
Output is correct |
38 |
Correct |
6 ms |
7532 KB |
Output is correct |
39 |
Correct |
6 ms |
7532 KB |
Output is correct |
40 |
Correct |
6 ms |
7532 KB |
Output is correct |
41 |
Correct |
6 ms |
7532 KB |
Output is correct |
42 |
Correct |
6 ms |
7532 KB |
Output is correct |
43 |
Correct |
6 ms |
7532 KB |
Output is correct |
44 |
Correct |
6 ms |
7532 KB |
Output is correct |
45 |
Correct |
6 ms |
7532 KB |
Output is correct |
46 |
Correct |
6 ms |
7532 KB |
Output is correct |
47 |
Correct |
8 ms |
7532 KB |
Output is correct |
48 |
Correct |
8 ms |
7532 KB |
Output is correct |
49 |
Correct |
6 ms |
7532 KB |
Output is correct |
50 |
Correct |
6 ms |
7536 KB |
Output is correct |
51 |
Correct |
6 ms |
7532 KB |
Output is correct |
52 |
Correct |
6 ms |
7532 KB |
Output is correct |
53 |
Correct |
6 ms |
7532 KB |
Output is correct |
54 |
Correct |
6 ms |
7532 KB |
Output is correct |
55 |
Correct |
6 ms |
7532 KB |
Output is correct |
56 |
Correct |
6 ms |
7532 KB |
Output is correct |
57 |
Correct |
6 ms |
7660 KB |
Output is correct |
58 |
Correct |
6 ms |
7532 KB |
Output is correct |
59 |
Correct |
6 ms |
7532 KB |
Output is correct |
60 |
Correct |
6 ms |
7532 KB |
Output is correct |
61 |
Correct |
6 ms |
7532 KB |
Output is correct |
62 |
Correct |
6 ms |
7660 KB |
Output is correct |
63 |
Correct |
6 ms |
7532 KB |
Output is correct |
64 |
Correct |
182 ms |
16492 KB |
Output is correct |
65 |
Correct |
204 ms |
16492 KB |
Output is correct |
66 |
Correct |
186 ms |
16492 KB |
Output is correct |
67 |
Correct |
188 ms |
16492 KB |
Output is correct |
68 |
Correct |
182 ms |
16620 KB |
Output is correct |
69 |
Correct |
190 ms |
16492 KB |
Output is correct |
70 |
Correct |
191 ms |
16492 KB |
Output is correct |
71 |
Correct |
195 ms |
16620 KB |
Output is correct |
72 |
Correct |
184 ms |
16492 KB |
Output is correct |
73 |
Correct |
223 ms |
31596 KB |
Output is correct |
74 |
Correct |
218 ms |
31596 KB |
Output is correct |
75 |
Correct |
182 ms |
16364 KB |
Output is correct |
76 |
Correct |
191 ms |
16620 KB |
Output is correct |
77 |
Correct |
191 ms |
16364 KB |
Output is correct |
78 |
Correct |
210 ms |
16492 KB |
Output is correct |
79 |
Correct |
193 ms |
16340 KB |
Output is correct |
80 |
Correct |
190 ms |
16364 KB |
Output is correct |
81 |
Correct |
154 ms |
16740 KB |
Output is correct |
82 |
Correct |
170 ms |
16616 KB |
Output is correct |
83 |
Correct |
200 ms |
22636 KB |
Output is correct |
84 |
Correct |
239 ms |
25324 KB |
Output is correct |
85 |
Correct |
207 ms |
24812 KB |
Output is correct |
86 |
Correct |
188 ms |
18156 KB |
Output is correct |
87 |
Correct |
207 ms |
17520 KB |
Output is correct |
88 |
Correct |
181 ms |
17920 KB |
Output is correct |
89 |
Correct |
178 ms |
17900 KB |
Output is correct |
90 |
Correct |
180 ms |
17644 KB |
Output is correct |
91 |
Correct |
182 ms |
17900 KB |
Output is correct |
92 |
Correct |
194 ms |
18412 KB |
Output is correct |
93 |
Correct |
197 ms |
21224 KB |
Output is correct |
94 |
Correct |
166 ms |
23300 KB |
Output is correct |
95 |
Correct |
167 ms |
21736 KB |
Output is correct |
96 |
Correct |
174 ms |
19692 KB |
Output is correct |
97 |
Correct |
170 ms |
17772 KB |
Output is correct |
98 |
Correct |
172 ms |
17260 KB |
Output is correct |
99 |
Correct |
200 ms |
16876 KB |
Output is correct |
100 |
Correct |
186 ms |
20716 KB |
Output is correct |
101 |
Correct |
170 ms |
18284 KB |
Output is correct |
102 |
Correct |
178 ms |
17388 KB |
Output is correct |
103 |
Correct |
181 ms |
17132 KB |
Output is correct |
104 |
Correct |
101 ms |
17248 KB |
Output is correct |
105 |
Correct |
208 ms |
27884 KB |
Output is correct |
106 |
Correct |
112 ms |
17120 KB |
Output is correct |
107 |
Correct |
105 ms |
17248 KB |
Output is correct |
108 |
Correct |
223 ms |
32632 KB |
Output is correct |
109 |
Correct |
104 ms |
17508 KB |
Output is correct |
110 |
Correct |
213 ms |
33004 KB |
Output is correct |
111 |
Correct |
173 ms |
16620 KB |
Output is correct |