// cre: Nguyen Ngoc Hung - Train VOI 2024
#include<bits/stdc++.h>
using namespace std;
#define __nnhzzz__ signed main()
#define BIT(i,j) ((i>>j)&1LL)
#define MASK(i) (1LL<<i)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define gcd __gcd
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define pii pair<int,int>
#define vpii vector<pii>
#define REP(i,a,b) for(int i = (a); i<=(b); ++i)
#define REPD(i,a,b) for(int i = (a); i>=(b); --i)
#define REPDIS(i,a,b,c) for(int i = (a); i<=(b); i+=c)
//-------------------------------------------------------------//
const int oo = 1e9,LOG = 20,MAXN = 2e5+7,N = 1e2+3;
const int MOD = 1e9+7,MOD1 = 1e9+207,MOD2 = 1e9+407,MOD3 = 998244353;
//-------------------------------------------------------------//
template<typename T> bool mini(T &a, const T &b){if(a>b){a=b;return true;}return false;}
template<typename T> bool maxi(T &a, const T &b){if(a<b){a=b;return true;}return false;}
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Nguyen Ngoc Hung - nnhzzz
Training for VOI24 gold medal
----------------------------------------------------------------
*/
int dp[MAXN],x[MAXN];
vi adj[MAXN];
int res = 0;
int dfs(int u, int par){
int ma = 0;
for(auto &v:adj[u]){
if(v==par) continue;
dp[u] += dfs(v,u);
maxi(ma,dp[v]);
}
dp[u] = max(dp[u]-x[u],x[u]);
maxi(res,max(dp[u],ma+x[u]));
return dp[u];
}
void solve(){
int n; cin >> n;
REP(i,2,n){
int u,v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
string s; cin >> s;
REP(i,1,n) x[i] = s[i-1]-'0';
dfs(1,0);
cout << res;
}
__nnhzzz__{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "test"
if(fopen(name".inp","r")){
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
#define name1 "nnhzzz"
if(fopen(name1".inp","r")){
freopen(name1".inp","r",stdin);
freopen(name1".out","w",stdout);
}
int test = 1;
while(test--){
solve();
}
cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
return 0;
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
power.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
power.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | freopen(name1".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
power.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen(name1".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
4956 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
4952 KB |
Output is correct |
19 |
Correct |
1 ms |
4956 KB |
Output is correct |
20 |
Correct |
1 ms |
5132 KB |
Output is correct |
21 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
4956 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
4952 KB |
Output is correct |
19 |
Correct |
1 ms |
4956 KB |
Output is correct |
20 |
Correct |
1 ms |
5132 KB |
Output is correct |
21 |
Correct |
1 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
2 ms |
5172 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
2 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5228 KB |
Output is correct |
33 |
Correct |
2 ms |
5180 KB |
Output is correct |
34 |
Correct |
2 ms |
5208 KB |
Output is correct |
35 |
Correct |
2 ms |
5212 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
2 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5220 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 |
5220 KB |
Output is correct |
43 |
Correct |
2 ms |
5208 KB |
Output is correct |
44 |
Correct |
2 ms |
5224 KB |
Output is correct |
45 |
Correct |
2 ms |
5224 KB |
Output is correct |
46 |
Correct |
2 ms |
5224 KB |
Output is correct |
47 |
Correct |
2 ms |
5224 KB |
Output is correct |
48 |
Correct |
2 ms |
5224 KB |
Output is correct |
49 |
Correct |
2 ms |
5212 KB |
Output is correct |
50 |
Correct |
2 ms |
5216 KB |
Output is correct |
51 |
Correct |
2 ms |
5216 KB |
Output is correct |
52 |
Correct |
2 ms |
5216 KB |
Output is correct |
53 |
Correct |
2 ms |
5216 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 |
5216 KB |
Output is correct |
57 |
Correct |
2 ms |
5192 KB |
Output is correct |
58 |
Correct |
2 ms |
5216 KB |
Output is correct |
59 |
Correct |
2 ms |
5220 KB |
Output is correct |
60 |
Correct |
2 ms |
5216 KB |
Output is correct |
61 |
Correct |
2 ms |
5220 KB |
Output is correct |
62 |
Correct |
2 ms |
5220 KB |
Output is correct |
63 |
Correct |
2 ms |
5220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
4956 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
4952 KB |
Output is correct |
19 |
Correct |
1 ms |
4956 KB |
Output is correct |
20 |
Correct |
1 ms |
5132 KB |
Output is correct |
21 |
Correct |
1 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
2 ms |
5172 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
2 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5228 KB |
Output is correct |
33 |
Correct |
2 ms |
5180 KB |
Output is correct |
34 |
Correct |
2 ms |
5208 KB |
Output is correct |
35 |
Correct |
2 ms |
5212 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
2 ms |
5212 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5220 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 |
5220 KB |
Output is correct |
43 |
Correct |
2 ms |
5208 KB |
Output is correct |
44 |
Correct |
2 ms |
5224 KB |
Output is correct |
45 |
Correct |
2 ms |
5224 KB |
Output is correct |
46 |
Correct |
2 ms |
5224 KB |
Output is correct |
47 |
Correct |
2 ms |
5224 KB |
Output is correct |
48 |
Correct |
2 ms |
5224 KB |
Output is correct |
49 |
Correct |
2 ms |
5212 KB |
Output is correct |
50 |
Correct |
2 ms |
5216 KB |
Output is correct |
51 |
Correct |
2 ms |
5216 KB |
Output is correct |
52 |
Correct |
2 ms |
5216 KB |
Output is correct |
53 |
Correct |
2 ms |
5216 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 |
5216 KB |
Output is correct |
57 |
Correct |
2 ms |
5192 KB |
Output is correct |
58 |
Correct |
2 ms |
5216 KB |
Output is correct |
59 |
Correct |
2 ms |
5220 KB |
Output is correct |
60 |
Correct |
2 ms |
5216 KB |
Output is correct |
61 |
Correct |
2 ms |
5220 KB |
Output is correct |
62 |
Correct |
2 ms |
5220 KB |
Output is correct |
63 |
Correct |
2 ms |
5220 KB |
Output is correct |
64 |
Correct |
62 ms |
16032 KB |
Output is correct |
65 |
Correct |
57 ms |
16016 KB |
Output is correct |
66 |
Correct |
60 ms |
16004 KB |
Output is correct |
67 |
Correct |
57 ms |
16016 KB |
Output is correct |
68 |
Correct |
58 ms |
16004 KB |
Output is correct |
69 |
Correct |
57 ms |
15888 KB |
Output is correct |
70 |
Correct |
59 ms |
16032 KB |
Output is correct |
71 |
Correct |
59 ms |
15880 KB |
Output is correct |
72 |
Correct |
57 ms |
16016 KB |
Output is correct |
73 |
Correct |
75 ms |
28560 KB |
Output is correct |
74 |
Correct |
73 ms |
28564 KB |
Output is correct |
75 |
Correct |
54 ms |
15932 KB |
Output is correct |
76 |
Correct |
69 ms |
15888 KB |
Output is correct |
77 |
Correct |
61 ms |
15812 KB |
Output is correct |
78 |
Correct |
55 ms |
15824 KB |
Output is correct |
79 |
Correct |
60 ms |
15768 KB |
Output is correct |
80 |
Correct |
55 ms |
15768 KB |
Output is correct |
81 |
Correct |
56 ms |
16396 KB |
Output is correct |
82 |
Correct |
53 ms |
16040 KB |
Output is correct |
83 |
Correct |
62 ms |
21140 KB |
Output is correct |
84 |
Correct |
66 ms |
23204 KB |
Output is correct |
85 |
Correct |
67 ms |
22948 KB |
Output is correct |
86 |
Correct |
75 ms |
17556 KB |
Output is correct |
87 |
Correct |
58 ms |
16700 KB |
Output is correct |
88 |
Correct |
59 ms |
17100 KB |
Output is correct |
89 |
Correct |
59 ms |
17308 KB |
Output is correct |
90 |
Correct |
72 ms |
17140 KB |
Output is correct |
91 |
Correct |
65 ms |
17520 KB |
Output is correct |
92 |
Correct |
58 ms |
17532 KB |
Output is correct |
93 |
Correct |
64 ms |
20268 KB |
Output is correct |
94 |
Correct |
56 ms |
21748 KB |
Output is correct |
95 |
Correct |
58 ms |
20428 KB |
Output is correct |
96 |
Correct |
71 ms |
18936 KB |
Output is correct |
97 |
Correct |
57 ms |
17128 KB |
Output is correct |
98 |
Correct |
66 ms |
16588 KB |
Output is correct |
99 |
Correct |
56 ms |
16264 KB |
Output is correct |
100 |
Correct |
60 ms |
19444 KB |
Output is correct |
101 |
Correct |
56 ms |
17416 KB |
Output is correct |
102 |
Correct |
55 ms |
16740 KB |
Output is correct |
103 |
Correct |
55 ms |
16528 KB |
Output is correct |
104 |
Correct |
40 ms |
16800 KB |
Output is correct |
105 |
Correct |
74 ms |
25420 KB |
Output is correct |
106 |
Correct |
42 ms |
16788 KB |
Output is correct |
107 |
Correct |
41 ms |
16836 KB |
Output is correct |
108 |
Correct |
72 ms |
29488 KB |
Output is correct |
109 |
Correct |
41 ms |
16840 KB |
Output is correct |
110 |
Correct |
68 ms |
29952 KB |
Output is correct |
111 |
Correct |
61 ms |
15936 KB |
Output is correct |