#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);; cin.tie(NULL)
void selfMax(long long& a, long long b){
a = max(a, b);
}
void selfMin(long long& a, long long b){
a = min(a, b);
}
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& x) {return in >> x.first >> x.second;}
template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& x) {return out << x.first << ' ' << x.second;}
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {for(auto &x : a) out << x << ' '; return out;};
template<typename T> ostream& operator<<(ostream& out, vector<vector<T> >& a) {for(auto &x : a) out << x << '\n'; return out;};
template<typename T1, typename T2> ostream& operator<<(ostream& out, vector<pair<T1, T2> >& a) {for(auto &x : a) out << x << '\n'; return out;};
/*
Use DSU dsu(N);
*/
struct DSU {
vector<long long> e;
DSU(long long N) { e = vector<long long>(N, -1); }
// get representive component (uses path compression)
long long get(long long x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(long long a, long long b) { return get(a) == get(b); }
long long size(long long x) { return -e[get(x)]; }
bool unite(long long x, long long y) { // union by size
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y]; e[y] = x;
return true;
}
};
/*
Run with Bit<long long> BIT
*/
template <class T> class BIT {
private:
long long size;
vector<T> bit;
vector<T> arr;
public:
BIT(long long size) : size(size), bit(size + 1), arr(size) {}
void set(long long ind, long long val) { add(ind, val - arr[ind]); }
void add(long long ind, long long val) {
arr[ind] += val;
ind++;
for (; ind <= size; ind += ind & -ind) { bit[ind] += val; }
}
T pref_sum(long long ind) {
ind++;
T total = 0;
for (; ind > 0; ind -= ind & -ind) { total += bit[ind]; }
return total;
}
};
/*
Change Comb for specific Seg tree probs, but run with Seg<long long> Seg;
*/
template<class T> struct Seg {
const T ID = 1e18; T comb(T a, T b) { return min(a,b); }
long long n; vector<T> seg;
void init(long long _n) { n = _n; seg.assign(2*n,ID); }
void pull(long long p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
void upd(long long p, T val) {
seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
T query(long long l, long long r) {
T ra = ID, rb = ID;
for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
if (l&1) ra = comb(ra,seg[l++]);
if (r&1) rb = comb(seg[--r],rb);
}
return comb(ra,rb);
}
};
const int N = 200001;
int dp[N];
int subTreeSize[N];
vector<int> adj[N];
string s;
int ans = 0;
void dfs(int u, int p){
int counter = 0;
int sum = 0;
int cur = 0;
if(s[u] == '1'){
dp[u] = 1;
subTreeSize[u] = 1;
}
for(int v : adj[u]){
if(v != p){
dfs(v, u);
if(subTreeSize[v] > 0){
subTreeSize[u] += subTreeSize[v];
counter++;
sum += dp[v];
cur = max(cur, dp[v]);
}
}
}
if(s[u] == '1') {
ans = max(ans, cur + 1);
sum--;
}
dp[u] = max(dp[u], sum);
}
int main(){
int n;
cin >> n;
for(int i = 1; i < n; i++){
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin >> s;
s = '0' + s;
dfs(1, 0);
for(int i = 1; i <= n; i++){
ans = max(ans, dp[i]);
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 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 |
5000 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5000 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
5004 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 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 |
5000 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5000 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
5004 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 |
5028 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5012 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5256 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5104 KB |
Output is correct |
32 |
Correct |
5 ms |
5016 KB |
Output is correct |
33 |
Correct |
3 ms |
5008 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5016 KB |
Output is correct |
36 |
Correct |
3 ms |
5016 KB |
Output is correct |
37 |
Correct |
4 ms |
5016 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
5 ms |
5076 KB |
Output is correct |
40 |
Correct |
5 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5016 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5076 KB |
Output is correct |
47 |
Correct |
4 ms |
5016 KB |
Output is correct |
48 |
Correct |
3 ms |
5076 KB |
Output is correct |
49 |
Correct |
4 ms |
5144 KB |
Output is correct |
50 |
Correct |
3 ms |
5140 KB |
Output is correct |
51 |
Correct |
3 ms |
5076 KB |
Output is correct |
52 |
Correct |
3 ms |
5120 KB |
Output is correct |
53 |
Correct |
6 ms |
5144 KB |
Output is correct |
54 |
Correct |
5 ms |
5140 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
6 ms |
5008 KB |
Output is correct |
57 |
Correct |
4 ms |
5204 KB |
Output is correct |
58 |
Correct |
3 ms |
5020 KB |
Output is correct |
59 |
Correct |
3 ms |
5020 KB |
Output is correct |
60 |
Correct |
4 ms |
5204 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5148 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 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 |
5000 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5000 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
5004 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 |
5028 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5012 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5256 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5104 KB |
Output is correct |
32 |
Correct |
5 ms |
5016 KB |
Output is correct |
33 |
Correct |
3 ms |
5008 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5016 KB |
Output is correct |
36 |
Correct |
3 ms |
5016 KB |
Output is correct |
37 |
Correct |
4 ms |
5016 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
5 ms |
5076 KB |
Output is correct |
40 |
Correct |
5 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5016 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
4 ms |
5076 KB |
Output is correct |
47 |
Correct |
4 ms |
5016 KB |
Output is correct |
48 |
Correct |
3 ms |
5076 KB |
Output is correct |
49 |
Correct |
4 ms |
5144 KB |
Output is correct |
50 |
Correct |
3 ms |
5140 KB |
Output is correct |
51 |
Correct |
3 ms |
5076 KB |
Output is correct |
52 |
Correct |
3 ms |
5120 KB |
Output is correct |
53 |
Correct |
6 ms |
5144 KB |
Output is correct |
54 |
Correct |
5 ms |
5140 KB |
Output is correct |
55 |
Correct |
4 ms |
5076 KB |
Output is correct |
56 |
Correct |
6 ms |
5008 KB |
Output is correct |
57 |
Correct |
4 ms |
5204 KB |
Output is correct |
58 |
Correct |
3 ms |
5020 KB |
Output is correct |
59 |
Correct |
3 ms |
5020 KB |
Output is correct |
60 |
Correct |
4 ms |
5204 KB |
Output is correct |
61 |
Correct |
4 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5148 KB |
Output is correct |
63 |
Correct |
3 ms |
5076 KB |
Output is correct |
64 |
Correct |
172 ms |
13400 KB |
Output is correct |
65 |
Correct |
128 ms |
13424 KB |
Output is correct |
66 |
Correct |
150 ms |
13468 KB |
Output is correct |
67 |
Correct |
160 ms |
13400 KB |
Output is correct |
68 |
Correct |
163 ms |
13512 KB |
Output is correct |
69 |
Correct |
180 ms |
13488 KB |
Output is correct |
70 |
Correct |
188 ms |
13496 KB |
Output is correct |
71 |
Correct |
189 ms |
13520 KB |
Output is correct |
72 |
Correct |
161 ms |
13188 KB |
Output is correct |
73 |
Correct |
183 ms |
28576 KB |
Output is correct |
74 |
Correct |
229 ms |
28528 KB |
Output is correct |
75 |
Correct |
137 ms |
13336 KB |
Output is correct |
76 |
Correct |
145 ms |
13280 KB |
Output is correct |
77 |
Correct |
150 ms |
13328 KB |
Output is correct |
78 |
Correct |
149 ms |
13352 KB |
Output is correct |
79 |
Correct |
143 ms |
13320 KB |
Output is correct |
80 |
Correct |
130 ms |
13276 KB |
Output is correct |
81 |
Correct |
136 ms |
13880 KB |
Output is correct |
82 |
Correct |
171 ms |
13492 KB |
Output is correct |
83 |
Correct |
164 ms |
19644 KB |
Output is correct |
84 |
Correct |
147 ms |
22200 KB |
Output is correct |
85 |
Correct |
168 ms |
21736 KB |
Output is correct |
86 |
Correct |
136 ms |
15112 KB |
Output is correct |
87 |
Correct |
143 ms |
14480 KB |
Output is correct |
88 |
Correct |
138 ms |
14756 KB |
Output is correct |
89 |
Correct |
178 ms |
14800 KB |
Output is correct |
90 |
Correct |
151 ms |
14620 KB |
Output is correct |
91 |
Correct |
146 ms |
14884 KB |
Output is correct |
92 |
Correct |
140 ms |
15260 KB |
Output is correct |
93 |
Correct |
151 ms |
18216 KB |
Output is correct |
94 |
Correct |
144 ms |
20332 KB |
Output is correct |
95 |
Correct |
196 ms |
18576 KB |
Output is correct |
96 |
Correct |
161 ms |
16504 KB |
Output is correct |
97 |
Correct |
157 ms |
14744 KB |
Output is correct |
98 |
Correct |
154 ms |
13968 KB |
Output is correct |
99 |
Correct |
135 ms |
13848 KB |
Output is correct |
100 |
Correct |
146 ms |
17648 KB |
Output is correct |
101 |
Correct |
146 ms |
15316 KB |
Output is correct |
102 |
Correct |
135 ms |
14204 KB |
Output is correct |
103 |
Correct |
147 ms |
14072 KB |
Output is correct |
104 |
Correct |
108 ms |
13440 KB |
Output is correct |
105 |
Correct |
171 ms |
24980 KB |
Output is correct |
106 |
Correct |
104 ms |
14280 KB |
Output is correct |
107 |
Correct |
111 ms |
14268 KB |
Output is correct |
108 |
Correct |
209 ms |
29524 KB |
Output is correct |
109 |
Correct |
114 ms |
13440 KB |
Output is correct |
110 |
Correct |
161 ms |
29956 KB |
Output is correct |
111 |
Correct |
144 ms |
13360 KB |
Output is correct |