//Judges with GCC >= 12 only needs Ofast
//#pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
//MLE optimization
//#pragma GCC optimize("conserve-stack")
//Old judges
//#pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
//New judges. Test with assert(__builtin_cpu_supports("avx2"));
//#pragma GCC target("arch=skylake")
//Atcoder
//#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//write variables and debug
#define bugf cout << "Here" << endl
#define bug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define bugarr(arr,...) cout << #arr; _dbg_arr(arr, __VA_ARGS__)
template <class T>
ostream& operator<<(ostream& output, const vector<T> &arr){
int n = arr.size();
output << "[";
for(int i = 0; i < n; i++){
output << arr[i];
if(i != n - 1) output << ", ";
}
output << "]";
return output;
}
template<class T, class U>
ostream& operator<<(ostream& output, const pair<T, U> &A){
output << "(" << A.first << ", " << A.second << ")";
return output;
}
template<class T>
ostream& operator<<(ostream& output, const set<T> &s){
output << "{";
for(auto it = s.begin(); it != s.end(); it++){
if(it != s.begin()) output << ", ";
output << (*it);
}
output << "}";
return output;
}
template<class T, class U>
ostream& operator<<(ostream& output, const map<T, U> &m){
output << "{";
for(auto it = m.begin(); it != m.end(); it++){
if(it != m.begin()) output << ", ";
output << "(" << it->first << " : " << it->second << ")";
}
output << "}";
return output;
}
template<class TH>
void _dbg(const char *sdbg, TH h){
cout << sdbg << " = " << h << endl;
}
template<class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while(*sdbg != ',')cout << *sdbg++;
cout << " = " << h << "\n";
_dbg(sdbg+1, a...);
}
template<class _Arr, class _Index>
void _dbg_arr(_Arr arr, _Index index){
cout << '[' << index << "] = " << arr[index] << endl;
}
template<class _Arr, class _Index, class... TA>
void _dbg_arr(_Arr &arr, _Index index, TA... args){
cout << '[' << index << ']';
_dbg_arr(arr[index], args...);
}
//open file
#define taskname "" //name input and output file
#ifdef SteGG
void open(){
if(fopen("input.inp", "r")){
freopen("input.inp", "r", stdin);
//freopen("output.out", "w", stdout);
}
}
#else
void open(){
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
}
#endif
//initialize
#define testcase \
long long test; \
cin >> test; \
for(int tst = 1; tst <= test; tst++)
#define int long long
#define MOD 1000000007
#define FFTMOD (119 << 23 | 1)
#define EPSILON 1e-9
#define setpre(x) fixed << setprecision(x)
#define cd complex<double>
#define all(x) x.begin(), x.end()
const long long INF = 3e18L + 5;
const int inf = 1e9 + 7;
const double infd = 1e60;
const double PI = acos(-1);
const int maxn = 1e5 + 5;
//const int base = ;
int n;
vector<int> tr[2*maxn];
string state;
int dp[2*maxn];
//others struct or class
//others function
//others init
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
open();
cin >> n;
for(int i = 1; i < n; i++){
int a, b;
cin >> a >> b;
tr[a].push_back(b);
tr[b].push_back(a);
}
cin >> state;
int result = 0;
auto dfs = [&](auto &&dfs, int u, int p) -> void {
int max_profit = 0, sum = 0;
for(int v : tr[u]){
if(v == p) continue;
dfs(dfs, v, u);
sum += dp[v];
max_profit = max(max_profit, dp[v]);
}
int val = state[u - 1] == '1';
dp[u] = max(val, sum - val);
result = max(result, max(sum - val, max_profit + val));
};
dfs(dfs, 1, 0);
cout << result << endl;
return 0;
}
Compilation message
power.cpp: In function 'void open()':
power.cpp:101:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
power.cpp:102:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4960 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4960 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
2 ms |
5076 KB |
Output is correct |
26 |
Correct |
2 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
2 ms |
5076 KB |
Output is correct |
43 |
Correct |
2 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5076 KB |
Output is correct |
49 |
Correct |
3 ms |
5076 KB |
Output is correct |
50 |
Correct |
3 ms |
5076 KB |
Output is correct |
51 |
Correct |
3 ms |
5088 KB |
Output is correct |
52 |
Correct |
3 ms |
5076 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
3 ms |
5076 KB |
Output is correct |
58 |
Correct |
2 ms |
5076 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5076 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
3 ms |
5076 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4960 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
2 ms |
5076 KB |
Output is correct |
26 |
Correct |
2 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
2 ms |
5076 KB |
Output is correct |
43 |
Correct |
2 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5076 KB |
Output is correct |
49 |
Correct |
3 ms |
5076 KB |
Output is correct |
50 |
Correct |
3 ms |
5076 KB |
Output is correct |
51 |
Correct |
3 ms |
5088 KB |
Output is correct |
52 |
Correct |
3 ms |
5076 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
3 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
3 ms |
5076 KB |
Output is correct |
57 |
Correct |
3 ms |
5076 KB |
Output is correct |
58 |
Correct |
2 ms |
5076 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5076 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
3 ms |
5076 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
64 |
Correct |
75 ms |
14136 KB |
Output is correct |
65 |
Correct |
87 ms |
14120 KB |
Output is correct |
66 |
Correct |
105 ms |
14136 KB |
Output is correct |
67 |
Correct |
81 ms |
14180 KB |
Output is correct |
68 |
Correct |
67 ms |
14244 KB |
Output is correct |
69 |
Correct |
64 ms |
14136 KB |
Output is correct |
70 |
Correct |
70 ms |
14124 KB |
Output is correct |
71 |
Correct |
69 ms |
14212 KB |
Output is correct |
72 |
Correct |
69 ms |
14244 KB |
Output is correct |
73 |
Correct |
72 ms |
21260 KB |
Output is correct |
74 |
Correct |
120 ms |
21288 KB |
Output is correct |
75 |
Correct |
69 ms |
15488 KB |
Output is correct |
76 |
Correct |
72 ms |
15476 KB |
Output is correct |
77 |
Correct |
67 ms |
14212 KB |
Output is correct |
78 |
Correct |
66 ms |
14232 KB |
Output is correct |
79 |
Correct |
65 ms |
14196 KB |
Output is correct |
80 |
Correct |
70 ms |
14212 KB |
Output is correct |
81 |
Correct |
59 ms |
14308 KB |
Output is correct |
82 |
Correct |
70 ms |
14316 KB |
Output is correct |
83 |
Correct |
76 ms |
17436 KB |
Output is correct |
84 |
Correct |
75 ms |
18852 KB |
Output is correct |
85 |
Correct |
69 ms |
18584 KB |
Output is correct |
86 |
Correct |
79 ms |
15096 KB |
Output is correct |
87 |
Correct |
68 ms |
14732 KB |
Output is correct |
88 |
Correct |
67 ms |
14892 KB |
Output is correct |
89 |
Correct |
66 ms |
14840 KB |
Output is correct |
90 |
Correct |
69 ms |
14732 KB |
Output is correct |
91 |
Correct |
78 ms |
14928 KB |
Output is correct |
92 |
Correct |
98 ms |
15032 KB |
Output is correct |
93 |
Correct |
62 ms |
16464 KB |
Output is correct |
94 |
Correct |
60 ms |
17536 KB |
Output is correct |
95 |
Correct |
62 ms |
16608 KB |
Output is correct |
96 |
Correct |
82 ms |
15448 KB |
Output is correct |
97 |
Correct |
69 ms |
14564 KB |
Output is correct |
98 |
Correct |
65 ms |
14396 KB |
Output is correct |
99 |
Correct |
70 ms |
14212 KB |
Output is correct |
100 |
Correct |
62 ms |
16120 KB |
Output is correct |
101 |
Correct |
78 ms |
14760 KB |
Output is correct |
102 |
Correct |
78 ms |
14356 KB |
Output is correct |
103 |
Correct |
67 ms |
14532 KB |
Output is correct |
104 |
Correct |
44 ms |
14736 KB |
Output is correct |
105 |
Correct |
78 ms |
19284 KB |
Output is correct |
106 |
Correct |
44 ms |
14780 KB |
Output is correct |
107 |
Correct |
53 ms |
14892 KB |
Output is correct |
108 |
Correct |
73 ms |
21772 KB |
Output is correct |
109 |
Correct |
60 ms |
14792 KB |
Output is correct |
110 |
Correct |
99 ms |
22064 KB |
Output is correct |
111 |
Correct |
66 ms |
14076 KB |
Output is correct |