/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
#include<bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI acos(-1.0)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define BIT(x,i) (1&((x)>>(i)))
#define MASK(x) (1LL<<(x))
#define task "tnc"
typedef long long ll;
typedef long double ld;
const ll INF=1e18;
const int maxn=1e6+5;
const int mod=1e9+7;
const int mo=998244353;
using pi=pair<ll,ll>;
using vi=vector<ll>;
using pii=pair<pair<ll,ll>,ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
string s;
int dp[maxn];
int re[maxn];
vector<int>G[maxn];
int sum[maxn];
int n;
int ans=0;
void dfs(int u,int pa){
for(auto v:G[u]){
if(v==pa)continue;
dfs(v,u);
if(s[u]=='1' && s[v]=='1'){
ans=max(ans,2);
}
sum[u]+=dp[v];
}
dp[u]=max(s[u]-'0',sum[u]-(s[u]-'0'));
}
void re_dfs(int u,int pa){
if(u!=1){
re[u]=max(s[pa]-'0',sum[pa]-dp[u]+re[pa]-(s[pa]-'0'));
}
if(s[u]=='1'){
ans=max(ans,max(1,re[u]+sum[u]-1));
}
else{
ans=max(ans,re[u]+sum[u]);
}
for(auto v:G[u]){
if(v==pa)continue;
re_dfs(v,u);
}
}
signed main()
{
cin.tie(0),cout.tie(0)->sync_with_stdio(0);
//freopen(task".inp" , "r" , stdin);
//freopen(task".out" , "w" , stdout);
cin>>n;
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;
G[u].pb(v);
G[v].pb(u);
}
cin>>s;
s=' '+s;
dfs(1,-1);
re_dfs(1,-1);
cout<<ans<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23764 KB |
Output is correct |
4 |
Correct |
15 ms |
23784 KB |
Output is correct |
5 |
Correct |
16 ms |
23812 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23784 KB |
Output is correct |
9 |
Correct |
14 ms |
23836 KB |
Output is correct |
10 |
Correct |
12 ms |
23832 KB |
Output is correct |
11 |
Correct |
13 ms |
23836 KB |
Output is correct |
12 |
Correct |
13 ms |
23832 KB |
Output is correct |
13 |
Correct |
13 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23744 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23784 KB |
Output is correct |
17 |
Correct |
13 ms |
23764 KB |
Output is correct |
18 |
Correct |
14 ms |
23844 KB |
Output is correct |
19 |
Correct |
14 ms |
23840 KB |
Output is correct |
20 |
Correct |
15 ms |
23776 KB |
Output is correct |
21 |
Correct |
16 ms |
23840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23764 KB |
Output is correct |
4 |
Correct |
15 ms |
23784 KB |
Output is correct |
5 |
Correct |
16 ms |
23812 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23784 KB |
Output is correct |
9 |
Correct |
14 ms |
23836 KB |
Output is correct |
10 |
Correct |
12 ms |
23832 KB |
Output is correct |
11 |
Correct |
13 ms |
23836 KB |
Output is correct |
12 |
Correct |
13 ms |
23832 KB |
Output is correct |
13 |
Correct |
13 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23744 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23784 KB |
Output is correct |
17 |
Correct |
13 ms |
23764 KB |
Output is correct |
18 |
Correct |
14 ms |
23844 KB |
Output is correct |
19 |
Correct |
14 ms |
23840 KB |
Output is correct |
20 |
Correct |
15 ms |
23776 KB |
Output is correct |
21 |
Correct |
16 ms |
23840 KB |
Output is correct |
22 |
Correct |
14 ms |
23892 KB |
Output is correct |
23 |
Correct |
14 ms |
23848 KB |
Output is correct |
24 |
Correct |
14 ms |
23864 KB |
Output is correct |
25 |
Correct |
14 ms |
23844 KB |
Output is correct |
26 |
Correct |
13 ms |
23892 KB |
Output is correct |
27 |
Correct |
13 ms |
23892 KB |
Output is correct |
28 |
Correct |
17 ms |
23892 KB |
Output is correct |
29 |
Correct |
15 ms |
24072 KB |
Output is correct |
30 |
Correct |
17 ms |
24020 KB |
Output is correct |
31 |
Correct |
14 ms |
23844 KB |
Output is correct |
32 |
Correct |
14 ms |
23852 KB |
Output is correct |
33 |
Correct |
14 ms |
23880 KB |
Output is correct |
34 |
Correct |
14 ms |
23868 KB |
Output is correct |
35 |
Correct |
15 ms |
23852 KB |
Output is correct |
36 |
Correct |
13 ms |
23848 KB |
Output is correct |
37 |
Correct |
14 ms |
23884 KB |
Output is correct |
38 |
Correct |
15 ms |
23844 KB |
Output is correct |
39 |
Correct |
15 ms |
23968 KB |
Output is correct |
40 |
Correct |
15 ms |
23892 KB |
Output is correct |
41 |
Correct |
17 ms |
23964 KB |
Output is correct |
42 |
Correct |
14 ms |
23972 KB |
Output is correct |
43 |
Correct |
15 ms |
23892 KB |
Output is correct |
44 |
Correct |
14 ms |
23976 KB |
Output is correct |
45 |
Correct |
17 ms |
23872 KB |
Output is correct |
46 |
Correct |
13 ms |
23968 KB |
Output is correct |
47 |
Correct |
14 ms |
23972 KB |
Output is correct |
48 |
Correct |
15 ms |
24020 KB |
Output is correct |
49 |
Correct |
13 ms |
24020 KB |
Output is correct |
50 |
Correct |
15 ms |
23892 KB |
Output is correct |
51 |
Correct |
16 ms |
23896 KB |
Output is correct |
52 |
Correct |
14 ms |
23892 KB |
Output is correct |
53 |
Correct |
15 ms |
23884 KB |
Output is correct |
54 |
Correct |
13 ms |
23940 KB |
Output is correct |
55 |
Correct |
14 ms |
23972 KB |
Output is correct |
56 |
Correct |
14 ms |
23836 KB |
Output is correct |
57 |
Correct |
14 ms |
24020 KB |
Output is correct |
58 |
Correct |
13 ms |
23868 KB |
Output is correct |
59 |
Correct |
13 ms |
23836 KB |
Output is correct |
60 |
Correct |
14 ms |
24020 KB |
Output is correct |
61 |
Correct |
19 ms |
23892 KB |
Output is correct |
62 |
Correct |
14 ms |
23940 KB |
Output is correct |
63 |
Correct |
16 ms |
24020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23764 KB |
Output is correct |
4 |
Correct |
15 ms |
23784 KB |
Output is correct |
5 |
Correct |
16 ms |
23812 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23784 KB |
Output is correct |
9 |
Correct |
14 ms |
23836 KB |
Output is correct |
10 |
Correct |
12 ms |
23832 KB |
Output is correct |
11 |
Correct |
13 ms |
23836 KB |
Output is correct |
12 |
Correct |
13 ms |
23832 KB |
Output is correct |
13 |
Correct |
13 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23744 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23784 KB |
Output is correct |
17 |
Correct |
13 ms |
23764 KB |
Output is correct |
18 |
Correct |
14 ms |
23844 KB |
Output is correct |
19 |
Correct |
14 ms |
23840 KB |
Output is correct |
20 |
Correct |
15 ms |
23776 KB |
Output is correct |
21 |
Correct |
16 ms |
23840 KB |
Output is correct |
22 |
Correct |
14 ms |
23892 KB |
Output is correct |
23 |
Correct |
14 ms |
23848 KB |
Output is correct |
24 |
Correct |
14 ms |
23864 KB |
Output is correct |
25 |
Correct |
14 ms |
23844 KB |
Output is correct |
26 |
Correct |
13 ms |
23892 KB |
Output is correct |
27 |
Correct |
13 ms |
23892 KB |
Output is correct |
28 |
Correct |
17 ms |
23892 KB |
Output is correct |
29 |
Correct |
15 ms |
24072 KB |
Output is correct |
30 |
Correct |
17 ms |
24020 KB |
Output is correct |
31 |
Correct |
14 ms |
23844 KB |
Output is correct |
32 |
Correct |
14 ms |
23852 KB |
Output is correct |
33 |
Correct |
14 ms |
23880 KB |
Output is correct |
34 |
Correct |
14 ms |
23868 KB |
Output is correct |
35 |
Correct |
15 ms |
23852 KB |
Output is correct |
36 |
Correct |
13 ms |
23848 KB |
Output is correct |
37 |
Correct |
14 ms |
23884 KB |
Output is correct |
38 |
Correct |
15 ms |
23844 KB |
Output is correct |
39 |
Correct |
15 ms |
23968 KB |
Output is correct |
40 |
Correct |
15 ms |
23892 KB |
Output is correct |
41 |
Correct |
17 ms |
23964 KB |
Output is correct |
42 |
Correct |
14 ms |
23972 KB |
Output is correct |
43 |
Correct |
15 ms |
23892 KB |
Output is correct |
44 |
Correct |
14 ms |
23976 KB |
Output is correct |
45 |
Correct |
17 ms |
23872 KB |
Output is correct |
46 |
Correct |
13 ms |
23968 KB |
Output is correct |
47 |
Correct |
14 ms |
23972 KB |
Output is correct |
48 |
Correct |
15 ms |
24020 KB |
Output is correct |
49 |
Correct |
13 ms |
24020 KB |
Output is correct |
50 |
Correct |
15 ms |
23892 KB |
Output is correct |
51 |
Correct |
16 ms |
23896 KB |
Output is correct |
52 |
Correct |
14 ms |
23892 KB |
Output is correct |
53 |
Correct |
15 ms |
23884 KB |
Output is correct |
54 |
Correct |
13 ms |
23940 KB |
Output is correct |
55 |
Correct |
14 ms |
23972 KB |
Output is correct |
56 |
Correct |
14 ms |
23836 KB |
Output is correct |
57 |
Correct |
14 ms |
24020 KB |
Output is correct |
58 |
Correct |
13 ms |
23868 KB |
Output is correct |
59 |
Correct |
13 ms |
23836 KB |
Output is correct |
60 |
Correct |
14 ms |
24020 KB |
Output is correct |
61 |
Correct |
19 ms |
23892 KB |
Output is correct |
62 |
Correct |
14 ms |
23940 KB |
Output is correct |
63 |
Correct |
16 ms |
24020 KB |
Output is correct |
64 |
Correct |
157 ms |
35584 KB |
Output is correct |
65 |
Correct |
162 ms |
35480 KB |
Output is correct |
66 |
Correct |
148 ms |
35576 KB |
Output is correct |
67 |
Correct |
199 ms |
35576 KB |
Output is correct |
68 |
Correct |
177 ms |
35588 KB |
Output is correct |
69 |
Correct |
179 ms |
35592 KB |
Output is correct |
70 |
Correct |
183 ms |
35516 KB |
Output is correct |
71 |
Correct |
198 ms |
35540 KB |
Output is correct |
72 |
Correct |
163 ms |
35584 KB |
Output is correct |
73 |
Correct |
197 ms |
48100 KB |
Output is correct |
74 |
Correct |
221 ms |
48192 KB |
Output is correct |
75 |
Correct |
149 ms |
35360 KB |
Output is correct |
76 |
Correct |
158 ms |
35532 KB |
Output is correct |
77 |
Correct |
159 ms |
35400 KB |
Output is correct |
78 |
Correct |
210 ms |
35464 KB |
Output is correct |
79 |
Correct |
179 ms |
35464 KB |
Output is correct |
80 |
Correct |
170 ms |
35460 KB |
Output is correct |
81 |
Correct |
170 ms |
35820 KB |
Output is correct |
82 |
Correct |
160 ms |
35664 KB |
Output is correct |
83 |
Correct |
182 ms |
40652 KB |
Output is correct |
84 |
Correct |
198 ms |
42936 KB |
Output is correct |
85 |
Correct |
191 ms |
42396 KB |
Output is correct |
86 |
Correct |
188 ms |
36856 KB |
Output is correct |
87 |
Correct |
189 ms |
36280 KB |
Output is correct |
88 |
Correct |
169 ms |
36768 KB |
Output is correct |
89 |
Correct |
172 ms |
36776 KB |
Output is correct |
90 |
Correct |
202 ms |
36600 KB |
Output is correct |
91 |
Correct |
165 ms |
36776 KB |
Output is correct |
92 |
Correct |
157 ms |
37092 KB |
Output is correct |
93 |
Correct |
168 ms |
39488 KB |
Output is correct |
94 |
Correct |
186 ms |
41248 KB |
Output is correct |
95 |
Correct |
166 ms |
39872 KB |
Output is correct |
96 |
Correct |
154 ms |
38196 KB |
Output is correct |
97 |
Correct |
171 ms |
36632 KB |
Output is correct |
98 |
Correct |
148 ms |
36144 KB |
Output is correct |
99 |
Correct |
157 ms |
35908 KB |
Output is correct |
100 |
Correct |
161 ms |
39032 KB |
Output is correct |
101 |
Correct |
173 ms |
37028 KB |
Output is correct |
102 |
Correct |
145 ms |
36260 KB |
Output is correct |
103 |
Correct |
152 ms |
36048 KB |
Output is correct |
104 |
Correct |
103 ms |
35588 KB |
Output is correct |
105 |
Correct |
209 ms |
45096 KB |
Output is correct |
106 |
Correct |
99 ms |
35596 KB |
Output is correct |
107 |
Correct |
106 ms |
35500 KB |
Output is correct |
108 |
Correct |
227 ms |
48932 KB |
Output is correct |
109 |
Correct |
105 ms |
35588 KB |
Output is correct |
110 |
Correct |
192 ms |
49392 KB |
Output is correct |
111 |
Correct |
144 ms |
35364 KB |
Output is correct |