#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x;
const int maxn = 2e5 + 5;
const int INF = 1e18;
vector<int> adj[maxn];
int st[maxn], dp[maxn];
int ans = 0;
void dfs(int pos, int prev){
int ttl = 0, ad = 0, mx = 0;
for(auto x : adj[pos]){
if(x == prev) continue;
dfs(x, pos);
if(dp[x] == 0) continue;
mx = max(mx, dp[x]);
ttl += dp[x];
ad++;
}
ans = max(ans, mx + st[pos]);
dp[pos] = max(ttl - st[pos], st[pos]);
ans = max(ans, dp[pos]);
}
signed main(void){
fastio;
int n;
cin>>n;
for(int i = 0; i < n - 1; i++){
int a, b;
cin>>a>>b;
a--, b--;
adj[a].pb(b);
adj[b].pb(a);
}
string s;
cin>>s;
for(int i = 0; i < n; i++) st[i] = (s[i] == '1');
dfs(0, -1);
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
3 ms |
8092 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8108 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8028 KB |
Output is correct |
9 |
Correct |
2 ms |
8028 KB |
Output is correct |
10 |
Correct |
2 ms |
8028 KB |
Output is correct |
11 |
Correct |
2 ms |
8028 KB |
Output is correct |
12 |
Correct |
2 ms |
8112 KB |
Output is correct |
13 |
Correct |
2 ms |
8280 KB |
Output is correct |
14 |
Correct |
2 ms |
8028 KB |
Output is correct |
15 |
Correct |
3 ms |
8028 KB |
Output is correct |
16 |
Correct |
2 ms |
8028 KB |
Output is correct |
17 |
Correct |
2 ms |
8028 KB |
Output is correct |
18 |
Correct |
3 ms |
8028 KB |
Output is correct |
19 |
Correct |
2 ms |
8028 KB |
Output is correct |
20 |
Correct |
3 ms |
8028 KB |
Output is correct |
21 |
Correct |
2 ms |
8028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
3 ms |
8092 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8108 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8028 KB |
Output is correct |
9 |
Correct |
2 ms |
8028 KB |
Output is correct |
10 |
Correct |
2 ms |
8028 KB |
Output is correct |
11 |
Correct |
2 ms |
8028 KB |
Output is correct |
12 |
Correct |
2 ms |
8112 KB |
Output is correct |
13 |
Correct |
2 ms |
8280 KB |
Output is correct |
14 |
Correct |
2 ms |
8028 KB |
Output is correct |
15 |
Correct |
3 ms |
8028 KB |
Output is correct |
16 |
Correct |
2 ms |
8028 KB |
Output is correct |
17 |
Correct |
2 ms |
8028 KB |
Output is correct |
18 |
Correct |
3 ms |
8028 KB |
Output is correct |
19 |
Correct |
2 ms |
8028 KB |
Output is correct |
20 |
Correct |
3 ms |
8028 KB |
Output is correct |
21 |
Correct |
2 ms |
8028 KB |
Output is correct |
22 |
Correct |
3 ms |
8028 KB |
Output is correct |
23 |
Correct |
2 ms |
8028 KB |
Output is correct |
24 |
Correct |
2 ms |
8028 KB |
Output is correct |
25 |
Correct |
4 ms |
8284 KB |
Output is correct |
26 |
Correct |
3 ms |
8024 KB |
Output is correct |
27 |
Correct |
3 ms |
8028 KB |
Output is correct |
28 |
Correct |
2 ms |
8028 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
30 |
Correct |
3 ms |
8240 KB |
Output is correct |
31 |
Correct |
3 ms |
8028 KB |
Output is correct |
32 |
Correct |
2 ms |
8028 KB |
Output is correct |
33 |
Correct |
2 ms |
8028 KB |
Output is correct |
34 |
Correct |
2 ms |
8028 KB |
Output is correct |
35 |
Correct |
3 ms |
8124 KB |
Output is correct |
36 |
Correct |
3 ms |
8028 KB |
Output is correct |
37 |
Correct |
3 ms |
8284 KB |
Output is correct |
38 |
Correct |
3 ms |
8028 KB |
Output is correct |
39 |
Correct |
2 ms |
8028 KB |
Output is correct |
40 |
Correct |
3 ms |
8028 KB |
Output is correct |
41 |
Correct |
3 ms |
8284 KB |
Output is correct |
42 |
Correct |
3 ms |
8028 KB |
Output is correct |
43 |
Correct |
3 ms |
8028 KB |
Output is correct |
44 |
Correct |
3 ms |
8028 KB |
Output is correct |
45 |
Correct |
3 ms |
8280 KB |
Output is correct |
46 |
Correct |
3 ms |
8024 KB |
Output is correct |
47 |
Correct |
3 ms |
8028 KB |
Output is correct |
48 |
Correct |
3 ms |
8280 KB |
Output is correct |
49 |
Correct |
3 ms |
8284 KB |
Output is correct |
50 |
Correct |
3 ms |
8028 KB |
Output is correct |
51 |
Correct |
4 ms |
8536 KB |
Output is correct |
52 |
Correct |
3 ms |
8028 KB |
Output is correct |
53 |
Correct |
3 ms |
8216 KB |
Output is correct |
54 |
Correct |
3 ms |
8204 KB |
Output is correct |
55 |
Correct |
2 ms |
8028 KB |
Output is correct |
56 |
Correct |
4 ms |
8028 KB |
Output is correct |
57 |
Correct |
3 ms |
8284 KB |
Output is correct |
58 |
Correct |
2 ms |
8028 KB |
Output is correct |
59 |
Correct |
3 ms |
8028 KB |
Output is correct |
60 |
Correct |
3 ms |
8284 KB |
Output is correct |
61 |
Correct |
3 ms |
8028 KB |
Output is correct |
62 |
Correct |
3 ms |
8284 KB |
Output is correct |
63 |
Correct |
3 ms |
8024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
3 ms |
8092 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8108 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8028 KB |
Output is correct |
9 |
Correct |
2 ms |
8028 KB |
Output is correct |
10 |
Correct |
2 ms |
8028 KB |
Output is correct |
11 |
Correct |
2 ms |
8028 KB |
Output is correct |
12 |
Correct |
2 ms |
8112 KB |
Output is correct |
13 |
Correct |
2 ms |
8280 KB |
Output is correct |
14 |
Correct |
2 ms |
8028 KB |
Output is correct |
15 |
Correct |
3 ms |
8028 KB |
Output is correct |
16 |
Correct |
2 ms |
8028 KB |
Output is correct |
17 |
Correct |
2 ms |
8028 KB |
Output is correct |
18 |
Correct |
3 ms |
8028 KB |
Output is correct |
19 |
Correct |
2 ms |
8028 KB |
Output is correct |
20 |
Correct |
3 ms |
8028 KB |
Output is correct |
21 |
Correct |
2 ms |
8028 KB |
Output is correct |
22 |
Correct |
3 ms |
8028 KB |
Output is correct |
23 |
Correct |
2 ms |
8028 KB |
Output is correct |
24 |
Correct |
2 ms |
8028 KB |
Output is correct |
25 |
Correct |
4 ms |
8284 KB |
Output is correct |
26 |
Correct |
3 ms |
8024 KB |
Output is correct |
27 |
Correct |
3 ms |
8028 KB |
Output is correct |
28 |
Correct |
2 ms |
8028 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
30 |
Correct |
3 ms |
8240 KB |
Output is correct |
31 |
Correct |
3 ms |
8028 KB |
Output is correct |
32 |
Correct |
2 ms |
8028 KB |
Output is correct |
33 |
Correct |
2 ms |
8028 KB |
Output is correct |
34 |
Correct |
2 ms |
8028 KB |
Output is correct |
35 |
Correct |
3 ms |
8124 KB |
Output is correct |
36 |
Correct |
3 ms |
8028 KB |
Output is correct |
37 |
Correct |
3 ms |
8284 KB |
Output is correct |
38 |
Correct |
3 ms |
8028 KB |
Output is correct |
39 |
Correct |
2 ms |
8028 KB |
Output is correct |
40 |
Correct |
3 ms |
8028 KB |
Output is correct |
41 |
Correct |
3 ms |
8284 KB |
Output is correct |
42 |
Correct |
3 ms |
8028 KB |
Output is correct |
43 |
Correct |
3 ms |
8028 KB |
Output is correct |
44 |
Correct |
3 ms |
8028 KB |
Output is correct |
45 |
Correct |
3 ms |
8280 KB |
Output is correct |
46 |
Correct |
3 ms |
8024 KB |
Output is correct |
47 |
Correct |
3 ms |
8028 KB |
Output is correct |
48 |
Correct |
3 ms |
8280 KB |
Output is correct |
49 |
Correct |
3 ms |
8284 KB |
Output is correct |
50 |
Correct |
3 ms |
8028 KB |
Output is correct |
51 |
Correct |
4 ms |
8536 KB |
Output is correct |
52 |
Correct |
3 ms |
8028 KB |
Output is correct |
53 |
Correct |
3 ms |
8216 KB |
Output is correct |
54 |
Correct |
3 ms |
8204 KB |
Output is correct |
55 |
Correct |
2 ms |
8028 KB |
Output is correct |
56 |
Correct |
4 ms |
8028 KB |
Output is correct |
57 |
Correct |
3 ms |
8284 KB |
Output is correct |
58 |
Correct |
2 ms |
8028 KB |
Output is correct |
59 |
Correct |
3 ms |
8028 KB |
Output is correct |
60 |
Correct |
3 ms |
8284 KB |
Output is correct |
61 |
Correct |
3 ms |
8028 KB |
Output is correct |
62 |
Correct |
3 ms |
8284 KB |
Output is correct |
63 |
Correct |
3 ms |
8024 KB |
Output is correct |
64 |
Correct |
72 ms |
18448 KB |
Output is correct |
65 |
Correct |
74 ms |
18476 KB |
Output is correct |
66 |
Correct |
103 ms |
18544 KB |
Output is correct |
67 |
Correct |
80 ms |
18664 KB |
Output is correct |
68 |
Correct |
72 ms |
18564 KB |
Output is correct |
69 |
Correct |
93 ms |
18576 KB |
Output is correct |
70 |
Correct |
68 ms |
18636 KB |
Output is correct |
71 |
Correct |
69 ms |
18592 KB |
Output is correct |
72 |
Correct |
103 ms |
18644 KB |
Output is correct |
73 |
Correct |
102 ms |
30332 KB |
Output is correct |
74 |
Correct |
87 ms |
30068 KB |
Output is correct |
75 |
Correct |
71 ms |
19716 KB |
Output is correct |
76 |
Correct |
73 ms |
19864 KB |
Output is correct |
77 |
Correct |
68 ms |
18572 KB |
Output is correct |
78 |
Correct |
70 ms |
18576 KB |
Output is correct |
79 |
Correct |
110 ms |
18596 KB |
Output is correct |
80 |
Correct |
69 ms |
18996 KB |
Output is correct |
81 |
Correct |
55 ms |
18636 KB |
Output is correct |
82 |
Correct |
64 ms |
18640 KB |
Output is correct |
83 |
Correct |
100 ms |
23700 KB |
Output is correct |
84 |
Correct |
86 ms |
25700 KB |
Output is correct |
85 |
Correct |
80 ms |
25500 KB |
Output is correct |
86 |
Correct |
68 ms |
19860 KB |
Output is correct |
87 |
Correct |
97 ms |
19908 KB |
Output is correct |
88 |
Correct |
73 ms |
19408 KB |
Output is correct |
89 |
Correct |
64 ms |
19360 KB |
Output is correct |
90 |
Correct |
62 ms |
19192 KB |
Output is correct |
91 |
Correct |
67 ms |
19344 KB |
Output is correct |
92 |
Correct |
101 ms |
19884 KB |
Output is correct |
93 |
Correct |
70 ms |
22120 KB |
Output is correct |
94 |
Correct |
68 ms |
23748 KB |
Output is correct |
95 |
Correct |
66 ms |
22468 KB |
Output is correct |
96 |
Correct |
63 ms |
20476 KB |
Output is correct |
97 |
Correct |
67 ms |
19108 KB |
Output is correct |
98 |
Correct |
71 ms |
18624 KB |
Output is correct |
99 |
Correct |
98 ms |
18692 KB |
Output is correct |
100 |
Correct |
81 ms |
21596 KB |
Output is correct |
101 |
Correct |
93 ms |
19528 KB |
Output is correct |
102 |
Correct |
61 ms |
18788 KB |
Output is correct |
103 |
Correct |
93 ms |
18864 KB |
Output is correct |
104 |
Correct |
43 ms |
19136 KB |
Output is correct |
105 |
Correct |
92 ms |
27096 KB |
Output is correct |
106 |
Correct |
53 ms |
19132 KB |
Output is correct |
107 |
Correct |
57 ms |
19012 KB |
Output is correct |
108 |
Correct |
86 ms |
30900 KB |
Output is correct |
109 |
Correct |
48 ms |
19216 KB |
Output is correct |
110 |
Correct |
85 ms |
31376 KB |
Output is correct |
111 |
Correct |
107 ms |
18440 KB |
Output is correct |