#include<bits/stdc++.h>
using namespace std;
#define taskname "template"
#define ll long long
#define ld double
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int,int>
#define vii vector<pii>
#define vvi vector<vi>
#define pb push_back
#define eb emplace_back
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MN=2e5+5;
int n;
int a,b;
string s;
vi son[MN];
pii dp[MN];
int ans;
void dfs(int u,int par){
int cnt=0,max1=0;
for(int v:son[u]){
if(v==par) continue;
dfs(v,u);
dp[u].fi+=dp[v].fi;
max1=max(max1,dp[v].fi);
}
ans=max({ans,max1+(s[u]=='1'),dp[u].fi-(s[u]=='1')});
dp[u].fi = max(dp[u].fi - (s[u] == '1'), (s[u] - '0'));
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
/*
freopen(taskname".inp","r",stdin);
freopen(taskname".out","w",stdout);
*/
cin>>n;
for(int i=1;i<n;++i){
cin>>a>>b;
son[a].eb(b); son[b].eb(a);
}
cin>>s;
s='#'+s;
dfs(1,0);
// for(int i=1;i<=n;++i){
// cout<<"Case "<<i<<": "<<dp[i].fi<<'\n';
// }
cout<<ans<<'\n';
}
Compilation message
power.cpp: In function 'void dfs(int, int)':
power.cpp:28:9: warning: unused variable 'cnt' [-Wunused-variable]
28 | int cnt=0,max1=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
2 ms |
5024 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4928 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
5028 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
5028 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5004 KB |
Output is correct |
21 |
Correct |
2 ms |
5028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
2 ms |
5024 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4928 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
5028 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
5028 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5004 KB |
Output is correct |
21 |
Correct |
2 ms |
5028 KB |
Output is correct |
22 |
Correct |
3 ms |
5048 KB |
Output is correct |
23 |
Correct |
3 ms |
5040 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5040 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5004 KB |
Output is correct |
29 |
Correct |
3 ms |
5152 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5036 KB |
Output is correct |
32 |
Correct |
3 ms |
5088 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5040 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5044 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5168 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
5040 KB |
Output is correct |
43 |
Correct |
3 ms |
5052 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5040 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
3 ms |
5164 KB |
Output is correct |
50 |
Correct |
3 ms |
5168 KB |
Output is correct |
51 |
Correct |
3 ms |
5164 KB |
Output is correct |
52 |
Correct |
4 ms |
5172 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
3 ms |
5044 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
3 ms |
5176 KB |
Output is correct |
58 |
Correct |
3 ms |
5040 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
3 ms |
5204 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5028 KB |
Output is correct |
4 |
Correct |
2 ms |
5024 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4928 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
5028 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
5028 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5004 KB |
Output is correct |
21 |
Correct |
2 ms |
5028 KB |
Output is correct |
22 |
Correct |
3 ms |
5048 KB |
Output is correct |
23 |
Correct |
3 ms |
5040 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5040 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5004 KB |
Output is correct |
29 |
Correct |
3 ms |
5152 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5036 KB |
Output is correct |
32 |
Correct |
3 ms |
5088 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5120 KB |
Output is correct |
35 |
Correct |
4 ms |
5040 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5044 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5168 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
5040 KB |
Output is correct |
43 |
Correct |
3 ms |
5052 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5040 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
3 ms |
5164 KB |
Output is correct |
50 |
Correct |
3 ms |
5168 KB |
Output is correct |
51 |
Correct |
3 ms |
5164 KB |
Output is correct |
52 |
Correct |
4 ms |
5172 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
3 ms |
5044 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
3 ms |
5176 KB |
Output is correct |
58 |
Correct |
3 ms |
5040 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
3 ms |
5204 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
64 |
Correct |
104 ms |
13620 KB |
Output is correct |
65 |
Correct |
105 ms |
13600 KB |
Output is correct |
66 |
Correct |
76 ms |
13644 KB |
Output is correct |
67 |
Correct |
76 ms |
13600 KB |
Output is correct |
68 |
Correct |
75 ms |
13656 KB |
Output is correct |
69 |
Correct |
89 ms |
13584 KB |
Output is correct |
70 |
Correct |
108 ms |
13608 KB |
Output is correct |
71 |
Correct |
81 ms |
13636 KB |
Output is correct |
72 |
Correct |
84 ms |
13612 KB |
Output is correct |
73 |
Correct |
107 ms |
26180 KB |
Output is correct |
74 |
Correct |
131 ms |
26304 KB |
Output is correct |
75 |
Correct |
77 ms |
13516 KB |
Output is correct |
76 |
Correct |
70 ms |
13460 KB |
Output is correct |
77 |
Correct |
92 ms |
13452 KB |
Output is correct |
78 |
Correct |
72 ms |
13492 KB |
Output is correct |
79 |
Correct |
76 ms |
13672 KB |
Output is correct |
80 |
Correct |
73 ms |
13504 KB |
Output is correct |
81 |
Correct |
63 ms |
13932 KB |
Output is correct |
82 |
Correct |
100 ms |
13728 KB |
Output is correct |
83 |
Correct |
100 ms |
18680 KB |
Output is correct |
84 |
Correct |
87 ms |
20824 KB |
Output is correct |
85 |
Correct |
85 ms |
20560 KB |
Output is correct |
86 |
Correct |
86 ms |
14968 KB |
Output is correct |
87 |
Correct |
93 ms |
14376 KB |
Output is correct |
88 |
Correct |
83 ms |
14892 KB |
Output is correct |
89 |
Correct |
72 ms |
14848 KB |
Output is correct |
90 |
Correct |
111 ms |
14684 KB |
Output is correct |
91 |
Correct |
74 ms |
14872 KB |
Output is correct |
92 |
Correct |
84 ms |
15200 KB |
Output is correct |
93 |
Correct |
93 ms |
17704 KB |
Output is correct |
94 |
Correct |
82 ms |
19432 KB |
Output is correct |
95 |
Correct |
85 ms |
18048 KB |
Output is correct |
96 |
Correct |
75 ms |
16188 KB |
Output is correct |
97 |
Correct |
79 ms |
14744 KB |
Output is correct |
98 |
Correct |
86 ms |
14188 KB |
Output is correct |
99 |
Correct |
75 ms |
13944 KB |
Output is correct |
100 |
Correct |
69 ms |
17200 KB |
Output is correct |
101 |
Correct |
69 ms |
15148 KB |
Output is correct |
102 |
Correct |
96 ms |
14468 KB |
Output is correct |
103 |
Correct |
69 ms |
14168 KB |
Output is correct |
104 |
Correct |
52 ms |
14524 KB |
Output is correct |
105 |
Correct |
88 ms |
23024 KB |
Output is correct |
106 |
Correct |
57 ms |
14404 KB |
Output is correct |
107 |
Correct |
59 ms |
14452 KB |
Output is correct |
108 |
Correct |
91 ms |
26968 KB |
Output is correct |
109 |
Correct |
52 ms |
14540 KB |
Output is correct |
110 |
Correct |
85 ms |
27272 KB |
Output is correct |
111 |
Correct |
85 ms |
13604 KB |
Output is correct |