#ifndef CPL_TEMPLATE
#define CPL_TEMPLATE
/*
Normie's Template v2.5
Changes:
Added warning against using pragmas on USACO.
*/
// Standard library in one include.
#include <bits/stdc++.h>
using namespace std;
// ordered_set library.
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set(el) tree<el,null_type,less<el>,rb_tree_tag,tree_order_statistics_node_update>
// AtCoder library. (Comment out these two lines if you're not submitting in AtCoder.) (Or if you want to use it in other judges, run expander.py first.)
//#include <atcoder/all>
//using namespace atcoder;
//Pragmas (Comment out these three lines if you're submitting in szkopul or USACO.)
//File I/O.
#define FILE_IN "cseq.inp"
#define FILE_OUT "cseq.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
//Fast I/O.
#define fio ios::sync_with_stdio(0);cin.tie(0)
#define nfio cin.tie(0)
#define endl "\n"
//Order checking.
#define ord(a,b,c) ((a>=b)and(b>=c))
//min/max redefines, so i dont have to resolve annoying compile errors.
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))
// Fast min/max assigns to use with AVX.
// Requires g++ 9.2.0.
template<typename T>
__attribute__((always_inline)) void chkmin(T& a, const T& b) {
a=(a<b)?a:b;
}
template<typename T>
__attribute__((always_inline)) void chkmax(T& a, const T& b) {
a=(a>b)?a:b;
}
//Constants.
#define MOD (ll(998244353))
#define MAX 300001
#define mag 320
const long double PI=3.14159265358979;
//Pairs and 3-pairs.
#define p1 first
#define p2 second.first
#define p3 second.second
#define fi first
#define se second
#define pii(element_type) pair<element_type,element_type>
#define piii(element_type) pair<element_type,pii(element_type)>
//Quick power of 2.
#define pow2(x) (ll(1)<<x)
//Short for-loops.
#define ff(i,__,___) for(int i=__;i<=___;i++)
#define rr(i,__,___) for(int i=__;i>=___;i--)
//Typedefs.
#define bi BigInt
typedef long long ll;
typedef long double ld;
typedef short sh;
// Binpow and stuff
ll BOW(ll a, ll x, ll p)
{
if (!x) return 1;
ll res=BOW(a,x/2,p);
res*=res;
res%=p;
if (x%2) res*=a;
return res%p;
}
ll INV(ll a, ll p)
{
return BOW(a,p-2,p);
}
//---------END-------//
#endif
vector<int> gt[200001];
int n,m,i,j,k,t,t1,u,v,a,b;
int dp[200001][2];
string gen;
void calc(int x, int p) {
int cur,a,b,c;
vector<int> ch;
for (auto g : gt[x]) if (g-p) {
calc(g,x);
ch.push_back(g);
}
if (gen[x]=='0') {
cur=0; a=0; b=0;
for (auto g : ch) {
a+=dp[g][1];
b=max(b,dp[g][0]);
}
dp[x][0]=max(a,b);
dp[x][1]=a;
}
else {
cur=1;
dp[x][cur]=1;
a=0; b=0;
for (auto g : ch) {
a+=dp[g][1];
b=max(b,dp[g][1]);
}
dp[x][cur]=max(dp[x][cur],-1+a);
// dp[x][cur]=max(dp[x][cur],b);
cur=0;
dp[x][cur]=-1e9;
a=0; b=0; c=0;
for (auto g : ch) {
a+=dp[g][1];
b=max(b,dp[g][0]);
c=max(c,dp[g][1]);
}
dp[x][cur]=max(dp[x][cur],-1+a);
dp[x][cur]=max(dp[x][cur],b);
dp[x][cur]=max(dp[x][cur],c+1);
}
// cout<<"A\n";
// cout<<x<<' '<<gen[x]<<' '<<dp[x][0]<<' '<<dp[x][1]<<endl;
// for (auto g : ch) cout<<g<<' ';
// cout<<endl;
}
int main()
{
fio;
cin>>n;
for (i=1;i<n;i++) {
cin>>a>>b; a--; b--;
gt[a].push_back(b);
gt[b].push_back(a);
}
cin>>gen;
calc(0,-1);
cout<<dp[0][0];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5008 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
4908 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5016 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5012 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5008 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
4908 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5016 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5012 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5012 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5024 KB |
Output is correct |
24 |
Correct |
4 ms |
5028 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5024 KB |
Output is correct |
27 |
Correct |
4 ms |
5032 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5288 KB |
Output is correct |
30 |
Correct |
4 ms |
5156 KB |
Output is correct |
31 |
Correct |
4 ms |
5020 KB |
Output is correct |
32 |
Correct |
3 ms |
5024 KB |
Output is correct |
33 |
Correct |
4 ms |
5028 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5080 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
3 ms |
5028 KB |
Output is correct |
39 |
Correct |
4 ms |
5148 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
4 ms |
5156 KB |
Output is correct |
42 |
Correct |
4 ms |
5060 KB |
Output is correct |
43 |
Correct |
3 ms |
5024 KB |
Output is correct |
44 |
Correct |
4 ms |
5196 KB |
Output is correct |
45 |
Correct |
4 ms |
5068 KB |
Output is correct |
46 |
Correct |
4 ms |
5068 KB |
Output is correct |
47 |
Correct |
4 ms |
5068 KB |
Output is correct |
48 |
Correct |
3 ms |
5196 KB |
Output is correct |
49 |
Correct |
4 ms |
5196 KB |
Output is correct |
50 |
Correct |
3 ms |
5068 KB |
Output is correct |
51 |
Correct |
4 ms |
5132 KB |
Output is correct |
52 |
Correct |
4 ms |
5068 KB |
Output is correct |
53 |
Correct |
3 ms |
5068 KB |
Output is correct |
54 |
Correct |
4 ms |
5068 KB |
Output is correct |
55 |
Correct |
4 ms |
5068 KB |
Output is correct |
56 |
Correct |
3 ms |
5024 KB |
Output is correct |
57 |
Correct |
3 ms |
5196 KB |
Output is correct |
58 |
Correct |
4 ms |
5032 KB |
Output is correct |
59 |
Correct |
3 ms |
5068 KB |
Output is correct |
60 |
Correct |
4 ms |
5196 KB |
Output is correct |
61 |
Correct |
4 ms |
5068 KB |
Output is correct |
62 |
Correct |
4 ms |
5196 KB |
Output is correct |
63 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5008 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
4908 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5016 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5012 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5012 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5024 KB |
Output is correct |
24 |
Correct |
4 ms |
5028 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5024 KB |
Output is correct |
27 |
Correct |
4 ms |
5032 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5288 KB |
Output is correct |
30 |
Correct |
4 ms |
5156 KB |
Output is correct |
31 |
Correct |
4 ms |
5020 KB |
Output is correct |
32 |
Correct |
3 ms |
5024 KB |
Output is correct |
33 |
Correct |
4 ms |
5028 KB |
Output is correct |
34 |
Correct |
4 ms |
5072 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5080 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
3 ms |
5028 KB |
Output is correct |
39 |
Correct |
4 ms |
5148 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
4 ms |
5156 KB |
Output is correct |
42 |
Correct |
4 ms |
5060 KB |
Output is correct |
43 |
Correct |
3 ms |
5024 KB |
Output is correct |
44 |
Correct |
4 ms |
5196 KB |
Output is correct |
45 |
Correct |
4 ms |
5068 KB |
Output is correct |
46 |
Correct |
4 ms |
5068 KB |
Output is correct |
47 |
Correct |
4 ms |
5068 KB |
Output is correct |
48 |
Correct |
3 ms |
5196 KB |
Output is correct |
49 |
Correct |
4 ms |
5196 KB |
Output is correct |
50 |
Correct |
3 ms |
5068 KB |
Output is correct |
51 |
Correct |
4 ms |
5132 KB |
Output is correct |
52 |
Correct |
4 ms |
5068 KB |
Output is correct |
53 |
Correct |
3 ms |
5068 KB |
Output is correct |
54 |
Correct |
4 ms |
5068 KB |
Output is correct |
55 |
Correct |
4 ms |
5068 KB |
Output is correct |
56 |
Correct |
3 ms |
5024 KB |
Output is correct |
57 |
Correct |
3 ms |
5196 KB |
Output is correct |
58 |
Correct |
4 ms |
5032 KB |
Output is correct |
59 |
Correct |
3 ms |
5068 KB |
Output is correct |
60 |
Correct |
4 ms |
5196 KB |
Output is correct |
61 |
Correct |
4 ms |
5068 KB |
Output is correct |
62 |
Correct |
4 ms |
5196 KB |
Output is correct |
63 |
Correct |
4 ms |
5068 KB |
Output is correct |
64 |
Correct |
146 ms |
15880 KB |
Output is correct |
65 |
Correct |
156 ms |
15876 KB |
Output is correct |
66 |
Correct |
155 ms |
15884 KB |
Output is correct |
67 |
Correct |
150 ms |
15996 KB |
Output is correct |
68 |
Correct |
152 ms |
15984 KB |
Output is correct |
69 |
Correct |
170 ms |
16156 KB |
Output is correct |
70 |
Correct |
149 ms |
15936 KB |
Output is correct |
71 |
Correct |
161 ms |
15888 KB |
Output is correct |
72 |
Correct |
151 ms |
15984 KB |
Output is correct |
73 |
Correct |
144 ms |
33676 KB |
Output is correct |
74 |
Correct |
165 ms |
33660 KB |
Output is correct |
75 |
Correct |
155 ms |
15852 KB |
Output is correct |
76 |
Correct |
157 ms |
15820 KB |
Output is correct |
77 |
Correct |
154 ms |
15744 KB |
Output is correct |
78 |
Correct |
131 ms |
15836 KB |
Output is correct |
79 |
Correct |
163 ms |
15784 KB |
Output is correct |
80 |
Correct |
128 ms |
15752 KB |
Output is correct |
81 |
Correct |
115 ms |
17144 KB |
Output is correct |
82 |
Correct |
142 ms |
16320 KB |
Output is correct |
83 |
Correct |
169 ms |
23436 KB |
Output is correct |
84 |
Correct |
169 ms |
26728 KB |
Output is correct |
85 |
Correct |
159 ms |
26156 KB |
Output is correct |
86 |
Correct |
161 ms |
17912 KB |
Output is correct |
87 |
Correct |
151 ms |
17072 KB |
Output is correct |
88 |
Correct |
124 ms |
17468 KB |
Output is correct |
89 |
Correct |
123 ms |
17436 KB |
Output is correct |
90 |
Correct |
148 ms |
17096 KB |
Output is correct |
91 |
Correct |
149 ms |
17468 KB |
Output is correct |
92 |
Correct |
144 ms |
17992 KB |
Output is correct |
93 |
Correct |
121 ms |
21668 KB |
Output is correct |
94 |
Correct |
132 ms |
24216 KB |
Output is correct |
95 |
Correct |
134 ms |
22048 KB |
Output is correct |
96 |
Correct |
138 ms |
19548 KB |
Output is correct |
97 |
Correct |
116 ms |
17224 KB |
Output is correct |
98 |
Correct |
120 ms |
16564 KB |
Output is correct |
99 |
Correct |
152 ms |
16256 KB |
Output is correct |
100 |
Correct |
143 ms |
20820 KB |
Output is correct |
101 |
Correct |
131 ms |
17920 KB |
Output is correct |
102 |
Correct |
141 ms |
16788 KB |
Output is correct |
103 |
Correct |
138 ms |
16464 KB |
Output is correct |
104 |
Correct |
84 ms |
18540 KB |
Output is correct |
105 |
Correct |
172 ms |
29204 KB |
Output is correct |
106 |
Correct |
84 ms |
18280 KB |
Output is correct |
107 |
Correct |
78 ms |
18252 KB |
Output is correct |
108 |
Correct |
141 ms |
34744 KB |
Output is correct |
109 |
Correct |
83 ms |
18436 KB |
Output is correct |
110 |
Correct |
150 ms |
35132 KB |
Output is correct |
111 |
Correct |
130 ms |
15764 KB |
Output is correct |