# include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
using namespace std;
const int N=3e5+5;
long long par[N],dp[N],dp1[N],n,a,b,bb,ans;
vector <long long> v[N];
string s;
void dfs (int a, int p) {
int ff=0;
par[a]=p;
for (int i=0; i<v[a].size(); i++) {
if (v[a][i]!=p) {
dfs(v[a][i],a);
dp[a]+=dp[v[a][i]];
if (s[a]=='1' && !ff) dp[a]--, ff=1;
dp1[a]+=dp1[v[a][i]];
}
}
dp1[a]++;
if (s[a]=='1') dp[a]=max(1LL, dp[a]);
if (s[a]=='0') dp[a]=max(0LL,dp[a]);
}
int main() {
cin>>n;
for (int i=1; i<=n-1; i++) {
cin>>a>>b;
v[a].pb(b);
v[b].pb(a);
}
cin>>s;
s="@"+s;
dfs(1,0);
for (int i=1; i<=n; i++) {
if (par[i] && s[par[i]]=='1') bb=1; else bb=0;
ans=max(ans,dp[i]+bb);
}
cout<<ans<<endl;
}
Compilation message
power.cpp: In function 'void dfs(int, int)':
power.cpp:13:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for (int i=0; i<v[a].size(); i++) {
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7248 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7244 KB |
Output is correct |
8 |
Correct |
5 ms |
7268 KB |
Output is correct |
9 |
Correct |
5 ms |
7248 KB |
Output is correct |
10 |
Correct |
5 ms |
7284 KB |
Output is correct |
11 |
Correct |
5 ms |
7356 KB |
Output is correct |
12 |
Correct |
5 ms |
7244 KB |
Output is correct |
13 |
Correct |
5 ms |
7244 KB |
Output is correct |
14 |
Correct |
5 ms |
7244 KB |
Output is correct |
15 |
Correct |
5 ms |
7244 KB |
Output is correct |
16 |
Correct |
6 ms |
7372 KB |
Output is correct |
17 |
Correct |
5 ms |
7348 KB |
Output is correct |
18 |
Correct |
5 ms |
7244 KB |
Output is correct |
19 |
Correct |
5 ms |
7372 KB |
Output is correct |
20 |
Correct |
5 ms |
7372 KB |
Output is correct |
21 |
Correct |
5 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7248 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7244 KB |
Output is correct |
8 |
Correct |
5 ms |
7268 KB |
Output is correct |
9 |
Correct |
5 ms |
7248 KB |
Output is correct |
10 |
Correct |
5 ms |
7284 KB |
Output is correct |
11 |
Correct |
5 ms |
7356 KB |
Output is correct |
12 |
Correct |
5 ms |
7244 KB |
Output is correct |
13 |
Correct |
5 ms |
7244 KB |
Output is correct |
14 |
Correct |
5 ms |
7244 KB |
Output is correct |
15 |
Correct |
5 ms |
7244 KB |
Output is correct |
16 |
Correct |
6 ms |
7372 KB |
Output is correct |
17 |
Correct |
5 ms |
7348 KB |
Output is correct |
18 |
Correct |
5 ms |
7244 KB |
Output is correct |
19 |
Correct |
5 ms |
7372 KB |
Output is correct |
20 |
Correct |
5 ms |
7372 KB |
Output is correct |
21 |
Correct |
5 ms |
7372 KB |
Output is correct |
22 |
Correct |
6 ms |
7512 KB |
Output is correct |
23 |
Correct |
6 ms |
7500 KB |
Output is correct |
24 |
Correct |
6 ms |
7408 KB |
Output is correct |
25 |
Correct |
7 ms |
7500 KB |
Output is correct |
26 |
Correct |
6 ms |
7500 KB |
Output is correct |
27 |
Correct |
6 ms |
7500 KB |
Output is correct |
28 |
Correct |
7 ms |
7492 KB |
Output is correct |
29 |
Correct |
6 ms |
7624 KB |
Output is correct |
30 |
Correct |
7 ms |
7552 KB |
Output is correct |
31 |
Correct |
7 ms |
7488 KB |
Output is correct |
32 |
Correct |
6 ms |
7500 KB |
Output is correct |
33 |
Correct |
6 ms |
7496 KB |
Output is correct |
34 |
Correct |
6 ms |
7492 KB |
Output is correct |
35 |
Correct |
6 ms |
7500 KB |
Output is correct |
36 |
Correct |
6 ms |
7500 KB |
Output is correct |
37 |
Correct |
6 ms |
7500 KB |
Output is correct |
38 |
Correct |
7 ms |
7500 KB |
Output is correct |
39 |
Correct |
6 ms |
7500 KB |
Output is correct |
40 |
Correct |
7 ms |
7488 KB |
Output is correct |
41 |
Correct |
7 ms |
7500 KB |
Output is correct |
42 |
Correct |
7 ms |
7488 KB |
Output is correct |
43 |
Correct |
6 ms |
7500 KB |
Output is correct |
44 |
Correct |
6 ms |
7500 KB |
Output is correct |
45 |
Correct |
6 ms |
7500 KB |
Output is correct |
46 |
Correct |
7 ms |
7500 KB |
Output is correct |
47 |
Correct |
6 ms |
7500 KB |
Output is correct |
48 |
Correct |
6 ms |
7500 KB |
Output is correct |
49 |
Correct |
7 ms |
7504 KB |
Output is correct |
50 |
Correct |
7 ms |
7500 KB |
Output is correct |
51 |
Correct |
6 ms |
7496 KB |
Output is correct |
52 |
Correct |
7 ms |
7480 KB |
Output is correct |
53 |
Correct |
6 ms |
7512 KB |
Output is correct |
54 |
Correct |
7 ms |
7496 KB |
Output is correct |
55 |
Correct |
7 ms |
7504 KB |
Output is correct |
56 |
Correct |
6 ms |
7488 KB |
Output is correct |
57 |
Correct |
6 ms |
7632 KB |
Output is correct |
58 |
Correct |
6 ms |
7508 KB |
Output is correct |
59 |
Correct |
7 ms |
7512 KB |
Output is correct |
60 |
Correct |
7 ms |
7508 KB |
Output is correct |
61 |
Correct |
7 ms |
7504 KB |
Output is correct |
62 |
Correct |
6 ms |
7496 KB |
Output is correct |
63 |
Correct |
6 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7248 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7244 KB |
Output is correct |
8 |
Correct |
5 ms |
7268 KB |
Output is correct |
9 |
Correct |
5 ms |
7248 KB |
Output is correct |
10 |
Correct |
5 ms |
7284 KB |
Output is correct |
11 |
Correct |
5 ms |
7356 KB |
Output is correct |
12 |
Correct |
5 ms |
7244 KB |
Output is correct |
13 |
Correct |
5 ms |
7244 KB |
Output is correct |
14 |
Correct |
5 ms |
7244 KB |
Output is correct |
15 |
Correct |
5 ms |
7244 KB |
Output is correct |
16 |
Correct |
6 ms |
7372 KB |
Output is correct |
17 |
Correct |
5 ms |
7348 KB |
Output is correct |
18 |
Correct |
5 ms |
7244 KB |
Output is correct |
19 |
Correct |
5 ms |
7372 KB |
Output is correct |
20 |
Correct |
5 ms |
7372 KB |
Output is correct |
21 |
Correct |
5 ms |
7372 KB |
Output is correct |
22 |
Correct |
6 ms |
7512 KB |
Output is correct |
23 |
Correct |
6 ms |
7500 KB |
Output is correct |
24 |
Correct |
6 ms |
7408 KB |
Output is correct |
25 |
Correct |
7 ms |
7500 KB |
Output is correct |
26 |
Correct |
6 ms |
7500 KB |
Output is correct |
27 |
Correct |
6 ms |
7500 KB |
Output is correct |
28 |
Correct |
7 ms |
7492 KB |
Output is correct |
29 |
Correct |
6 ms |
7624 KB |
Output is correct |
30 |
Correct |
7 ms |
7552 KB |
Output is correct |
31 |
Correct |
7 ms |
7488 KB |
Output is correct |
32 |
Correct |
6 ms |
7500 KB |
Output is correct |
33 |
Correct |
6 ms |
7496 KB |
Output is correct |
34 |
Correct |
6 ms |
7492 KB |
Output is correct |
35 |
Correct |
6 ms |
7500 KB |
Output is correct |
36 |
Correct |
6 ms |
7500 KB |
Output is correct |
37 |
Correct |
6 ms |
7500 KB |
Output is correct |
38 |
Correct |
7 ms |
7500 KB |
Output is correct |
39 |
Correct |
6 ms |
7500 KB |
Output is correct |
40 |
Correct |
7 ms |
7488 KB |
Output is correct |
41 |
Correct |
7 ms |
7500 KB |
Output is correct |
42 |
Correct |
7 ms |
7488 KB |
Output is correct |
43 |
Correct |
6 ms |
7500 KB |
Output is correct |
44 |
Correct |
6 ms |
7500 KB |
Output is correct |
45 |
Correct |
6 ms |
7500 KB |
Output is correct |
46 |
Correct |
7 ms |
7500 KB |
Output is correct |
47 |
Correct |
6 ms |
7500 KB |
Output is correct |
48 |
Correct |
6 ms |
7500 KB |
Output is correct |
49 |
Correct |
7 ms |
7504 KB |
Output is correct |
50 |
Correct |
7 ms |
7500 KB |
Output is correct |
51 |
Correct |
6 ms |
7496 KB |
Output is correct |
52 |
Correct |
7 ms |
7480 KB |
Output is correct |
53 |
Correct |
6 ms |
7512 KB |
Output is correct |
54 |
Correct |
7 ms |
7496 KB |
Output is correct |
55 |
Correct |
7 ms |
7504 KB |
Output is correct |
56 |
Correct |
6 ms |
7488 KB |
Output is correct |
57 |
Correct |
6 ms |
7632 KB |
Output is correct |
58 |
Correct |
6 ms |
7508 KB |
Output is correct |
59 |
Correct |
7 ms |
7512 KB |
Output is correct |
60 |
Correct |
7 ms |
7508 KB |
Output is correct |
61 |
Correct |
7 ms |
7504 KB |
Output is correct |
62 |
Correct |
6 ms |
7496 KB |
Output is correct |
63 |
Correct |
6 ms |
7512 KB |
Output is correct |
64 |
Correct |
268 ms |
22336 KB |
Output is correct |
65 |
Correct |
262 ms |
22380 KB |
Output is correct |
66 |
Correct |
268 ms |
22488 KB |
Output is correct |
67 |
Correct |
253 ms |
22552 KB |
Output is correct |
68 |
Correct |
259 ms |
22364 KB |
Output is correct |
69 |
Correct |
267 ms |
22348 KB |
Output is correct |
70 |
Correct |
262 ms |
22356 KB |
Output is correct |
71 |
Correct |
262 ms |
22412 KB |
Output is correct |
72 |
Correct |
274 ms |
22360 KB |
Output is correct |
73 |
Correct |
286 ms |
33996 KB |
Output is correct |
74 |
Correct |
277 ms |
33996 KB |
Output is correct |
75 |
Correct |
252 ms |
23564 KB |
Output is correct |
76 |
Correct |
257 ms |
23660 KB |
Output is correct |
77 |
Correct |
259 ms |
22344 KB |
Output is correct |
78 |
Correct |
288 ms |
22388 KB |
Output is correct |
79 |
Correct |
261 ms |
22508 KB |
Output is correct |
80 |
Correct |
272 ms |
22396 KB |
Output is correct |
81 |
Correct |
233 ms |
22452 KB |
Output is correct |
82 |
Correct |
245 ms |
22380 KB |
Output is correct |
83 |
Correct |
297 ms |
27412 KB |
Output is correct |
84 |
Correct |
272 ms |
29564 KB |
Output is correct |
85 |
Correct |
264 ms |
29328 KB |
Output is correct |
86 |
Correct |
266 ms |
23664 KB |
Output is correct |
87 |
Correct |
263 ms |
23184 KB |
Output is correct |
88 |
Correct |
255 ms |
23184 KB |
Output is correct |
89 |
Correct |
270 ms |
23312 KB |
Output is correct |
90 |
Correct |
252 ms |
23108 KB |
Output is correct |
91 |
Correct |
290 ms |
23324 KB |
Output is correct |
92 |
Correct |
250 ms |
23692 KB |
Output is correct |
93 |
Correct |
247 ms |
25916 KB |
Output is correct |
94 |
Correct |
240 ms |
27472 KB |
Output is correct |
95 |
Correct |
246 ms |
26296 KB |
Output is correct |
96 |
Correct |
239 ms |
24432 KB |
Output is correct |
97 |
Correct |
237 ms |
22936 KB |
Output is correct |
98 |
Correct |
252 ms |
22540 KB |
Output is correct |
99 |
Correct |
246 ms |
22440 KB |
Output is correct |
100 |
Correct |
250 ms |
25472 KB |
Output is correct |
101 |
Correct |
255 ms |
23436 KB |
Output is correct |
102 |
Correct |
239 ms |
22628 KB |
Output is correct |
103 |
Correct |
253 ms |
22644 KB |
Output is correct |
104 |
Correct |
196 ms |
23092 KB |
Output is correct |
105 |
Correct |
280 ms |
30868 KB |
Output is correct |
106 |
Correct |
187 ms |
22936 KB |
Output is correct |
107 |
Correct |
187 ms |
22928 KB |
Output is correct |
108 |
Correct |
274 ms |
34704 KB |
Output is correct |
109 |
Correct |
190 ms |
23088 KB |
Output is correct |
110 |
Correct |
271 ms |
35132 KB |
Output is correct |
111 |
Correct |
252 ms |
22288 KB |
Output is correct |