#include<bits/stdc++.h>
// #define int long long
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back
#define eb emplace_back
#define For(i,a,b) for(int i=(a); i<=(b); ++i)
#define roF(i,a,b) for(int i=(a); i>=(b); --i)
#define fi first
#define se second
#define mod 998244353
using namespace std;
// using namespace atcoder;
#ifdef DEBUG__
struct db_os{
ostream& os;
bool chk;
template<class T> auto operator<<(T&& x){
if(!chk) os << ", ";
chk=0;
os << x;
return *this;
}
};
template<class... T> void db_out(T&&... t){
(db_os{cerr, 1} << ... << t);
}
#define dbg(...) \
do{ \
cerr << __LINE__ << ":" << #__VA_ARGS__ << "="; \
db_out(__VA_ARGS__); \
cerr << "\n"; \
}while(0);
#else
#define dbg(...)
#endif
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
typedef long double ld;
const int N=200005;
const ll inf=0x3f3f3f3f;
mt19937 rng(random_device {}());
int rand(int a){
return rng()%a;
}
int n, par[N], le[N], dp[N];
vi adj[N], vc;
char s[N];
void dfs(int u, int p=-1){
vc.pb(u);
for(auto &i: adj[u]){
if(i == p) continue;
par[i]=u;
dfs(i, u);
}
}
void rmain(){
cin >> n;
For(i,2,n){
int u, v; cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
//string s; cin >> s;
For(i,1,n){
cin >> s[i];
s[i]-='0';
}
int res=min((int)count(s+1, s+n+1, 1), 2);
dfs(1);
reverse(all(vc));
for(auto &v: vc){
dp[v]=-s[v];
for(auto &u: adj[v]){
dp[v]+=(u != par[v] ? dp[u] : 0);
}
dp[v]=max(dp[v], (int)s[v]);
}
reverse(all(vc));
for(auto &v: vc){
int ne=-s[v];
for(auto &i: adj[v]){
ne+=(i == par[v] ? le[v] : dp[i]);
}
for(auto &i: adj[v]){
if(i == par[v]) continue;
le[i]=max((int)s[v], ne-dp[i]);
}
}
For(i,1,n){
int ne=-s[i];
for(auto &j: adj[i]){
ne+=(j == par[i] ? le[i] : dp[j]);
}
res=max(res, ne);
}
cout << res;
}
signed main(int argc, char *argv[]){
iostream::sync_with_stdio(0);
int T=1;
while(T--) rmain();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5036 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 |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 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 |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 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 |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 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 |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5036 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 |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 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 |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 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 |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 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 |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5088 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5148 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 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 |
5152 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5148 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 |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 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 |
4 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
3 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5184 KB |
Output is correct |
51 |
Correct |
3 ms |
5068 KB |
Output is correct |
52 |
Correct |
4 ms |
5076 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
4 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
4 ms |
5076 KB |
Output is correct |
57 |
Correct |
4 ms |
5204 KB |
Output is correct |
58 |
Correct |
3 ms |
5076 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5196 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5036 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 |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 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 |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 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 |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 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 |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5088 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5148 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 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 |
5152 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5148 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 |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 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 |
4 ms |
5076 KB |
Output is correct |
48 |
Correct |
3 ms |
5204 KB |
Output is correct |
49 |
Correct |
3 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5184 KB |
Output is correct |
51 |
Correct |
3 ms |
5068 KB |
Output is correct |
52 |
Correct |
4 ms |
5076 KB |
Output is correct |
53 |
Correct |
3 ms |
5076 KB |
Output is correct |
54 |
Correct |
4 ms |
5076 KB |
Output is correct |
55 |
Correct |
3 ms |
5076 KB |
Output is correct |
56 |
Correct |
4 ms |
5076 KB |
Output is correct |
57 |
Correct |
4 ms |
5204 KB |
Output is correct |
58 |
Correct |
3 ms |
5076 KB |
Output is correct |
59 |
Correct |
3 ms |
5076 KB |
Output is correct |
60 |
Correct |
3 ms |
5204 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5196 KB |
Output is correct |
63 |
Correct |
4 ms |
5076 KB |
Output is correct |
64 |
Correct |
155 ms |
17512 KB |
Output is correct |
65 |
Correct |
151 ms |
17456 KB |
Output is correct |
66 |
Correct |
177 ms |
17560 KB |
Output is correct |
67 |
Correct |
158 ms |
17448 KB |
Output is correct |
68 |
Correct |
147 ms |
17532 KB |
Output is correct |
69 |
Correct |
161 ms |
17556 KB |
Output is correct |
70 |
Correct |
154 ms |
17456 KB |
Output is correct |
71 |
Correct |
149 ms |
17484 KB |
Output is correct |
72 |
Correct |
147 ms |
17580 KB |
Output is correct |
73 |
Correct |
199 ms |
27512 KB |
Output is correct |
74 |
Correct |
170 ms |
27528 KB |
Output is correct |
75 |
Correct |
141 ms |
17344 KB |
Output is correct |
76 |
Correct |
158 ms |
17392 KB |
Output is correct |
77 |
Correct |
211 ms |
17332 KB |
Output is correct |
78 |
Correct |
163 ms |
17456 KB |
Output is correct |
79 |
Correct |
195 ms |
17448 KB |
Output is correct |
80 |
Correct |
155 ms |
17380 KB |
Output is correct |
81 |
Correct |
128 ms |
17868 KB |
Output is correct |
82 |
Correct |
161 ms |
17584 KB |
Output is correct |
83 |
Correct |
212 ms |
21584 KB |
Output is correct |
84 |
Correct |
169 ms |
23260 KB |
Output is correct |
85 |
Correct |
186 ms |
23000 KB |
Output is correct |
86 |
Correct |
169 ms |
18604 KB |
Output is correct |
87 |
Correct |
154 ms |
18060 KB |
Output is correct |
88 |
Correct |
172 ms |
18660 KB |
Output is correct |
89 |
Correct |
183 ms |
18664 KB |
Output is correct |
90 |
Correct |
184 ms |
18436 KB |
Output is correct |
91 |
Correct |
171 ms |
18600 KB |
Output is correct |
92 |
Correct |
172 ms |
18904 KB |
Output is correct |
93 |
Correct |
188 ms |
20648 KB |
Output is correct |
94 |
Correct |
141 ms |
22008 KB |
Output is correct |
95 |
Correct |
151 ms |
21108 KB |
Output is correct |
96 |
Correct |
198 ms |
19664 KB |
Output is correct |
97 |
Correct |
171 ms |
18464 KB |
Output is correct |
98 |
Correct |
156 ms |
18028 KB |
Output is correct |
99 |
Correct |
148 ms |
17908 KB |
Output is correct |
100 |
Correct |
168 ms |
20420 KB |
Output is correct |
101 |
Correct |
150 ms |
18788 KB |
Output is correct |
102 |
Correct |
140 ms |
18244 KB |
Output is correct |
103 |
Correct |
143 ms |
18008 KB |
Output is correct |
104 |
Correct |
80 ms |
18240 KB |
Output is correct |
105 |
Correct |
200 ms |
25056 KB |
Output is correct |
106 |
Correct |
76 ms |
18092 KB |
Output is correct |
107 |
Correct |
76 ms |
18076 KB |
Output is correct |
108 |
Correct |
167 ms |
28160 KB |
Output is correct |
109 |
Correct |
108 ms |
18264 KB |
Output is correct |
110 |
Correct |
193 ms |
28460 KB |
Output is correct |
111 |
Correct |
138 ms |
17536 KB |
Output is correct |