//#i_love_aisukiuwu#9998
#include <bits/stdc++.h>
using namespace std;
void open(){
if(fopen("i.inp","r")){
freopen("i.inp","r",stdin);
freopen("o.out","w",stdout);
}
}
#define ll long long
#define all(a) (a).begin(), (a).end()
#define vi vector<ll>
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define fi first
#define se second
#define gcd __gcd
#define mset(a,v) memset(a, v, sizeof(a))
#define endl '\n'
#define spc " "
const int MN1 = 1e6 + 5,MN2 = 1e4 + 5,LOG = 27;
const ll MOD = 1e9 + 7,INF = 1e9;
ll n,dp[MN1],res = -INF;
bool power[MN1];
vector<ll>adj[MN1];
//dp[u] la max profit tai subtree u
//dp[u] = max(power[u],sum_{v = G[u]} dp[v] - power[u])
void dfs(ll u,ll pre){
ll sum = 0,mx = 0;
dp[u] = 1;
for(ll v : adj[u]){
if(v == pre) continue;
dfs(v,u);
sum += dp[v];
mx = max(mx,dp[v]);
}
dp[u] = max((ll)power[u],sum - power[u]);
res = max({res,sum - power[u],mx + power[u]});
}
void solve(){
cin>>n;
for(ll i = 1;i < n;++i){
ll u,v;cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(ll i = 1;i <= n;++i){
char c; cin>>c;
power[i] = (c == '1');
}
dfs(1,1);
cout<<res;
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
open();
ll t = 1; //cin>>t;
while(t--) solve();
cerr << "\n" << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
Compilation message
power.cpp: In function 'void open()':
power.cpp:8:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | freopen("i.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
power.cpp:9:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | freopen("o.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23716 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23772 KB |
Output is correct |
9 |
Correct |
11 ms |
23704 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23696 KB |
Output is correct |
13 |
Correct |
11 ms |
23740 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23764 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
19 |
Correct |
11 ms |
23820 KB |
Output is correct |
20 |
Correct |
13 ms |
23764 KB |
Output is correct |
21 |
Correct |
11 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23716 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23772 KB |
Output is correct |
9 |
Correct |
11 ms |
23704 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23696 KB |
Output is correct |
13 |
Correct |
11 ms |
23740 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23764 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
19 |
Correct |
11 ms |
23820 KB |
Output is correct |
20 |
Correct |
13 ms |
23764 KB |
Output is correct |
21 |
Correct |
11 ms |
23764 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
12 ms |
23844 KB |
Output is correct |
24 |
Correct |
11 ms |
23864 KB |
Output is correct |
25 |
Correct |
11 ms |
23864 KB |
Output is correct |
26 |
Correct |
11 ms |
23864 KB |
Output is correct |
27 |
Correct |
12 ms |
23884 KB |
Output is correct |
28 |
Correct |
12 ms |
23804 KB |
Output is correct |
29 |
Correct |
13 ms |
24020 KB |
Output is correct |
30 |
Correct |
12 ms |
23892 KB |
Output is correct |
31 |
Correct |
11 ms |
23892 KB |
Output is correct |
32 |
Correct |
11 ms |
23892 KB |
Output is correct |
33 |
Correct |
11 ms |
23892 KB |
Output is correct |
34 |
Correct |
12 ms |
23892 KB |
Output is correct |
35 |
Correct |
14 ms |
23892 KB |
Output is correct |
36 |
Correct |
14 ms |
23888 KB |
Output is correct |
37 |
Correct |
12 ms |
23892 KB |
Output is correct |
38 |
Correct |
12 ms |
23796 KB |
Output is correct |
39 |
Correct |
11 ms |
23892 KB |
Output is correct |
40 |
Correct |
12 ms |
23892 KB |
Output is correct |
41 |
Correct |
12 ms |
23868 KB |
Output is correct |
42 |
Correct |
12 ms |
23892 KB |
Output is correct |
43 |
Correct |
11 ms |
23892 KB |
Output is correct |
44 |
Correct |
12 ms |
23892 KB |
Output is correct |
45 |
Correct |
11 ms |
23828 KB |
Output is correct |
46 |
Correct |
12 ms |
23892 KB |
Output is correct |
47 |
Correct |
12 ms |
23892 KB |
Output is correct |
48 |
Correct |
12 ms |
23880 KB |
Output is correct |
49 |
Correct |
12 ms |
23864 KB |
Output is correct |
50 |
Correct |
12 ms |
23892 KB |
Output is correct |
51 |
Correct |
11 ms |
23932 KB |
Output is correct |
52 |
Correct |
11 ms |
23892 KB |
Output is correct |
53 |
Correct |
11 ms |
23892 KB |
Output is correct |
54 |
Correct |
13 ms |
23868 KB |
Output is correct |
55 |
Correct |
13 ms |
23824 KB |
Output is correct |
56 |
Correct |
12 ms |
23892 KB |
Output is correct |
57 |
Correct |
12 ms |
23964 KB |
Output is correct |
58 |
Correct |
11 ms |
23892 KB |
Output is correct |
59 |
Correct |
11 ms |
23892 KB |
Output is correct |
60 |
Correct |
14 ms |
23892 KB |
Output is correct |
61 |
Correct |
12 ms |
23808 KB |
Output is correct |
62 |
Correct |
11 ms |
23892 KB |
Output is correct |
63 |
Correct |
12 ms |
23892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23716 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23772 KB |
Output is correct |
9 |
Correct |
11 ms |
23704 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23696 KB |
Output is correct |
13 |
Correct |
11 ms |
23740 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23764 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
19 |
Correct |
11 ms |
23820 KB |
Output is correct |
20 |
Correct |
13 ms |
23764 KB |
Output is correct |
21 |
Correct |
11 ms |
23764 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
12 ms |
23844 KB |
Output is correct |
24 |
Correct |
11 ms |
23864 KB |
Output is correct |
25 |
Correct |
11 ms |
23864 KB |
Output is correct |
26 |
Correct |
11 ms |
23864 KB |
Output is correct |
27 |
Correct |
12 ms |
23884 KB |
Output is correct |
28 |
Correct |
12 ms |
23804 KB |
Output is correct |
29 |
Correct |
13 ms |
24020 KB |
Output is correct |
30 |
Correct |
12 ms |
23892 KB |
Output is correct |
31 |
Correct |
11 ms |
23892 KB |
Output is correct |
32 |
Correct |
11 ms |
23892 KB |
Output is correct |
33 |
Correct |
11 ms |
23892 KB |
Output is correct |
34 |
Correct |
12 ms |
23892 KB |
Output is correct |
35 |
Correct |
14 ms |
23892 KB |
Output is correct |
36 |
Correct |
14 ms |
23888 KB |
Output is correct |
37 |
Correct |
12 ms |
23892 KB |
Output is correct |
38 |
Correct |
12 ms |
23796 KB |
Output is correct |
39 |
Correct |
11 ms |
23892 KB |
Output is correct |
40 |
Correct |
12 ms |
23892 KB |
Output is correct |
41 |
Correct |
12 ms |
23868 KB |
Output is correct |
42 |
Correct |
12 ms |
23892 KB |
Output is correct |
43 |
Correct |
11 ms |
23892 KB |
Output is correct |
44 |
Correct |
12 ms |
23892 KB |
Output is correct |
45 |
Correct |
11 ms |
23828 KB |
Output is correct |
46 |
Correct |
12 ms |
23892 KB |
Output is correct |
47 |
Correct |
12 ms |
23892 KB |
Output is correct |
48 |
Correct |
12 ms |
23880 KB |
Output is correct |
49 |
Correct |
12 ms |
23864 KB |
Output is correct |
50 |
Correct |
12 ms |
23892 KB |
Output is correct |
51 |
Correct |
11 ms |
23932 KB |
Output is correct |
52 |
Correct |
11 ms |
23892 KB |
Output is correct |
53 |
Correct |
11 ms |
23892 KB |
Output is correct |
54 |
Correct |
13 ms |
23868 KB |
Output is correct |
55 |
Correct |
13 ms |
23824 KB |
Output is correct |
56 |
Correct |
12 ms |
23892 KB |
Output is correct |
57 |
Correct |
12 ms |
23964 KB |
Output is correct |
58 |
Correct |
11 ms |
23892 KB |
Output is correct |
59 |
Correct |
11 ms |
23892 KB |
Output is correct |
60 |
Correct |
14 ms |
23892 KB |
Output is correct |
61 |
Correct |
12 ms |
23808 KB |
Output is correct |
62 |
Correct |
11 ms |
23892 KB |
Output is correct |
63 |
Correct |
12 ms |
23892 KB |
Output is correct |
64 |
Correct |
82 ms |
32832 KB |
Output is correct |
65 |
Correct |
82 ms |
32912 KB |
Output is correct |
66 |
Correct |
86 ms |
32880 KB |
Output is correct |
67 |
Correct |
82 ms |
32884 KB |
Output is correct |
68 |
Correct |
86 ms |
32828 KB |
Output is correct |
69 |
Correct |
81 ms |
32824 KB |
Output is correct |
70 |
Correct |
83 ms |
32792 KB |
Output is correct |
71 |
Correct |
85 ms |
32844 KB |
Output is correct |
72 |
Correct |
101 ms |
32828 KB |
Output is correct |
73 |
Correct |
140 ms |
44484 KB |
Output is correct |
74 |
Correct |
99 ms |
44492 KB |
Output is correct |
75 |
Correct |
97 ms |
34100 KB |
Output is correct |
76 |
Correct |
101 ms |
34148 KB |
Output is correct |
77 |
Correct |
115 ms |
32820 KB |
Output is correct |
78 |
Correct |
81 ms |
32788 KB |
Output is correct |
79 |
Correct |
80 ms |
32848 KB |
Output is correct |
80 |
Correct |
90 ms |
32864 KB |
Output is correct |
81 |
Correct |
73 ms |
32876 KB |
Output is correct |
82 |
Correct |
94 ms |
32920 KB |
Output is correct |
83 |
Correct |
105 ms |
38012 KB |
Output is correct |
84 |
Correct |
94 ms |
40036 KB |
Output is correct |
85 |
Correct |
94 ms |
39780 KB |
Output is correct |
86 |
Correct |
83 ms |
34184 KB |
Output is correct |
87 |
Correct |
88 ms |
33596 KB |
Output is correct |
88 |
Correct |
81 ms |
33740 KB |
Output is correct |
89 |
Correct |
85 ms |
33740 KB |
Output is correct |
90 |
Correct |
98 ms |
33580 KB |
Output is correct |
91 |
Correct |
111 ms |
33736 KB |
Output is correct |
92 |
Correct |
82 ms |
34124 KB |
Output is correct |
93 |
Correct |
99 ms |
36316 KB |
Output is correct |
94 |
Correct |
80 ms |
38028 KB |
Output is correct |
95 |
Correct |
83 ms |
36672 KB |
Output is correct |
96 |
Correct |
82 ms |
34916 KB |
Output is correct |
97 |
Correct |
85 ms |
33388 KB |
Output is correct |
98 |
Correct |
106 ms |
33028 KB |
Output is correct |
99 |
Correct |
78 ms |
32884 KB |
Output is correct |
100 |
Correct |
83 ms |
35884 KB |
Output is correct |
101 |
Correct |
90 ms |
33724 KB |
Output is correct |
102 |
Correct |
79 ms |
33168 KB |
Output is correct |
103 |
Correct |
81 ms |
33124 KB |
Output is correct |
104 |
Correct |
61 ms |
33356 KB |
Output is correct |
105 |
Correct |
99 ms |
41356 KB |
Output is correct |
106 |
Correct |
61 ms |
33388 KB |
Output is correct |
107 |
Correct |
60 ms |
33352 KB |
Output is correct |
108 |
Correct |
102 ms |
45276 KB |
Output is correct |
109 |
Correct |
61 ms |
33380 KB |
Output is correct |
110 |
Correct |
125 ms |
45684 KB |
Output is correct |
111 |
Correct |
77 ms |
32832 KB |
Output is correct |