//#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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23712 KB |
Output is correct |
2 |
Correct |
12 ms |
23700 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23756 KB |
Output is correct |
8 |
Correct |
11 ms |
23816 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23784 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
10 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23808 KB |
Output is correct |
17 |
Correct |
11 ms |
23752 KB |
Output is correct |
18 |
Correct |
12 ms |
23756 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23724 KB |
Output is correct |
21 |
Correct |
11 ms |
23752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23712 KB |
Output is correct |
2 |
Correct |
12 ms |
23700 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23756 KB |
Output is correct |
8 |
Correct |
11 ms |
23816 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23784 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
10 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23808 KB |
Output is correct |
17 |
Correct |
11 ms |
23752 KB |
Output is correct |
18 |
Correct |
12 ms |
23756 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23724 KB |
Output is correct |
21 |
Correct |
11 ms |
23752 KB |
Output is correct |
22 |
Correct |
11 ms |
23868 KB |
Output is correct |
23 |
Correct |
11 ms |
23844 KB |
Output is correct |
24 |
Correct |
11 ms |
23892 KB |
Output is correct |
25 |
Correct |
12 ms |
23828 KB |
Output is correct |
26 |
Correct |
11 ms |
23832 KB |
Output is correct |
27 |
Correct |
12 ms |
23788 KB |
Output is correct |
28 |
Correct |
12 ms |
23892 KB |
Output is correct |
29 |
Correct |
12 ms |
24020 KB |
Output is correct |
30 |
Correct |
12 ms |
23892 KB |
Output is correct |
31 |
Correct |
14 ms |
23892 KB |
Output is correct |
32 |
Correct |
11 ms |
23892 KB |
Output is correct |
33 |
Correct |
12 ms |
23888 KB |
Output is correct |
34 |
Correct |
12 ms |
23792 KB |
Output is correct |
35 |
Correct |
12 ms |
23892 KB |
Output is correct |
36 |
Correct |
12 ms |
23892 KB |
Output is correct |
37 |
Correct |
11 ms |
23892 KB |
Output is correct |
38 |
Correct |
12 ms |
23860 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 |
23860 KB |
Output is correct |
42 |
Correct |
13 ms |
23860 KB |
Output is correct |
43 |
Correct |
12 ms |
23892 KB |
Output is correct |
44 |
Correct |
12 ms |
23892 KB |
Output is correct |
45 |
Correct |
12 ms |
23892 KB |
Output is correct |
46 |
Correct |
12 ms |
23892 KB |
Output is correct |
47 |
Correct |
13 ms |
23892 KB |
Output is correct |
48 |
Correct |
11 ms |
23864 KB |
Output is correct |
49 |
Correct |
13 ms |
23988 KB |
Output is correct |
50 |
Correct |
12 ms |
23824 KB |
Output is correct |
51 |
Correct |
14 ms |
23892 KB |
Output is correct |
52 |
Correct |
13 ms |
23888 KB |
Output is correct |
53 |
Correct |
12 ms |
23892 KB |
Output is correct |
54 |
Correct |
13 ms |
23892 KB |
Output is correct |
55 |
Correct |
12 ms |
23892 KB |
Output is correct |
56 |
Correct |
12 ms |
23892 KB |
Output is correct |
57 |
Correct |
12 ms |
24020 KB |
Output is correct |
58 |
Correct |
12 ms |
23872 KB |
Output is correct |
59 |
Correct |
11 ms |
23892 KB |
Output is correct |
60 |
Correct |
13 ms |
23892 KB |
Output is correct |
61 |
Correct |
12 ms |
23892 KB |
Output is correct |
62 |
Correct |
12 ms |
23892 KB |
Output is correct |
63 |
Correct |
12 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23712 KB |
Output is correct |
2 |
Correct |
12 ms |
23700 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
11 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23756 KB |
Output is correct |
8 |
Correct |
11 ms |
23816 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23784 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
10 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23808 KB |
Output is correct |
17 |
Correct |
11 ms |
23752 KB |
Output is correct |
18 |
Correct |
12 ms |
23756 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23724 KB |
Output is correct |
21 |
Correct |
11 ms |
23752 KB |
Output is correct |
22 |
Correct |
11 ms |
23868 KB |
Output is correct |
23 |
Correct |
11 ms |
23844 KB |
Output is correct |
24 |
Correct |
11 ms |
23892 KB |
Output is correct |
25 |
Correct |
12 ms |
23828 KB |
Output is correct |
26 |
Correct |
11 ms |
23832 KB |
Output is correct |
27 |
Correct |
12 ms |
23788 KB |
Output is correct |
28 |
Correct |
12 ms |
23892 KB |
Output is correct |
29 |
Correct |
12 ms |
24020 KB |
Output is correct |
30 |
Correct |
12 ms |
23892 KB |
Output is correct |
31 |
Correct |
14 ms |
23892 KB |
Output is correct |
32 |
Correct |
11 ms |
23892 KB |
Output is correct |
33 |
Correct |
12 ms |
23888 KB |
Output is correct |
34 |
Correct |
12 ms |
23792 KB |
Output is correct |
35 |
Correct |
12 ms |
23892 KB |
Output is correct |
36 |
Correct |
12 ms |
23892 KB |
Output is correct |
37 |
Correct |
11 ms |
23892 KB |
Output is correct |
38 |
Correct |
12 ms |
23860 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 |
23860 KB |
Output is correct |
42 |
Correct |
13 ms |
23860 KB |
Output is correct |
43 |
Correct |
12 ms |
23892 KB |
Output is correct |
44 |
Correct |
12 ms |
23892 KB |
Output is correct |
45 |
Correct |
12 ms |
23892 KB |
Output is correct |
46 |
Correct |
12 ms |
23892 KB |
Output is correct |
47 |
Correct |
13 ms |
23892 KB |
Output is correct |
48 |
Correct |
11 ms |
23864 KB |
Output is correct |
49 |
Correct |
13 ms |
23988 KB |
Output is correct |
50 |
Correct |
12 ms |
23824 KB |
Output is correct |
51 |
Correct |
14 ms |
23892 KB |
Output is correct |
52 |
Correct |
13 ms |
23888 KB |
Output is correct |
53 |
Correct |
12 ms |
23892 KB |
Output is correct |
54 |
Correct |
13 ms |
23892 KB |
Output is correct |
55 |
Correct |
12 ms |
23892 KB |
Output is correct |
56 |
Correct |
12 ms |
23892 KB |
Output is correct |
57 |
Correct |
12 ms |
24020 KB |
Output is correct |
58 |
Correct |
12 ms |
23872 KB |
Output is correct |
59 |
Correct |
11 ms |
23892 KB |
Output is correct |
60 |
Correct |
13 ms |
23892 KB |
Output is correct |
61 |
Correct |
12 ms |
23892 KB |
Output is correct |
62 |
Correct |
12 ms |
23892 KB |
Output is correct |
63 |
Correct |
12 ms |
23808 KB |
Output is correct |
64 |
Correct |
82 ms |
32788 KB |
Output is correct |
65 |
Correct |
93 ms |
32844 KB |
Output is correct |
66 |
Correct |
86 ms |
32856 KB |
Output is correct |
67 |
Correct |
81 ms |
32816 KB |
Output is correct |
68 |
Correct |
99 ms |
32940 KB |
Output is correct |
69 |
Correct |
84 ms |
32892 KB |
Output is correct |
70 |
Correct |
85 ms |
32788 KB |
Output is correct |
71 |
Correct |
86 ms |
32844 KB |
Output is correct |
72 |
Correct |
82 ms |
32912 KB |
Output is correct |
73 |
Correct |
102 ms |
44500 KB |
Output is correct |
74 |
Correct |
103 ms |
44492 KB |
Output is correct |
75 |
Correct |
95 ms |
34164 KB |
Output is correct |
76 |
Correct |
85 ms |
34148 KB |
Output is correct |
77 |
Correct |
89 ms |
32824 KB |
Output is correct |
78 |
Correct |
84 ms |
32816 KB |
Output is correct |
79 |
Correct |
85 ms |
32868 KB |
Output is correct |
80 |
Correct |
83 ms |
32804 KB |
Output is correct |
81 |
Correct |
74 ms |
32932 KB |
Output is correct |
82 |
Correct |
78 ms |
32760 KB |
Output is correct |
83 |
Correct |
91 ms |
37920 KB |
Output is correct |
84 |
Correct |
117 ms |
40016 KB |
Output is correct |
85 |
Correct |
103 ms |
39748 KB |
Output is correct |
86 |
Correct |
89 ms |
34252 KB |
Output is correct |
87 |
Correct |
83 ms |
33676 KB |
Output is correct |
88 |
Correct |
107 ms |
33680 KB |
Output is correct |
89 |
Correct |
104 ms |
33844 KB |
Output is correct |
90 |
Correct |
81 ms |
33600 KB |
Output is correct |
91 |
Correct |
85 ms |
33740 KB |
Output is correct |
92 |
Correct |
87 ms |
34124 KB |
Output is correct |
93 |
Correct |
83 ms |
36296 KB |
Output is correct |
94 |
Correct |
103 ms |
38172 KB |
Output is correct |
95 |
Correct |
93 ms |
36608 KB |
Output is correct |
96 |
Correct |
112 ms |
34848 KB |
Output is correct |
97 |
Correct |
85 ms |
33416 KB |
Output is correct |
98 |
Correct |
82 ms |
32996 KB |
Output is correct |
99 |
Correct |
86 ms |
32972 KB |
Output is correct |
100 |
Correct |
97 ms |
35828 KB |
Output is correct |
101 |
Correct |
82 ms |
33756 KB |
Output is correct |
102 |
Correct |
82 ms |
33108 KB |
Output is correct |
103 |
Correct |
97 ms |
33108 KB |
Output is correct |
104 |
Correct |
62 ms |
33336 KB |
Output is correct |
105 |
Correct |
121 ms |
41368 KB |
Output is correct |
106 |
Correct |
61 ms |
33344 KB |
Output is correct |
107 |
Correct |
61 ms |
33340 KB |
Output is correct |
108 |
Correct |
128 ms |
45280 KB |
Output is correct |
109 |
Correct |
61 ms |
33372 KB |
Output is correct |
110 |
Correct |
105 ms |
45540 KB |
Output is correct |
111 |
Correct |
101 ms |
32932 KB |
Output is correct |