#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 2e5 + 69;
const int INF = 1e9 + 69;
int n;
vector<int> graph[N];
int e[N];
int dp[N][2];
int ans = 0;
void dfs(int u, int p){
dp[u][0] = 0;
dp[u][1] = e[u];
int sum = -e[u];
for(int v: graph[u]) if (v != p){
dfs(v, u);
maximize(ans, max(dp[v][0], dp[v][1]) + e[u]);
sum += max(dp[v][0], dp[v][1]);
}
maximize(dp[u][1], sum);
maximize(ans, dp[u][1]);
// cout << u << " " << dp[u][0] << " " << dp[u][1] << "\n";
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t start = clock();
cin >> n;
for(int i = 1; i<n; ++i){
int u, v; cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
string s; cin >> s;
for(int i = 0; i<n; ++i) e[i+1] = s[i] == '1';
dfs(1, 0);
cout << ans << "\n";
cerr << "Time elapsed: " << clock() - start << " ms\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7260 KB |
Output is correct |
5 |
Correct |
1 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
8 |
Correct |
1 ms |
7260 KB |
Output is correct |
9 |
Correct |
1 ms |
7260 KB |
Output is correct |
10 |
Correct |
1 ms |
7260 KB |
Output is correct |
11 |
Correct |
1 ms |
7260 KB |
Output is correct |
12 |
Correct |
1 ms |
7260 KB |
Output is correct |
13 |
Correct |
1 ms |
7260 KB |
Output is correct |
14 |
Correct |
1 ms |
7260 KB |
Output is correct |
15 |
Correct |
1 ms |
7260 KB |
Output is correct |
16 |
Correct |
1 ms |
7328 KB |
Output is correct |
17 |
Correct |
1 ms |
7260 KB |
Output is correct |
18 |
Correct |
1 ms |
7260 KB |
Output is correct |
19 |
Correct |
1 ms |
7260 KB |
Output is correct |
20 |
Correct |
1 ms |
7260 KB |
Output is correct |
21 |
Correct |
1 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7260 KB |
Output is correct |
5 |
Correct |
1 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
8 |
Correct |
1 ms |
7260 KB |
Output is correct |
9 |
Correct |
1 ms |
7260 KB |
Output is correct |
10 |
Correct |
1 ms |
7260 KB |
Output is correct |
11 |
Correct |
1 ms |
7260 KB |
Output is correct |
12 |
Correct |
1 ms |
7260 KB |
Output is correct |
13 |
Correct |
1 ms |
7260 KB |
Output is correct |
14 |
Correct |
1 ms |
7260 KB |
Output is correct |
15 |
Correct |
1 ms |
7260 KB |
Output is correct |
16 |
Correct |
1 ms |
7328 KB |
Output is correct |
17 |
Correct |
1 ms |
7260 KB |
Output is correct |
18 |
Correct |
1 ms |
7260 KB |
Output is correct |
19 |
Correct |
1 ms |
7260 KB |
Output is correct |
20 |
Correct |
1 ms |
7260 KB |
Output is correct |
21 |
Correct |
1 ms |
7260 KB |
Output is correct |
22 |
Correct |
2 ms |
7260 KB |
Output is correct |
23 |
Correct |
2 ms |
7260 KB |
Output is correct |
24 |
Correct |
2 ms |
7412 KB |
Output is correct |
25 |
Correct |
2 ms |
7260 KB |
Output is correct |
26 |
Correct |
3 ms |
7260 KB |
Output is correct |
27 |
Correct |
2 ms |
7260 KB |
Output is correct |
28 |
Correct |
2 ms |
7260 KB |
Output is correct |
29 |
Correct |
1 ms |
7516 KB |
Output is correct |
30 |
Correct |
2 ms |
7516 KB |
Output is correct |
31 |
Correct |
2 ms |
7260 KB |
Output is correct |
32 |
Correct |
2 ms |
7260 KB |
Output is correct |
33 |
Correct |
2 ms |
7408 KB |
Output is correct |
34 |
Correct |
2 ms |
7260 KB |
Output is correct |
35 |
Correct |
2 ms |
7260 KB |
Output is correct |
36 |
Correct |
1 ms |
7260 KB |
Output is correct |
37 |
Correct |
2 ms |
7260 KB |
Output is correct |
38 |
Correct |
1 ms |
7260 KB |
Output is correct |
39 |
Correct |
2 ms |
7260 KB |
Output is correct |
40 |
Correct |
2 ms |
7260 KB |
Output is correct |
41 |
Correct |
2 ms |
7356 KB |
Output is correct |
42 |
Correct |
2 ms |
7260 KB |
Output is correct |
43 |
Correct |
2 ms |
7260 KB |
Output is correct |
44 |
Correct |
1 ms |
7260 KB |
Output is correct |
45 |
Correct |
2 ms |
7256 KB |
Output is correct |
46 |
Correct |
2 ms |
7260 KB |
Output is correct |
47 |
Correct |
2 ms |
7260 KB |
Output is correct |
48 |
Correct |
2 ms |
7516 KB |
Output is correct |
49 |
Correct |
2 ms |
7260 KB |
Output is correct |
50 |
Correct |
2 ms |
7260 KB |
Output is correct |
51 |
Correct |
2 ms |
7260 KB |
Output is correct |
52 |
Correct |
2 ms |
7260 KB |
Output is correct |
53 |
Correct |
2 ms |
7340 KB |
Output is correct |
54 |
Correct |
2 ms |
7264 KB |
Output is correct |
55 |
Correct |
2 ms |
7352 KB |
Output is correct |
56 |
Correct |
2 ms |
7260 KB |
Output is correct |
57 |
Correct |
2 ms |
7524 KB |
Output is correct |
58 |
Correct |
2 ms |
7260 KB |
Output is correct |
59 |
Correct |
2 ms |
7424 KB |
Output is correct |
60 |
Correct |
2 ms |
7260 KB |
Output is correct |
61 |
Correct |
2 ms |
7260 KB |
Output is correct |
62 |
Correct |
2 ms |
7516 KB |
Output is correct |
63 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7260 KB |
Output is correct |
5 |
Correct |
1 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
8 |
Correct |
1 ms |
7260 KB |
Output is correct |
9 |
Correct |
1 ms |
7260 KB |
Output is correct |
10 |
Correct |
1 ms |
7260 KB |
Output is correct |
11 |
Correct |
1 ms |
7260 KB |
Output is correct |
12 |
Correct |
1 ms |
7260 KB |
Output is correct |
13 |
Correct |
1 ms |
7260 KB |
Output is correct |
14 |
Correct |
1 ms |
7260 KB |
Output is correct |
15 |
Correct |
1 ms |
7260 KB |
Output is correct |
16 |
Correct |
1 ms |
7328 KB |
Output is correct |
17 |
Correct |
1 ms |
7260 KB |
Output is correct |
18 |
Correct |
1 ms |
7260 KB |
Output is correct |
19 |
Correct |
1 ms |
7260 KB |
Output is correct |
20 |
Correct |
1 ms |
7260 KB |
Output is correct |
21 |
Correct |
1 ms |
7260 KB |
Output is correct |
22 |
Correct |
2 ms |
7260 KB |
Output is correct |
23 |
Correct |
2 ms |
7260 KB |
Output is correct |
24 |
Correct |
2 ms |
7412 KB |
Output is correct |
25 |
Correct |
2 ms |
7260 KB |
Output is correct |
26 |
Correct |
3 ms |
7260 KB |
Output is correct |
27 |
Correct |
2 ms |
7260 KB |
Output is correct |
28 |
Correct |
2 ms |
7260 KB |
Output is correct |
29 |
Correct |
1 ms |
7516 KB |
Output is correct |
30 |
Correct |
2 ms |
7516 KB |
Output is correct |
31 |
Correct |
2 ms |
7260 KB |
Output is correct |
32 |
Correct |
2 ms |
7260 KB |
Output is correct |
33 |
Correct |
2 ms |
7408 KB |
Output is correct |
34 |
Correct |
2 ms |
7260 KB |
Output is correct |
35 |
Correct |
2 ms |
7260 KB |
Output is correct |
36 |
Correct |
1 ms |
7260 KB |
Output is correct |
37 |
Correct |
2 ms |
7260 KB |
Output is correct |
38 |
Correct |
1 ms |
7260 KB |
Output is correct |
39 |
Correct |
2 ms |
7260 KB |
Output is correct |
40 |
Correct |
2 ms |
7260 KB |
Output is correct |
41 |
Correct |
2 ms |
7356 KB |
Output is correct |
42 |
Correct |
2 ms |
7260 KB |
Output is correct |
43 |
Correct |
2 ms |
7260 KB |
Output is correct |
44 |
Correct |
1 ms |
7260 KB |
Output is correct |
45 |
Correct |
2 ms |
7256 KB |
Output is correct |
46 |
Correct |
2 ms |
7260 KB |
Output is correct |
47 |
Correct |
2 ms |
7260 KB |
Output is correct |
48 |
Correct |
2 ms |
7516 KB |
Output is correct |
49 |
Correct |
2 ms |
7260 KB |
Output is correct |
50 |
Correct |
2 ms |
7260 KB |
Output is correct |
51 |
Correct |
2 ms |
7260 KB |
Output is correct |
52 |
Correct |
2 ms |
7260 KB |
Output is correct |
53 |
Correct |
2 ms |
7340 KB |
Output is correct |
54 |
Correct |
2 ms |
7264 KB |
Output is correct |
55 |
Correct |
2 ms |
7352 KB |
Output is correct |
56 |
Correct |
2 ms |
7260 KB |
Output is correct |
57 |
Correct |
2 ms |
7524 KB |
Output is correct |
58 |
Correct |
2 ms |
7260 KB |
Output is correct |
59 |
Correct |
2 ms |
7424 KB |
Output is correct |
60 |
Correct |
2 ms |
7260 KB |
Output is correct |
61 |
Correct |
2 ms |
7260 KB |
Output is correct |
62 |
Correct |
2 ms |
7516 KB |
Output is correct |
63 |
Correct |
2 ms |
7260 KB |
Output is correct |
64 |
Correct |
53 ms |
16880 KB |
Output is correct |
65 |
Correct |
50 ms |
16784 KB |
Output is correct |
66 |
Correct |
52 ms |
16776 KB |
Output is correct |
67 |
Correct |
49 ms |
16788 KB |
Output is correct |
68 |
Correct |
50 ms |
16764 KB |
Output is correct |
69 |
Correct |
49 ms |
16784 KB |
Output is correct |
70 |
Correct |
51 ms |
16848 KB |
Output is correct |
71 |
Correct |
49 ms |
16900 KB |
Output is correct |
72 |
Correct |
49 ms |
16780 KB |
Output is correct |
73 |
Correct |
70 ms |
29348 KB |
Output is correct |
74 |
Correct |
66 ms |
29420 KB |
Output is correct |
75 |
Correct |
48 ms |
16784 KB |
Output is correct |
76 |
Correct |
54 ms |
16796 KB |
Output is correct |
77 |
Correct |
49 ms |
16784 KB |
Output is correct |
78 |
Correct |
49 ms |
16804 KB |
Output is correct |
79 |
Correct |
49 ms |
16792 KB |
Output is correct |
80 |
Correct |
49 ms |
16780 KB |
Output is correct |
81 |
Correct |
44 ms |
17224 KB |
Output is correct |
82 |
Correct |
50 ms |
16980 KB |
Output is correct |
83 |
Correct |
53 ms |
21908 KB |
Output is correct |
84 |
Correct |
55 ms |
23968 KB |
Output is correct |
85 |
Correct |
59 ms |
23604 KB |
Output is correct |
86 |
Correct |
50 ms |
18068 KB |
Output is correct |
87 |
Correct |
50 ms |
17552 KB |
Output is correct |
88 |
Correct |
52 ms |
18128 KB |
Output is correct |
89 |
Correct |
51 ms |
18076 KB |
Output is correct |
90 |
Correct |
53 ms |
17776 KB |
Output is correct |
91 |
Correct |
53 ms |
18108 KB |
Output is correct |
92 |
Correct |
51 ms |
18300 KB |
Output is correct |
93 |
Correct |
50 ms |
20832 KB |
Output is correct |
94 |
Correct |
50 ms |
22476 KB |
Output is correct |
95 |
Correct |
46 ms |
21236 KB |
Output is correct |
96 |
Correct |
58 ms |
19456 KB |
Output is correct |
97 |
Correct |
50 ms |
17896 KB |
Output is correct |
98 |
Correct |
50 ms |
17348 KB |
Output is correct |
99 |
Correct |
49 ms |
17040 KB |
Output is correct |
100 |
Correct |
50 ms |
20348 KB |
Output is correct |
101 |
Correct |
50 ms |
18300 KB |
Output is correct |
102 |
Correct |
47 ms |
17504 KB |
Output is correct |
103 |
Correct |
48 ms |
17232 KB |
Output is correct |
104 |
Correct |
39 ms |
17604 KB |
Output is correct |
105 |
Correct |
59 ms |
26312 KB |
Output is correct |
106 |
Correct |
35 ms |
17620 KB |
Output is correct |
107 |
Correct |
35 ms |
17608 KB |
Output is correct |
108 |
Correct |
59 ms |
30096 KB |
Output is correct |
109 |
Correct |
35 ms |
17616 KB |
Output is correct |
110 |
Correct |
58 ms |
30596 KB |
Output is correct |
111 |
Correct |
45 ms |
16800 KB |
Output is correct |