#include<bits/stdc++.h>
#define ll long long
#define ldb long double
#define fi first
#define se second
#define sza(a) (int)a.size()
#define pir pair<int,int>
#define pirll pair<ll,ll>
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 1e8;
bool a[maxn];
int dp[maxn],f[maxn];
vector <vector<int>> vec(maxn);
void input(int n){
for (int i = 1 ; i < n ; i++){
int u,v;
cin >> u >> v;
vec[u].push_back(v);
vec[v].push_back(u);
}
string s;
cin >> s;
for (int i = 0 ; i < s.size() ; i++)
a[i + 1] = s[i] - 48;
}
void dfs(int u,int par){
int t = -a[u];
dp[u] = a[u];
f[u] = a[u];
for (int v : vec[u])
if (v != par){
dfs(v,u);
t += dp[v];
f[u] = max(f[u],a[u] + dp[v]);
}
dp[u] = max(dp[u],t);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// freopen("power.inp","r",stdin);
// freopen("power.out","w",stdout);
int n;
cin >> n;
input(n);
dfs(1,0);
int res = 0;
for (int i = 1 ; i <= n ; i++) res = max(res,max(dp[i],f[i]));
cout << res;
return 0;
}
Compilation message
power.cpp: In function 'void input(int)':
power.cpp:27:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int i = 0 ; i < s.size() ; i++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
5180 KB |
Output is correct |
8 |
Correct |
3 ms |
5044 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
4 ms |
5184 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
5180 KB |
Output is correct |
8 |
Correct |
3 ms |
5044 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
4 ms |
5184 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
4 ms |
5260 KB |
Output is correct |
24 |
Correct |
4 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5188 KB |
Output is correct |
26 |
Correct |
4 ms |
5208 KB |
Output is correct |
27 |
Correct |
4 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5208 KB |
Output is correct |
30 |
Correct |
3 ms |
5184 KB |
Output is correct |
31 |
Correct |
4 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
2 ms |
5224 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
4 ms |
5280 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
3 ms |
5212 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
39 |
Correct |
3 ms |
5212 KB |
Output is correct |
40 |
Correct |
2 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
2 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
3 ms |
5208 KB |
Output is correct |
47 |
Correct |
3 ms |
5212 KB |
Output is correct |
48 |
Correct |
3 ms |
5212 KB |
Output is correct |
49 |
Correct |
2 ms |
5212 KB |
Output is correct |
50 |
Correct |
3 ms |
5212 KB |
Output is correct |
51 |
Correct |
2 ms |
5208 KB |
Output is correct |
52 |
Correct |
2 ms |
5212 KB |
Output is correct |
53 |
Correct |
2 ms |
5212 KB |
Output is correct |
54 |
Correct |
2 ms |
5212 KB |
Output is correct |
55 |
Correct |
2 ms |
5212 KB |
Output is correct |
56 |
Correct |
2 ms |
5212 KB |
Output is correct |
57 |
Correct |
2 ms |
5212 KB |
Output is correct |
58 |
Correct |
2 ms |
5464 KB |
Output is correct |
59 |
Correct |
2 ms |
5208 KB |
Output is correct |
60 |
Correct |
2 ms |
5212 KB |
Output is correct |
61 |
Correct |
2 ms |
5212 KB |
Output is correct |
62 |
Correct |
3 ms |
5212 KB |
Output is correct |
63 |
Correct |
3 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
5180 KB |
Output is correct |
8 |
Correct |
3 ms |
5044 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
4 ms |
5184 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
4 ms |
5260 KB |
Output is correct |
24 |
Correct |
4 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5188 KB |
Output is correct |
26 |
Correct |
4 ms |
5208 KB |
Output is correct |
27 |
Correct |
4 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5208 KB |
Output is correct |
30 |
Correct |
3 ms |
5184 KB |
Output is correct |
31 |
Correct |
4 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
2 ms |
5224 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
4 ms |
5280 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
3 ms |
5212 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
39 |
Correct |
3 ms |
5212 KB |
Output is correct |
40 |
Correct |
2 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
2 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
3 ms |
5208 KB |
Output is correct |
47 |
Correct |
3 ms |
5212 KB |
Output is correct |
48 |
Correct |
3 ms |
5212 KB |
Output is correct |
49 |
Correct |
2 ms |
5212 KB |
Output is correct |
50 |
Correct |
3 ms |
5212 KB |
Output is correct |
51 |
Correct |
2 ms |
5208 KB |
Output is correct |
52 |
Correct |
2 ms |
5212 KB |
Output is correct |
53 |
Correct |
2 ms |
5212 KB |
Output is correct |
54 |
Correct |
2 ms |
5212 KB |
Output is correct |
55 |
Correct |
2 ms |
5212 KB |
Output is correct |
56 |
Correct |
2 ms |
5212 KB |
Output is correct |
57 |
Correct |
2 ms |
5212 KB |
Output is correct |
58 |
Correct |
2 ms |
5464 KB |
Output is correct |
59 |
Correct |
2 ms |
5208 KB |
Output is correct |
60 |
Correct |
2 ms |
5212 KB |
Output is correct |
61 |
Correct |
2 ms |
5212 KB |
Output is correct |
62 |
Correct |
3 ms |
5212 KB |
Output is correct |
63 |
Correct |
3 ms |
5212 KB |
Output is correct |
64 |
Correct |
57 ms |
16080 KB |
Output is correct |
65 |
Correct |
62 ms |
16076 KB |
Output is correct |
66 |
Correct |
89 ms |
16092 KB |
Output is correct |
67 |
Correct |
69 ms |
16080 KB |
Output is correct |
68 |
Correct |
57 ms |
16056 KB |
Output is correct |
69 |
Correct |
59 ms |
16108 KB |
Output is correct |
70 |
Correct |
60 ms |
16072 KB |
Output is correct |
71 |
Correct |
55 ms |
15940 KB |
Output is correct |
72 |
Correct |
82 ms |
16072 KB |
Output is correct |
73 |
Correct |
65 ms |
28620 KB |
Output is correct |
74 |
Correct |
79 ms |
28656 KB |
Output is correct |
75 |
Correct |
55 ms |
15884 KB |
Output is correct |
76 |
Correct |
75 ms |
15836 KB |
Output is correct |
77 |
Correct |
59 ms |
15820 KB |
Output is correct |
78 |
Correct |
53 ms |
15812 KB |
Output is correct |
79 |
Correct |
55 ms |
15828 KB |
Output is correct |
80 |
Correct |
55 ms |
15848 KB |
Output is correct |
81 |
Correct |
47 ms |
16312 KB |
Output is correct |
82 |
Correct |
50 ms |
16008 KB |
Output is correct |
83 |
Correct |
85 ms |
21200 KB |
Output is correct |
84 |
Correct |
65 ms |
23192 KB |
Output is correct |
85 |
Correct |
62 ms |
23004 KB |
Output is correct |
86 |
Correct |
54 ms |
17360 KB |
Output is correct |
87 |
Correct |
55 ms |
16844 KB |
Output is correct |
88 |
Correct |
57 ms |
17284 KB |
Output is correct |
89 |
Correct |
56 ms |
17112 KB |
Output is correct |
90 |
Correct |
57 ms |
16944 KB |
Output is correct |
91 |
Correct |
58 ms |
17144 KB |
Output is correct |
92 |
Correct |
66 ms |
17564 KB |
Output is correct |
93 |
Correct |
54 ms |
19872 KB |
Output is correct |
94 |
Correct |
82 ms |
21652 KB |
Output is correct |
95 |
Correct |
99 ms |
20432 KB |
Output is correct |
96 |
Correct |
60 ms |
18540 KB |
Output is correct |
97 |
Correct |
54 ms |
16932 KB |
Output is correct |
98 |
Correct |
53 ms |
16636 KB |
Output is correct |
99 |
Correct |
58 ms |
16336 KB |
Output is correct |
100 |
Correct |
58 ms |
19640 KB |
Output is correct |
101 |
Correct |
59 ms |
17480 KB |
Output is correct |
102 |
Correct |
55 ms |
16852 KB |
Output is correct |
103 |
Correct |
56 ms |
16620 KB |
Output is correct |
104 |
Correct |
41 ms |
17172 KB |
Output is correct |
105 |
Correct |
68 ms |
25292 KB |
Output is correct |
106 |
Correct |
38 ms |
16836 KB |
Output is correct |
107 |
Correct |
39 ms |
16840 KB |
Output is correct |
108 |
Correct |
79 ms |
29388 KB |
Output is correct |
109 |
Correct |
42 ms |
16988 KB |
Output is correct |
110 |
Correct |
80 ms |
29860 KB |
Output is correct |
111 |
Correct |
54 ms |
15848 KB |
Output is correct |