#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back
//#define ll __int128
#define ll long long
#define int long long
#define LEFT(a) ((a)<<1)
#define RIGHT(a) (LEFT(a) + 1)
#define MID(a,b) ((a+b)>>1)
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define y1 y122
/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
/*
#pragma GCC optimize ("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target ("avx2")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/STACK: 20000000005")
*/
using namespace std;
const int N = 200005;
int n, ans;
int c[N];
int dp[N], DP[N];
vector < int > g[N];
void dfs (int k, int p){
for (int to : g[k]){
if (to == p)
continue;
dfs (to, k);
dp[k] += dp[to];
}
dp[k] = max (dp[k] - c[k], c[k]);
}
void go (int k, int p, int X){
int sum = 0;
ans = max (ans, c[k] + X);
for (int to : g[k]){
if (to == p)
continue;
sum += dp[to];
ans = max (ans, c[k] + dp[to]);
}
sum += X;
ans = max (ans, sum - c[k]);
for (int to : g[k]){
if (to == p)
continue;
sum -= dp[to];
int mx = max (c[k], sum - c[k]);
go (to, k, mx);
sum += dp[to];
}
}
main()
{
//freopen ("in.in", "r", stdin);freopen ("out.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin >> n;
for (int u, v, i = 1; i < n; i++){
cin >> u >> v;
g[u].pb (v);
g[v].pb (u);
}
char ch;
for (int i = 1; i <= n; i++){
cin >> ch;
c[i] = ch - '0';
c[0] += c[i];
}
if (c[0] >= 2)
ans = 2;
else
ans = c[0];
dfs (1, 0);
go (1, 0, 0);
cout << ans << endl;
}
Compilation message
power.cpp:19:1: warning: "/*" within comment [-Wcomment]
19 | /*
|
power.cpp:71:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
71 | main()
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
5 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
5 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
4992 KB |
Output is correct |
22 |
Correct |
5 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
5 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
5 ms |
5248 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
5 ms |
5248 KB |
Output is correct |
33 |
Correct |
5 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
5 ms |
5248 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
5 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
39 |
Correct |
5 ms |
5248 KB |
Output is correct |
40 |
Correct |
5 ms |
5248 KB |
Output is correct |
41 |
Correct |
5 ms |
5248 KB |
Output is correct |
42 |
Correct |
5 ms |
5248 KB |
Output is correct |
43 |
Correct |
5 ms |
5120 KB |
Output is correct |
44 |
Correct |
5 ms |
5376 KB |
Output is correct |
45 |
Correct |
4 ms |
5248 KB |
Output is correct |
46 |
Correct |
5 ms |
5120 KB |
Output is correct |
47 |
Correct |
4 ms |
5120 KB |
Output is correct |
48 |
Correct |
5 ms |
5248 KB |
Output is correct |
49 |
Correct |
5 ms |
5248 KB |
Output is correct |
50 |
Correct |
5 ms |
5248 KB |
Output is correct |
51 |
Correct |
5 ms |
5248 KB |
Output is correct |
52 |
Correct |
5 ms |
5120 KB |
Output is correct |
53 |
Correct |
5 ms |
5248 KB |
Output is correct |
54 |
Correct |
5 ms |
5248 KB |
Output is correct |
55 |
Correct |
5 ms |
5248 KB |
Output is correct |
56 |
Correct |
4 ms |
5120 KB |
Output is correct |
57 |
Correct |
5 ms |
5248 KB |
Output is correct |
58 |
Correct |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
4 ms |
5120 KB |
Output is correct |
60 |
Correct |
5 ms |
5248 KB |
Output is correct |
61 |
Correct |
5 ms |
5248 KB |
Output is correct |
62 |
Correct |
5 ms |
5376 KB |
Output is correct |
63 |
Correct |
5 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
5 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
4992 KB |
Output is correct |
22 |
Correct |
5 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
5 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
5 ms |
5248 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
5 ms |
5248 KB |
Output is correct |
33 |
Correct |
5 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
5 ms |
5248 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
5 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
39 |
Correct |
5 ms |
5248 KB |
Output is correct |
40 |
Correct |
5 ms |
5248 KB |
Output is correct |
41 |
Correct |
5 ms |
5248 KB |
Output is correct |
42 |
Correct |
5 ms |
5248 KB |
Output is correct |
43 |
Correct |
5 ms |
5120 KB |
Output is correct |
44 |
Correct |
5 ms |
5376 KB |
Output is correct |
45 |
Correct |
4 ms |
5248 KB |
Output is correct |
46 |
Correct |
5 ms |
5120 KB |
Output is correct |
47 |
Correct |
4 ms |
5120 KB |
Output is correct |
48 |
Correct |
5 ms |
5248 KB |
Output is correct |
49 |
Correct |
5 ms |
5248 KB |
Output is correct |
50 |
Correct |
5 ms |
5248 KB |
Output is correct |
51 |
Correct |
5 ms |
5248 KB |
Output is correct |
52 |
Correct |
5 ms |
5120 KB |
Output is correct |
53 |
Correct |
5 ms |
5248 KB |
Output is correct |
54 |
Correct |
5 ms |
5248 KB |
Output is correct |
55 |
Correct |
5 ms |
5248 KB |
Output is correct |
56 |
Correct |
4 ms |
5120 KB |
Output is correct |
57 |
Correct |
5 ms |
5248 KB |
Output is correct |
58 |
Correct |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
4 ms |
5120 KB |
Output is correct |
60 |
Correct |
5 ms |
5248 KB |
Output is correct |
61 |
Correct |
5 ms |
5248 KB |
Output is correct |
62 |
Correct |
5 ms |
5376 KB |
Output is correct |
63 |
Correct |
5 ms |
5120 KB |
Output is correct |
64 |
Correct |
217 ms |
18232 KB |
Output is correct |
65 |
Correct |
215 ms |
18168 KB |
Output is correct |
66 |
Correct |
218 ms |
18168 KB |
Output is correct |
67 |
Correct |
217 ms |
18172 KB |
Output is correct |
68 |
Correct |
215 ms |
18168 KB |
Output is correct |
69 |
Correct |
213 ms |
18168 KB |
Output is correct |
70 |
Correct |
218 ms |
18168 KB |
Output is correct |
71 |
Correct |
216 ms |
18296 KB |
Output is correct |
72 |
Correct |
216 ms |
18168 KB |
Output is correct |
73 |
Correct |
244 ms |
27260 KB |
Output is correct |
74 |
Correct |
248 ms |
27256 KB |
Output is correct |
75 |
Correct |
217 ms |
19448 KB |
Output is correct |
76 |
Correct |
237 ms |
19576 KB |
Output is correct |
77 |
Correct |
226 ms |
18168 KB |
Output is correct |
78 |
Correct |
264 ms |
18296 KB |
Output is correct |
79 |
Correct |
229 ms |
18168 KB |
Output is correct |
80 |
Correct |
225 ms |
18168 KB |
Output is correct |
81 |
Correct |
177 ms |
18284 KB |
Output is correct |
82 |
Correct |
201 ms |
18160 KB |
Output is correct |
83 |
Correct |
233 ms |
22392 KB |
Output is correct |
84 |
Correct |
233 ms |
23928 KB |
Output is correct |
85 |
Correct |
233 ms |
23672 KB |
Output is correct |
86 |
Correct |
232 ms |
19320 KB |
Output is correct |
87 |
Correct |
235 ms |
18936 KB |
Output is correct |
88 |
Correct |
216 ms |
18936 KB |
Output is correct |
89 |
Correct |
218 ms |
18936 KB |
Output is correct |
90 |
Correct |
212 ms |
18808 KB |
Output is correct |
91 |
Correct |
246 ms |
18936 KB |
Output is correct |
92 |
Correct |
211 ms |
19232 KB |
Output is correct |
93 |
Correct |
220 ms |
20844 KB |
Output is correct |
94 |
Correct |
194 ms |
22124 KB |
Output is correct |
95 |
Correct |
194 ms |
21224 KB |
Output is correct |
96 |
Correct |
204 ms |
19880 KB |
Output is correct |
97 |
Correct |
210 ms |
18680 KB |
Output is correct |
98 |
Correct |
206 ms |
18452 KB |
Output is correct |
99 |
Correct |
211 ms |
18296 KB |
Output is correct |
100 |
Correct |
211 ms |
20600 KB |
Output is correct |
101 |
Correct |
200 ms |
18936 KB |
Output is correct |
102 |
Correct |
203 ms |
18424 KB |
Output is correct |
103 |
Correct |
216 ms |
18448 KB |
Output is correct |
104 |
Correct |
125 ms |
18832 KB |
Output is correct |
105 |
Correct |
247 ms |
24824 KB |
Output is correct |
106 |
Correct |
130 ms |
18660 KB |
Output is correct |
107 |
Correct |
122 ms |
18660 KB |
Output is correct |
108 |
Correct |
250 ms |
27948 KB |
Output is correct |
109 |
Correct |
120 ms |
18820 KB |
Output is correct |
110 |
Correct |
246 ms |
28280 KB |
Output is correct |
111 |
Correct |
212 ms |
18168 KB |
Output is correct |